您遇到过的源代码中最好的注释是什么?[关闭]


360

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

Answers:


1462

我对此特别内,,在我的大多数项目中都嵌入了非建设性的注释,代码诗和小玩笑(尽管我通常有足够的意识在发布代码之前删除任何直接令人反感的内容)。这是我特别喜欢的一个,放在一个设计不当的“上帝对象”的下面,很远:

/**
* For the brave souls who get this far: You are the chosen ones,
* the valiant knights of programming who toil away, without rest,
* fixing our most awful code. To you, true saviors, kings of men,
* I say this: never gonna give you up, never gonna let you down,
* never gonna run around and desert you. Never gonna make you cry,
* never gonna say goodbye. Never gonna tell a lie and hurt you.
*/

对不起!!!!我只是无法帮助自己.....!

而另一位,这我承认我还没有真正释放到野外,即使我很动心,我不太直观的一个类可以这样做:

// 
// Dear maintainer:
// 
// Once you are done trying to 'optimize' this routine,
// and have realized what a terrible mistake that was,
// please increment the following counter as a warning
// to the next guy:
// 
// total_hours_wasted_here = 42
// 

2
///亲爱的程序员/////现在,由于我修复了您引入的所有错误,因此您的代码现在运行速度较慢。//但是您的优化肯定会有所帮助。
— DevinB,2009年

2
评论中的Rickrolling为+ .5,total_hours为+.5。很棒
— Wayne Werner 2010年

3
我第一次真正成为RickRolled,这很有趣。当我看到指向YouTube的链接时,我总是期望成为RickRolled(骆驼式英语!),但我从来没有期望过在代码注释中成为RickRolled。
— 文森特·麦克纳伯

4
total_hours_wasted_here = -32768,我该怎么办?我认为有些东西坏了。
— Behrooz

8
请停止“更新”计数器。再次将这个问题放在首页上是不值得的。
— 迈克尔·迈尔斯

1055
Exception up = new Exception("Something is really wrong.");
throw up;  //ha ha

5
我从头到尾阅读了所有评论,想知道人们所说的“抓紧,读这篇”或“抓紧,读那个”是什么意思。好。我刚读完这篇文章……
— 达伦·托马斯

37
从字面上看,LOL结合了这一点的聪明和幼稚。
— David Koelle

2
MonkeyPoo类扩展了Exception {}; ...抛出新的MonkeyPoo(); //因为抛出异常比抛出异常有趣得多
— Adam Rosenfield 2009年

2
我要从现在开始在XD上开始使用而不是用完
— Ammar

23
当我从真正的深度睡眠中醒来时,我的智商下降到25左右,我的头脑变得异常陌生。有一次,经过长时间的Java编写,我在半夜醒来时感到很不适,然后狂奔到洗手间。当我向厕所呕吐时,我唯一能想到的就是“我想知道我现在会抛出哪个异常……”
— Frank Farmer 2010年

1052
//When I wrote this, only God and I understood what I was doing
//Now, God only knows

175
这是数学家卡尔·韦斯特拉斯(Karl Weierstrass)的一句话,他为我们提供了出色的epsilon和delta连续性定义。
— Edwin Jarvis,

3
不可分辨的维尔斯特拉斯?
— Peter Wone

4
@Augusto,您是否有将该短语归因于Weierstrass的来源?我想知道他指的是什么。
— 凯文,


1030

这似乎阻止白痴混乱我的代码...

// Autogenerated, do not edit. All changes will be undone.

7
让我希望在个别回复中获得支持的收藏夹
— AviD 2009年


@alex,嗯,嗯,现在 ...无论如何,我的意思是一样的,你可以将一个问题标记为最爱...
— AviD 2010年

@AviD是的,我知道,只是建议一种解决方法。
— Alex

3
我认为他们反正会把您的代码弄乱:它说它会自行修复,那么为什么不把它打断呢?
— Mateen Ulhaq

946
// sometimes I believe compiler ignores all my comments

26
我希望编译器忽略此人的评论。
— Windows程序员

