溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

web程序員應(yīng)該避免的五種代碼注釋是什么

發(fā)布時間:2022-01-06 21:57:48 來源:億速云 閱讀:84 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“web程序員應(yīng)該避免的五種代碼注釋是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“web程序員應(yīng)該避免的五種代碼注釋是什么”吧!

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

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

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

2.過時的程序員

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

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

3.多此一舉的程序員

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

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

4.愛講故事的程序員

public class Program {     static void Main(string[] args)     {        /* I discussed with Jim from Sales over coffee         * at the Starbucks on main street one day and he         * told me that Sales Reps receive commission         * based upon the following structure.         * Friday: 25%         * Wednesday: 15%         * All Other Days: 5%         * Did I mention that I ordered the Caramel Latte with         * a double shot of Espresso?        */         double price = 5.00;         double commissionRate;         double commission;         if (DateTime.Today.DayOfWeek == DayOfWeek.Friday)         {             commissionRate = .25;         }         else if (DateTime.Today.DayOfWeek == DayOfWeek.Wednesday)         {             commissionRate = .15;         }         else         {             commissionRate = .05;         }         commission = price * commissionRate;     } }

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

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

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

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

到此,相信大家對“web程序員應(yīng)該避免的五種代碼注釋是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

web
AI