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

程序員應(yīng)該避免的5種代碼注釋

開發(fā) 后端 開發(fā)工具
我特別討厭這5種注釋類型以及制造它們的程序員。希望你不是其中之一。

 

 

你有沒有這樣的經(jīng)歷:別人審查過你的代碼之后給出的注釋,你認為是沒有必要的?注釋代碼是為了提高代碼的可讀性,目的是為了能讓其他人更容易理解你的代碼。

我特別討厭這5種注釋類型以及制造它們的程序員。希望你不是其中之一。

1.自以為很了不得的程序員

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

[[146030]]

這個程序員自認為寫了一段很了不得的代碼,所以覺得有必要用自己的名字對每行代碼進行標記。實施版本控制系統(tǒng)(VCS)能實現(xiàn)對代碼變更的問責(zé),但是也不會這么明顯知道誰應(yīng)對此負責(zé)。

2.過時的程序員

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

[[146031]]

如果一段代碼已不再使用(即過時),那就刪除它——不要浪費時間給這些代碼寫注釋。此外,如果你需要復(fù)制這段被刪除的代碼,別忘了還有版本控制系統(tǒng),你完全可以從早期的版本中恢復(fù)代碼。

3.多此一舉的程序員

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

[[146032]]

我們都知道基礎(chǔ)的編程邏輯是如何工作的——所以你不需要多此一舉來解釋這些顯而易見的工作原理,雖然說你解釋得很happy,但這只是在浪費時間和空間。

4.愛講故事的程序員

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

[[146033]]

如果你一定要在注釋里提及需求,那么不要涉及別人的名字。銷售部門的Jim可能會離開公司,而且很有可能大多數(shù)程序員根本不知道這是何許人也。不要在注釋里提及不相干的事實。

5.“以后再做”的程序員

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

[[146034]]

這種類型的注釋包含了上面所有其他類型。如果是在項目的初始開發(fā)階段,這種待做注釋是非常有用的,但如果是在幾年后的產(chǎn)品代碼——那就會出問題了。如果有什么需要修復(fù)的,立馬解決,不要把它擱置一邊,“以后再做”。

如果你也常常犯這樣的注釋錯誤,如果你想了解注釋的***做法,我建議你閱讀類似于Steve McConnell寫的《Code Complete》這樣的好書。

責(zé)任編輯:王雪燕 來源: 碼農(nóng)網(wǎng)
相關(guān)推薦

2015-10-20 15:59:57

注釋代碼程序

2015-10-26 09:38:52

避免注釋代碼

2017-11-20 22:28:43

程序員源代碼編程

2009-12-28 09:42:14

程序員

2012-01-10 14:43:48

程序員

2012-11-20 10:20:03

程序員程序注釋編程注釋

2010-08-13 10:00:19

程序員注釋

2023-01-12 12:53:00

程序員離職違法

2015-07-02 11:20:17

程序員代碼

2011-12-19 09:40:21

程序員

2016-06-03 15:18:45

程序員

2009-03-13 15:18:45

程序員飲食雜談

2014-11-10 09:46:57

程序員

2017-12-19 20:35:22

程序員中興事件自殺

2016-04-11 17:49:33

程序員外包

2013-04-01 15:51:09

程序員管理

2013-04-28 11:03:41

編程語言程序員私有云

2020-12-07 10:19:01

程序員技術(shù)IT

2015-08-11 09:20:51

初級程序員Linux命令

2021-03-02 15:31:37

程序員技能開發(fā)者
點贊
收藏

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