86
//因为我从没听到过编译器的消息,所以我相信这是对的。
— 利文

40
我认为这应该具有讽刺意味……
— 尼尔·巴恩韦尔

52
笑声,那就是未来,编译器在阅读注释只是为了确保他们没有误解
— ;-D

4
//#reply_from_compiler您喜欢clippy,不是吗?
— 卡尔,

930
// I dedicate all this code, all my work, to my wife, Darlene, who will 
// have to support me and our three children and the dog once it gets 
// released into the public.

169
这在永远的Duke Nukem源代码上吗?;)
— Adhip Gupta,

1
我不明白,有什么好笑的?
— 哈森

11
他将下面的代码献给了他的妻子,即使他知道这太糟糕了也可能导致他被解雇或使公司倒闭。:)
— 罗伯特·P

390
狗为什么要向公众释放?
— Nosredna,2009年

1
@Nosredna,谢谢你这么有趣的评论!@hasen j,代码编写得很糟,以至于“程序员”很可能会失业,因此他的妻子将不得不养家糊口。
— Jamol

911
//Code sanitized to protect the foolish.
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Web.UI;

namespace Mobile.Web.Control
{
    /// <summary>
    /// Class used to work around Richard being a fucking idiot
    /// </summary>
    /// <remarks>
    /// The point of this is to work around his poor design so that paging will 
    /// work on a mobile control. The main problem is the BindCompany() method, 
    /// which he hoped would be able to do everything. I hope he dies.
    /// </remarks>
    public abstract class RichardIsAFuckingIdiotControl : MobileBaseControl, ICompanyProfileControl
    {
        protected abstract Pager Pager { get; }

        public void BindCompany(int companyId) { }

        public RichardIsAFuckingIdiotControl()
        {
            MakeSureNobodyAccidentallyGetsBittenByRichardsStupidity();
        }

