你遇到过的源代码中最好的注释是什么?


当前回答

几年前,我在一个没有单元测试可言的大型代码库中工作。

代码中隐藏了一个执行一些日历计算的方法。它有点坏了,由于一些不幸的情况,不得不以一种非常笨拙的方式处理夏令时。

我们不得不修了几次,每一次,我们都会在几个月后发现一些东西坏了。

在花了一整天的时间修复和分析之后,我把代码放到了源代码控制中,并附上了这样的评论:

// this code was written after a version trying to do {this} failed because of {reason},
// previously we were doing {this} which failed because of {reason}. This is 
// now written {this} way so that {lots of reasons here}. If you want to touch
// this code, please make sure that it produces the right answers when tested with:
//
// {some sort of unit test}

最终,我的团队被外包了。有时我想知道这段代码发生了什么:)

其他回答

我没有亲身经历过这种情况,但这是一个很好的故事(详见我的评论中的解释):

#define MSGTAG_B33R     0x723 /* RIPLVB */
#ifdef TRACE
#undef TRACE     /* All your trace are belong to us. */
#endif
#define TRACE ....
/* You are not meant to understand this */ 
long john; // silver

在一组相当长且复杂的while循环和if块的末尾,开发人员插入了最后的注释:

else
{
    // wobbly wilson said this would *never* happen!!
}

机智和讽刺的简洁混合:)