你有没有这样的经历:别人审查过你的代码之后给出的注释,你认为是没有必要的?注释代码是为了提高代码的可读性,目的是为了能让其他人更容易理解你的代码。
我特别讨厌这5种注释类型以及制造它们的程序员。希望你不是其中之一。

<a target="_blank"></a>
<code>public class program</code>
<code>{</code>
<code>static void main(string[] args)</code>
<code>string message = "hello world!"; // 07/24/2010 bob</code>
<code>console.writeline(message); // 07/24/2010 bob</code>
<code>message = "i am so proud of this code!"; // 07/24/2010 bob</code>
<code>}</code>
这个程序员自认为写了一段很了不得的代码,所以觉得有必要用自己的名字对每行代码进行标记。实施版本控制系统(vcs)能实现对代码变更的问责,但是也不会这么明显知道谁应对此负责。
<code>/* this block of code is no longer needed</code>
<code>* because we found out that y2k was a hoax</code>
<code>* and our systems did not roll over to 1/1/1900 */</code>
<code>//datetime today = datetime.today;</code>
<code>//if (today == new datetime(1900, 1, 1))</code>
<code>//{</code>
<code>// today = today.addyears(100);</code>
<code>// string message = "the date has been fixed for y2k.";</code>
<code>// console.writeline(message);</code>
<code>//}</code>
如果一段代码已不再使用(即过时),那就删除它——不要浪费时间给这些代码写注释。此外,如果你需要复制这段被删除的代码,别忘了还有版本控制系统,你完全可以从早期的版本中恢复代码。
<code>/* this is a for loop that prints the</code>
<code>* words "i rule!" to the console screen</code>
<code>* 1 million times, each on its own line. it</code>
<code>* accomplishes this by starting at 0 and</code>
<code>* incrementing by 1. if the value of the</code>
<code>* counter equals 1 million the for loop</code>
<code>* stops executing.*/</code>
<code>for (int i = 0; i < 1000000; i++)</code>
<code>console.writeline("i rule!");</code>
我们都知道基础的编程逻辑是如何工作的——所以你不需要多此一举来解释这些显而易见的工作原理,虽然说你解释得很happy,但这只是在浪费时间和空间。
<code>/* i discussed with jim from sales over coffee</code>
<code>* at the starbucks on main street one day and he</code>
<code>* told me that sales reps receive commission</code>
<code>* based upon the following structure.</code>
<code>* friday: 25%</code>
<code>* wednesday: 15%</code>
<code>* all other days: 5%</code>
<code>* did i mention that i ordered the caramel latte with</code>
<code>* a double shot of espresso?</code>
<code>*/</code>
<code>double price = 5.00;</code>
<code>double commissionrate;</code>
<code>double commission;</code>
<code>if (datetime.today.dayofweek == dayofweek.friday)</code>
<code>commissionrate = .25;</code>
<code>else if (datetime.today.dayofweek == dayofweek.wednesday)</code>
<code>commissionrate = .15;</code>
<code>else</code>
<code>commissionrate = .05;</code>
<code>commission = price * commissionrate;</code>
如果你一定要在注释里提及需求,那么不要涉及别人的名字。销售部门的jim可能会离开公司,而且很有可能大多数程序员根本不知道这是何许人也。不要在注释里提及不相干的事实。
<code>//todo: i need to fix this someday - 07/24/1995 bob</code>
<code>/* i know this error message is hard coded and</code>
<code>* i am relying on a contains function, but</code>
<code>* someday i will make this code print a</code>
<code>* meaningful error message and exit gracefully.</code>
<code>* i just don't have the time right now.</code>
<code>string message = "an error has occurred";</code>
<code>if(message.contains("error"))</code>
<code>throw new exception(message);</code>
这种类型的注释包含了上面所有其他类型。如果是在项目的初始开发阶段,这种待做注释是非常有用的,但如果是在几年后的产品代码——那就会出问题了。如果有什么需要修复的,立马解决,不要把它搁置一边,“以后再做”。
如果你也常常犯这样的注释错误,如果你想了解注释的最佳做法,我建议你阅读类似于steve mcconnell写的《code complete》这样的好书。本文来自云栖
社区合作伙伴“linux中国”,原文发布日期:2015-10-02