        private void MakeSureNobodyAccidentallyGetsBittenByRichardsStupidity()
        {
            // Make sure nobody is actually using that fucking bindcompany method
            MethodInfo m = this.GetType().GetMethod("BindCompany", BindingFlags.DeclaredOnly | 
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            if (m != null)
            {
                throw new RichardIsAFuckingIdiotException("No!! Don't use the fucking BindCompany method!!!");
            }
            // P.S. this method is a joke ... the rest of the class is fucking serious
        }

        /// <summary>
        /// This returns true if this control is supposed to be doing anything
        /// at all for this request. Richard thought it was a good idea to load
        /// the entire website during every request and have things turn themselves
        /// off. He also thought bandanas and aviator sunglasses were "fuckin' 
        /// gnarly, dude."
        /// </summary>
        protected bool IsThisTheRightPageImNotSureBecauseRichardIsDumb()
        {
            return Request.QueryString["Section"] == this.MenuItemKey;
        }

        protected override void OnLoad(EventArgs e)
        {
            if (IsThisTheRightPageImNotSureBecauseRichardIsDumb())
            {
                Page.LoadComplete += new EventHandler(Page_LoadComplete);
                Pager.RowCount = GetRowCountBecauseRichardIsDumb();
            }
            base.OnLoad(e);
        }

        protected abstract int GetRowCountBecauseRichardIsDumb();
        protected abstract void BindDataBecauseRichardIsDumb();

        void Page_LoadComplete(object sender, EventArgs e)
        {
            BindDataBecauseRichardIsDumb();
        }

        // the rest of his reduh-ndant interface members
        public abstract string MenuItemName { get; set; }
        public abstract string MenuItemKey { get; set; }
        public abstract bool IsCapable(CapabilityCheck checker, int companyId);
        public abstract bool ShowInMenu { get; }
        public virtual Control CreateHeaderControl()
        {
            return null;
        }
    }
}

更新: 代码的原始作者已经过时了,所以我必须在适当的时候给予感谢。丹·麦金莱(Dan McKinley)刚成立不久就离开了我所在的公司,他谈论了更多代码,并解释了“理查德”(Richard)撰写的一些背景知识和一些“ WTF”。


27
这真是令人ob目结舌,但我在阅读立方体的整个过程中确实在笑。
— willasaywhat

241
审查制度糟透了。带回原来的!
— 编码器

58
回滚。我们都是成年人。如果原始代码已被“清理”,您将永远不会记住它,无法在此处发布它。
— JosephStyons

56
如果客户在堆栈跟踪中看到类名,则将更加有趣。
— finnw

5
请不要ROLLBACK TO NSFW版本-作者明确接受了SFW版本的修订版3和6
— 亚当戴维斯


727
// drunk, fix later

希望我在开玩笑。而且知道认识了编写代码的开发人员之后,我认为他的意思是字面上的意思。


258
太可笑了,他们喝醉后会写评论。
— Jiminy

66
@Jiminy:认真!当我编写醉酒的代码时,我会醒来,但不幸的是,我听不懂那些华丽的代码。
— 2009年

22
喔...如果你不理解它,它有辉煌!
— 安德斯·汉森,

14
哈哈!!!我只是为了好玩而做:在本地的Subversion仓库中添加了“醉酒”,并发现了一个我不记得的提交评论:“以某种方式再次工作。嗯……我醉了!”
— ivan_ivanovich_ivanoff

14
哈哈,“ ..以某种方式介于0.129%和0.138%之间的BAC赋予超人编程能力...” xkcd.com/323
— Alpha Codemonkey,2010年

719
// Magic. Do not touch.

20
魔术=脆弱,也许?
— MrBoJangles


5
我不得不写很多次-主要是提醒自己:“如果您在不真正知道自己在做什么的情况下触摸以下代码,就会发生不好的事情!”
— Shalom Craimer 09年

6
@西蒙·霍华德(Simon Howard),如果它说“ More Magic”,我只会碰它
— DevinB

4
我仍然记得在高中时曾教过CS,那时我们的愚蠢问题都用“魔术!”来回答。和最险恶的微笑。
— nevets1219 2009年

701

#define TRUE FALSE //Happy debugging suckers


95
#define NULL(:: rand()%2)//也会很不错
— Viktor Sehr 2010年

33
#define if(if(!
— Bart van Heukelom

66
@Bart:您的代码中不能有一半括号,但是您可以这样做#define if(x) if(!(x))(或更危险的是#define if while)
— Chris Lutz 2010年

8
如给定的那样,它实际上会起作用:注释直到行尾字符确保使用此宏的可能行不会编译。例如:a = TRUE;将转换为a = FALSE // Happy debugging suckers;会导致编译错误,因为该术语;现在在注释中。如果使用块注释/* Eat this */,它将起作用。
— rix0rrr 2010年

6
#define struct union在内存受限的系统中非常有帮助。
— MSW 2010年


636
return 1; # returns 1

32
经典!但是我更喜欢这个:“ i ++; //将i加1”
— steffenj

71
谁知道他是否#DEFINE 1还有其他东西……
— icelava

13
我希望这样的评论对我来说是陌生的。
— 德鲁·多曼

6
我看了很多。我知道很多编码员会在函数中添加注释,然后在每个注释下方/旁边填充代码。编写大型业务功能非常容易,但是有时您会得到一些相当详细的注释。
— 哔哔声,

88
#返回一个什么?更加详细一些。
— 奥赞

591
/* This is O(scary), but seems quick enough in practice. */ 

随后是四个嵌套的for循环


33
pfft,最有可能只有N ^ 4,没有哪一个比4 ^ N或N差!
— 2008年

14
我曾经遇到过一个循环嵌套深度为8的情况。运行时间以小时为单位。
— 罗兰·佩希特尔

8
我有一个O(n)算法,其内部循环具有500ms网络RTT。n> 100k。哎哟。
— geofftnz

6
有一种赎回的品质:他们至少知道什么是O大术语。希望每个循环都在非常小的N上运行:-D
— Jon Smock

27
我与一个会说“ O(MFG),请”的人一起工作
— Jason Orendorff,2009年

589
// Replaces with spaces the braces in cases where braces in places cause stasis 
   $str = str_replace(array("\{","\}")," ",$str);

34
+1-天才。这应该穿一件T恤或其他东西:-)
— Rook

