自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

千萬(wàn)要避免的五種程序注釋方式

開(kāi)發(fā) 開(kāi)發(fā)工具 后端
你是否曾在檢查代碼時(shí)碰到一條在你看來(lái)多余的注釋?zhuān)吭诖a中使用注釋的目的是提升代碼的可讀性,以讓那些非原始代碼開(kāi)發(fā)者能更好地理解它們。

你是否曾在檢查代碼時(shí)碰到一條在你看來(lái)多余的注釋?zhuān)吭诖a中使用注釋的目的是提升代碼的可讀性,以讓那些非原始代碼開(kāi)發(fā)者能更好地理解它們。

我甄別出5類(lèi)讓我不勝其擾的注釋及5類(lèi)生成它們的程序員。我希望讀過(guò)本篇之后,你不會(huì)與他們一樣墜入同一條河流。作為一項(xiàng)挑戰(zhàn),你不妨把寫(xiě)這5類(lèi)注釋的程序員與5類(lèi)程序員[英文]作一下匹配。

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.     }  

這類(lèi)程序員對(duì)其代碼自視甚高,以至于他覺(jué)得有必要在每行代碼后都要簽上自己的大名。應(yīng)用版本控制系統(tǒng)(VCS)是能知道誰(shuí)修改了代碼,但是乍看之下責(zé)任人也不會(huì)如此打眼。

2. 過(guò)時(shí)型程序員

  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.     }  

如果一段代碼不再使用了(也就是過(guò)時(shí)了),請(qǐng)刪除它——勿要讓你的工作代碼被數(shù)行冗余的注釋弄得七零八亂。而且,你任何時(shí)候需要復(fù)制這段刪除的代碼,都可以使用版本控制系統(tǒng),這樣你便能從以前版本中恢復(fù)出它來(lái)。

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.     }  

我們都知道編程的基本工作邏輯——這可不是什么“編程入門(mén)”!你無(wú)需浪費(fèi)時(shí)間解釋顯而易見(jiàn)的程序工作原理,雖然我們很高興看到你愿意解釋代碼的功能——但這不過(guò)是畫(huà)蛇添足。

4. 傳記型程序員

  1. public class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.        /* I discussed with Jim from Sales over coffee   
  6.         * at the Starbucks on main street one day and he  
  7.         * told me that Sales Reps receive commission   
  8.         * based upon the following structure.   
  9.         * Friday: 25%  
  10.         * Wednesday: 15%  
  11.         * All Other Days: 5%  
  12.         * Did I mention that I ordered the Caramel Latte with  
  13.         * a double shot of Espresso?   
  14.        */ 
  15.         double price = 5.00;  
  16.         double commissionRate;  
  17.         double commission;  
  18.         if (DateTime.Today.DayOfWeek == DayOfWeek.Friday)  
  19.         {  
  20.             commissionRate = .25;  
  21.         }  
  22.         else if (DateTime.Today.DayOfWeek == DayOfWeek.Wednesday)  
  23.         {  
  24.             commissionRate = .15;  
  25.         }  
  26.         else 
  27.         {  
  28.             commissionRate = .05;  
  29.         }  
  30.         commission = price * commissionRate;  
  31.     }  

如果你非得在代碼中提到某些必需的東西,也別提到人名。Jim from Sales(譯注:銷(xiāo)售人員Jim)也許離開(kāi)這家公司了,那些閱讀代碼的程序員極可能根本就不知道他是誰(shuí),更甭提注釋里那些毫無(wú)干系的事情。

5. “總有一天”型程序員

  1. public class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.        //TODO: I need to fix this someday – 07/24/1995 Bob  
  6.        /* I know this error message is hard coded and  
  7.         * I am relying on a Contains function but   
  8.         * someday I will make this code print a   
  9.         * meaningful error message and exit gracefully.  
  10.         * I just don’t have the time right now.  
  11.        */ 
  12.        string message = "An error has occurred";  
  13.        if(message.Contains("error"))  
  14.        {  
  15.            throw new Exception(message);  
  16.        }  
  17.     }  

這類(lèi)注釋在某種程度上說(shuō)是前面幾種類(lèi)型的大雜燴。TODO注釋在項(xiàng)目初始開(kāi)發(fā)階段用處不小,但是如果幾年后出現(xiàn)在產(chǎn)品代碼中——那就會(huì)帶來(lái)麻煩。如果有什么需要修補(bǔ)的,趁現(xiàn)在動(dòng)手,而不要推遲到以后去做。

如果你不幸是生成這些類(lèi)型注釋的人,或者你想學(xué)習(xí)注釋用法的***實(shí)踐,我推薦你閱讀Steve McConnell寫(xiě)的Code Complete(《代碼大全》)。這是一本我建議程序員必讀的書(shū)籍,OSC地址 http://my.oschina.net/justjavac/blog/66624

你是否在自己的代碼中看到了其它類(lèi)型多余或擾人的注釋?zhuān)空?qǐng)不吝分享。

原文鏈接:http://www.oschina.net/question/253614_79956

責(zé)任編輯:林師授 來(lái)源: OSCHINA
相關(guān)推薦

2015-08-20 09:06:48

程序員

2013-07-16 10:49:11

代碼注釋

2013-07-17 17:21:49

避免代碼注釋移動(dòng)開(kāi)發(fā)移動(dòng)互聯(lián)網(wǎng)

2019-03-27 08:27:32

物聯(lián)網(wǎng)IOT技術(shù)

2022-12-29 08:46:15

IT采購(gòu)投資

2022-12-07 11:24:51

首席信息官IT

2021-12-02 18:07:53

云網(wǎng)絡(luò)部署云端云計(jì)算

2022-01-10 06:52:59

查詢(xún)MySQL字段

2010-08-27 09:10:15

網(wǎng)絡(luò)隱私

2011-02-28 13:51:30

Spring事物配置

2009-06-19 18:26:38

Spring事務(wù)配置

2011-11-25 10:25:27

SpringJava

2021-06-28 10:12:34

云計(jì)算云平臺(tái)云計(jì)算架構(gòu)

2016-05-25 10:03:51

JavaScript內(nèi)存泄露

2014-02-19 11:12:04

WAN優(yōu)化WAN

2018-09-10 15:58:49

2010-08-13 13:25:53

Flex頁(yè)面跳轉(zhuǎn)

2022-12-27 14:21:42

VR

2022-01-13 19:25:28

服務(wù)方式TCP

2023-07-25 10:45:48

OHScrcpy鴻蒙
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)