我见过最有趣的代码注释,都在这里了

537 阅读6分钟

代码注释的作用,不需要对程序员解释了。有时在查看他人代码,能看到一些令人不禁大笑的注释。

1. Adon的分享

或者:

 // 写这段代码的时候,只有上帝和我知道它是干嘛的
 
 // 现在只有上帝知道

2.Anirubha Bose的分享

谢尔盖.布林(Google的联合创始人之一)在斯坦福大学念计算机科学博士学位的时候,他的简历里并不含任何”待遇要求“(Objective)的字眼。但当你去查看他的简历的HTML源代码的时候,你会看到(他在简历HTML源文件里明确写了“待遇要求”,只是用注释注掉了,在浏览器页面上不显示。)

(其“待遇要求“的内容是:办公室要大,挣钱要多,干活要少。如果能经常去奇妙的地方旅行而且还能给报销的话,那就更好了。)

3.Liu Wei 的分享

我在一周前在社交网站上看到很多人在讨论这个网站,网站的源代码包含了这些注释。

有人说,这家公司应该加强对代码的审核机制。有人则怀疑这家公司可能没有足够的人力资源来做代码审核,因为至少需要两个程序员才能完成这项工作。

📍Facebook大佬带你拆解大厂原题,理顺解题思路,1月内高效备战算法面试。

  • 解析五个一线大厂(字节跳动/美团/快手/网易/blibli)的40多道2020算法真题+99道配套巩固习题

  • 搭建面试必备知识框架,让你快速掌握面试核心软硬技能。

点击链接即可立即报名免费试听www.jiuzhang.com/course/73/?…

4.Abhinav Upadhyay的分享

 /* You are not expected to understand this */
 
 /* 我们并不指望你能看懂这段话 */

这段注释并不是我亲眼所见,但是它在网上传得很厉害。这段注释是出自于贝尔实验室的Unix系统第六发行版,并在《Lions’ Commentary on UNIX 6th Edition, with Source Code》这本书中标注出来的。

代码和标注的细节如下:

/*

 * 切换到新进程栈,并设置其段寄存器

 */

retu(rp->p_addr);

sureg();

/*

 * 如果新进程因为被换出而暂停,则设置其栈级别为last call,并传至sayu(u_ssay). 

 * 这样做的目的是确保aretu方法被调用后立即返回的值实质上是上一次调用sayu的方法的返回值。

 *

 * You are not expected to understand this.

 * 我们并不指望你能看懂这段话

 */

if(rp->p_flag&SSWAP) {

   rp->p_flag =& ~SSWAP;

   aretu(u.u_ssav);

}

 /*

 * The value returned here has many subtle implications.

 * See the newproc comments.

 */

return(1);

5.Sasha Krassovsky的分享

曾经有一次, 我从学生交给我的代码里随便挑了一份来看,然后发现了这么一条注释:

 /* Do NOT delete this comment */
 
 /* 不要删除这段注释 */

当然,我就是要试一下如果把注释删除了到底会怎么样。所以我就删除了,然后重新编译。结果程序真的就不能运行了。然后我把注释重新加回去,结果又好了。

删掉注释以后会报错 LINK1000,根据链接器错误文档的说明,错误的原因直接就是:“未知错误;请参考文档或寻求技术支持。”

为什么这个注释不能去掉呢?我估计这个问题对我来说一直就是个谜。

6.Edwin Romero的分享

我不确认有多少人熟悉站点内的Robots.txt这个文件。其实这个文件不是运行必要的代码,但是它声明了爬虫/搜索引擎能爬到/搜到站点的哪些内容。

我在Nike网站上发现的Robots.txt文件非常有意思,并在里面加入了一个有趣的图案:

7.Terry Lambert的分享

我最喜欢的注释有两个,都是Bill Paul写的。这家伙为FreeBSD做了大量的工作,现在受雇于Wind River System,听说这个公司最近要被Intel收购了。Bill是一个非常有才华的程序员,但是他对愚蠢的容忍度也出奇的低,并且他的幽默感也很不同寻常。

下面是我喜欢的第一条注释,这是从RelTek 8129/8139 PCI NIC 驱动程序里找到的。

 /*
 
 * The RealTek 8139 PCI NIC redefines the meaning of 'low end.'
 
    RealTek 8139 PCI NIC重刷了low逼的下限
 
 * 这可能是史上写得最烂的PCI以太网控制器驱动
 
 * with the possible exception of the FEAST chip made by SMC.> 
 * The 8139 supports bus-master DMA, but it has a terrible
 
 * interface that nullifies any performance gains that
 
 * bus-master DMA usually offers.
 
 *
 * For transmission, the chip offers a series of four TX
 
 * descriptor registers. Each transmit frame must be in a
 
 * contiguous buffer, aligned on a longword (32-bit) boundary.
 
 * This means we almost always have to do mbuf copies in order
 
 * to transmit a frame, except in the unlikely case where a)
 
 * the packet fits into a single mbuf, and b) the packet is
 
 * 32-bit aligned within the mbuf's data area. The presence of
 
  * only four descriptor registers means that we can never have
 
  * more than four packets queued for transmission at any one
 
  * time.
 
  *
 
  * Reception is not much better. The driver has to allocate a
 
  * single large buffer area (up to 64K in size) into which the
 
 * chip will DMA received frames. Because we don't know where
 
 * within this region received packets will begin or end, we

 * have no choice but to copy data from the buffer area into
 
 * mbufs in order to pass the packets up to the higher
 
 * protocol levels.
 
 *
 
 * It's impossible given this rotten design to really achieve
 
  要让这么烂的设计去达到100Mbps的速度简直就是天方夜谭
 
  * decent performance at 100Mbps, unless you happen to have a
 
    除非你有一台CPU强劲的电脑去驱动
 
  * 400Mhz PII or some equally overmuscled CPU to drive it.
 
  *
 
  * On the bright side, the 8139 does have a built-in PHY,
 
  * although rather than using an MDIO serial interface like
 
  * most other NICs, the PHY registers are directly accessible
 
 * through the 8139's register space. The 8139 supports
 
 * autonegotiation, as well as a 64-bit multicast filter.
 
 *

这绝对是一个很爽的注释。传说为了让 Bill 能把这段注释删掉/修订/修改/更新等等,厂家用了各种条件去诱惑他,但是他都拒绝了。

第二段注释是写在一个修改版的BSD许可证的“限制伤害”条款里的,Bill在他的代码里引用了这个许可证协议。其实它并没有对原先的协议做大的修改,所以很多人看到这个协议以后,一看跟模板差不多,然后就跳过了,几乎没什么人仔细去看整个文字。

 IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD BE LIABLE
 
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
 CONSEQUENTIAL DAMAGES

怎么样,你没见过这条吧。其实很容易就看掉了。有趣的地方正好在这里:

「Bill Paul以及他头脑中的想法绝不会直接,间接,偶然,特殊,典型或实质性地造成任何损害。」

总之,这哥们儿是个天才。