71
苏斯博士写代码吗?+1!
— Blorgbeard在

11
最后还需要两个音节...
— Michael Myers

88
刚打开评论说它还需要两个音节,但显然我被击败了三个月。
— 迈克尔·迈尔斯

15
我不同意需要额外的音节,因为它是完美的。五组,每组三个音节。
— KevDog,

542

它谈到我们的职业,当被问及“最佳评论”时,我们都会回答最糟糕的评论。


67
我完全同意您的评论,即很少需要评论。
— 哈波

16
取决于评论。甚至“自我记录”语言也可以从注释中受益,例如Python。当一个简单的句子告诉我时,我讨厌不得不遍历代码以弄清楚正在发生的事情。
— crystalattice

32
我不同意以下意见:评论是不必要的。有时,简单的“框架”注释可以理解整个代码。例如,如果您使用复杂的算法来逃避蛮力算法的时间复杂性,那么参考就必不可少。
— Mark Brittingham

83
代码说明了什么...注释说明了原因!
— 理查德·埃夫

3
我完全同意“理查德E”。评论旨在告诉您的未来*(同事,审阅者,您自己)为什么您选择以邪恶的方式而不是另类的方式进行工作。我并不是到处乱扔评论​​,但有时它们应该是强制性的。
— 丹妮塔


508
Catch (Exception e) {
 //who cares?
} 

11
这个让我像精神病一样大笑
— 平板电脑

68
我看到//前进的是同一件事,这里什么也看不到。
— Ferruccio's

6
我为这个笑得很辛苦。
— 弗兰克

9
必须承认,我已经多次这样做了,因为有时这无关紧要。
— NotMe

17
哇,我一直都在写那个评论。我从未认为这很有趣,我认为这是一个严肃的声明。
— 杰伊,

496
/**
 * Always returns true.
 */
public boolean isAvailable() {
    return false;
}

永远不要依赖评论...


85
我总是告诉我的同事们。“评论不起作用!”
— OscarRyz

15
我相信您的意思是“永远不要依赖代码”。我确定规格,设计和要求都需要返回码或true :)
— gbjbaanb 2009年

看起来就像您在failblog.org技术版上看到的东西
— BobC

5
这样的事情实际上发生了,即使功能正上方,人们也不必费心将文档保持最新。
— Henk

25
... #define false true?
— new123456 2010年

457
/*
 * You may think you know what the following code does.
 * But you dont. Trust me.
 * Fiddle with it, and youll spend many a sleepless
 * night cursing the moment you thought youd be clever
 * enough to "optimize" the code below.
 * Now close this file and go play with something else.
 */ 

32
让我想起了一个发誓他可以优化我几年前在一个AI项目中编写的机械定理证明算法的家伙。他确实提高了性能,但随后代码不起作用...他发誓是我的错。
— Mark Brittingham

嗯,我没在某个地方看到这个吗?
— bzlm

6
@Mark他只是说他会优化时间,他对功能一无所知= P
— DevinB

98
如果没有用,我可以将任何代码优化为零运行时间。
— Michael Borgwardt

9
这就是源代码管理的目的。浪费几个小时尝试对其进行优化,然后如果无法解决问题,请回滚。
— Kibbee

429
try {

} finally { // should never happen 

}

15
++。我公司的其他人一直在写这个!
— CVertex

17
我过去曾经看过该评论(甚至是自己写的),因为绝对不会发生相关的事情(但以防万一,这里有处理错误的代码)。最有趣的部分是,ti被放置在了finally块中,这意味着它应该总是发生……
— Troy Howard

32
是的,那是个玩笑。=)
— Erik Forbes

15
如果在try块中有一个System.exit()调用,则是,最终应该永远不会发生!
— 罗曼·林索拉斯

6
原因#984使用Debug.Assert()。;)
— Scott Hanselman

411

