关闭

千万要避免的五种程序注释方式

发表于:2012-11-22 09:41

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:justjavac    来源:51Testing软件测试网采编

  你是否曾在检查代码时碰到一条在你看来多余的注释?在代码中使用注释的目的是提升代码的可读性,以让那些非原始代码开发者能更好地理解它们。

  我甄别出5类让我不胜其扰的注释及5类生成它们的程序员。我希望读过本篇之后,你不会与他们一样坠入同一条河流。作为一项挑战,你不妨把写这5类注释的程序员与5类程序员[英文]作一下匹配。

  1、骄傲型程序员

  1. public class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         string message = "Hello World!";  // 07/24/2010 Bob 
  6.         Console.WriteLine(message); // 07/24/2010 Bob 
  7.         message = "I am so proud of this code!"// 07/24/2010 Bob 
  8.         Console.WriteLine(message); // 07/24/2010 Bob 
  9.     }  
  10. }

  这类程序员对其代码自视甚高,以至于他觉得有必要在每行代码后都要签上自己的大名。应用版本控制系统(VCS)是能知道谁修改了代码,但是乍看之下责任人也不会如此打眼。

  2、过时型程序员

  1. public class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         /* This block of code is no longer needed 
  6.          * because we found out that Y2K was a hoax 
  7.          * and our systems did not roll over to 1/1/1900 */ 
  8.         //DateTime today = DateTime.Today; 
  9.         //if (today == new DateTime(1900 1 1)) 
  10.         //{ 
  11.         //    today = today.AddYears(100); 
  12.         //    string message = "The date has been fixed for Y2K."; 
  13.         //    Console.WriteLine(message); 
  14.         //} 
  15.     }  
  16. }

  如果一段代码不再使用了(也就是过时了),请删除它——勿要让你的工作代码被数行冗余的注释弄得七零八乱。而且,你任何时候需要复制这段删除的代码,都可以使用版本控制系统,这样你便能从以前版本中恢复出它来。

  3、显然型程序员

  1. public class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         /* This is a for loop that prints the  
  6.          * words "I Rule!" to the console screen  
  7.          * 1 million times each on its own line. It 
  8.          * accomplishes this by starting at 0 and  
  9.          * incrementing by 1. If the value of the  
  10.          * counter equals 1 million the for loop 
  11.          * stops executing.*/ 
  12.         for (int i = 0; i < 1000000; i++)  
  13.         {  
  14.             Console.WriteLine("I Rule!");  
  15.         }  
  16.     }  
  17. }

  我们都知道编程的基本工作逻辑——这可不是什么“编程入门”!你无需浪费时间解释显而易见的程序工作原理,虽然我们很高兴看到你愿意解释代码的功能——但这不过是画蛇添足。

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号