来自:https : //github.com/zepouet/Xee-xCode-4.5/blob/master/XeePhotoshopLoader.m#L108

// At this point, I'd like to take a moment to speak to you about the Adobe PSD
// format. PSD is not a good format. PSD is not even a bad format. Calling it
// such would be an insult to other bad formats, such as PCX or JPEG. No, PSD
// is an abysmal format. Having worked on this code for several weeks now, my
// hate for PSD has grown to a raging fire that burns with the fierce passion
// of a million suns.
//
// If there are two different ways of doing something, PSD will do both, in
// different places. It will then make up three more ways no sane human would
// think of, and do those too. PSD makes inconsistency an art form. Why, for
// instance, did it suddenly decide that *these* particular chunks should be
// aligned to four bytes, and that this alignement should *not* be included in
// the size? Other chunks in other places are either unaligned, or aligned with
// the alignment included in the size. Here, though, it is not included. Either
// one of these three behaviours would be fine. A sane format would pick one.
// PSD, of course, uses all three, and more.
//
// Trying to get data out of a PSD file is like trying to find something in the
// attic of your eccentric old uncle who died in a freak freshwater shark
// attack on his 58th birthday. That last detail may not be important for the
// purposes of the simile, but at this point I am spending a lot of time
// imagining amusing fates for the people responsible for this Rube Goldberg of
// a file format.
//
// Earlier, I tried to get a hold of the latest specs for the PSD file format.
// To do this, I had to apply to them for permission to apply to them to have
// them consider sending me this sacred tome. This would have involved faxing
// them a copy of some document or other, probably signed in blood. I can only
// imagine that they make this process so difficult because they are intensely
// ashamed of having created this abomination. I was naturally not gullible
// enough to go through with this procedure, but if I had done so, I would have
// printed out every single page of the spec, and set them all on fire. Were it
// within my power, I would gather every single copy of those specs, and launch
// them on a spaceship directly into the sun.
//
// PSD is not my favourite file format.

1
此后的一行及其评论确实是蛋糕的重点。理智吗?什么理智?格式全部吃完了
— Kim Reece

66
s / PSD / Internet Explorer / g。s /文件格式/网络浏览器/ g。现在,它符合我的想法。
— TJ L 2009年

10
我很遗憾程序员不得不经历这样的痛苦!!!但是很棒的评论。倾注了自己的心。
— Microkernel 2010年

4
这行代码:// PSD不是我最喜欢的文件格式。使这成为经典。
— joedevon 2010年

387
const int TEN=10; // As if the value of 10 will fluctuate... 

136
在今天的市场10现在只有9的价值
— 史蒂夫法洛斯

13
也许有一天我们会发展更多的手指……
— Loren Pechtel

59
那么非常大的10值呢?
— Mikeage,

184
const int TEN = 11; //我的音乐排到了11点
— 配音是2009年

18
从C调用Fortran-Fortran仅按引用进行调用,因此您需要所有常量的变量。
— Martin Beckett

364
    #Christmas tree initializer  
    toConnect = []  
    toRead =   [  ]  
    toWrite = [    ]   
    primes = [      ]  
    responses = {}  
    remaining = {}  

25
那是胜利。巨大的胜利……
— 飓风

16
好的,下一次我将使用这个,我需要四个空数组和两个哈希!
— 乔什

嗯 大声的“失败!” 致作者不知道他是否正在使用无格式语言。
— TheBlastOne'9

我怀疑他对无格式语言一无所知。那真是个好玩笑:-)
— 罗曼2010年


353

完全没有注释的2000行方法

{ 
  { 
    while (.. ){ 
      if (..){
          }
      for (.. ){ 
          }
         .... (just putting in the control flow here, imagine another few hundred ifs)
      if(..)   {
            if(..)     {
                   if(..)   {
                ...
                (another few hundred brackets)
                       }
                  }
         } //endif

(实际上,我有一天把所有括号都删掉了,只是为了看看情况有多糟,没有格式,得到的是:

{{{{}}{}{}{}{}}{{}{{}{}{}{}{}{}{{}{}}{}{}{{}{}{}{}{}{}{}{}{}{}{}{{}}}{{}{{}}{{{}}}{{}{}{}{}{}{}{}{{}}{}{{{}}{}{{}{}}{{{}}{}{}{}{}}{{}}}{}{{}{}{}{{}{{}}{}}{{}}}{{}}{{}}{{}}{}{{}}{{}}{{}}{{}{}{}}{}{}{{{}}{{}}}{}{}{}{}}{{{}{{}{}{}{{}{}{}{}{}{}}{}}{{}}{{}{}}}{{}}{{}}}{{}}{{}}{}{}{}{}{{}}{{}{}{}{}}}}{}{}}{{}{{{}{}{}{}}}}{{}{{{}}}}{{}{{{}{{}}{}{{}}{}{{}{}}{{}}{}{{}}}{{}}}}{{}{}{}{}{}{{{}    {{{{}}{}{}{}{}}{{}{{}{}{}{}{}{}{{}{}}{}{}{{}{}{}{}{}{}{}{}{}{}{}{{}}}{{}{{}}{{{}}}{{}{}{}{}{}{}{}{{}}{}{{{}}{}{{}{}}{{{}}{}{}{}{}}{{}}}{}{{}{}{}{{}{{}}{}}{{}}}{{}}{{}}{{}}{}{{}}{{}}{{}}{{}{}{}}{}{}{{{}}{{}}}{}{}{}{}}{{{}{{}{}{}{{}{}{}{}{}{}}{}}{{}}{{}{}}}{{}}{{}}}{{}}{{}}{}{}{}{}{{}}{{}{}{}{}}}}{}{}}{{}{{{}{}{}{}}}}{{}{{{}}}}{{}{{{}{{}}{}{{}}{}{{}{}}{{}}{}{{}}}{{}}}}{{}{}{}{}{}{{{}{}{{}}{}}}{}}{{}}{{}{}}{{}{{}{{}}}}{{{}{{{}}}}}{{{{{}}}}}{}{}{}{{{{}}}{}{}}{{}{{}}}}{}{{}}{}}}{}}{{}}{{}{}}{{}{{}{{}}}}{{{}{{{}}}}}{{{{{}}}}}{}{}{}{{{{}}}{}{}}{{}{{}}}}

Endif出现在800行附近)


180
游览-de-France的代码
— 安德烈亚斯·彼得森

7
这是值得的dailyworsethanfailure.com!
— 08年

67
看起来像Lisp!
— endian

8
该评论可能在那里修复了编译器错误。:|
— 罗伯·霍华德

49
OH SHIIIIIIIII的圈复杂度水平

353
long long ago; /* in a galaxy far far away */ 

1
这真的有效吗?用哪种语言?
— 毛里斯

16
@Mauris:它最初是64位整数的C和C ++扩展,许多供应商都支持。然后C99和C ++ 0x对其进行了标准化支持:en.wikipedia.org/wiki/Long_long
— Juliano

让我考虑一下我们在16位区域中使用的远近指针。:)
— Macke 2010年

1
它仍然可以正常工作。大约25年前,我们首先将其放入Convex的编译器中,因为DMR建议该名称应为我们的64位int的名称。
— tchrist


324
// If this comment is removed the program will blow up 

201
很久以前,我不小心通过添加注释修复了Java3D中的段错误。它是100%可复制的;如果我删除了评论,则会崩溃。只要有评论,它就可以正常工作。我认为这是一个奇怪的时间安排问题,但我从未弄清楚到底发生了什么。
— DNS

54
魔法/更多魔法
— DevinB,2009年

16
这有点像病毒。它包含旨在保护自己的内容。就像垃圾DNA一样,擅长复制自身。
— 查理·弗罗里斯

23
您应该比较两个版本生成的字节码
— JoelFan

13
我花了半天的时间才弄清楚,以C注释结尾\ (用于标记2D数组的方向)会导致编译器跳过下一行。
— Tgr 2010年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.