小鎮樹妖--住在樹上的妖

          To follow the path: look to the master, follow the master, walk with the master, see through the master, become the master.

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            10 Posts :: 50 Stories :: 7 Comments :: 0 Trackbacks

          撰文:DotNetSpider.comhttp://www.dotnetspider.com

          翻譯:楊賀宏

          誰都會寫代碼!幾個月的編程經驗可以讓你寫出“可運行應用程序”。讓它可運行容易,但是以最有效率的方式編碼就需要下更多的功夫!

          要知道,大多數程序員在寫”可運行代碼,“而不是”高效代碼“。我們在這個指南課程前面提到,你想成為你們公司”最尊貴的專業人員“嗎?寫”高效代碼“是一項藝術,你必須學習和實踐它。

          命名慣例和規范



          注記 :
          Pascal 大小寫形式-所有單詞第一個字母大寫,其他字母小寫。
          Camel   大小寫形式-除了第一個單詞,所有單詞第一個字母大寫,其他字母小寫。

        1. 類名使用Pascal 大小寫形式
          public class HelloWorld{ ...}

        2. 方法使用Pascal 大小寫形式
          public class HelloWorld{ void SayHello(string name) {  ... }}

        3. 變量和方法參數使用Camel 大小寫形式


          public class HelloWorld{ int totalCount = 0; void SayHello(string name) {  string fullMessage = "Hello " + name;  ... }}  
        4. 不要使用匈牙利方法來命名變量

          以前,多數程序員喜歡它-把數據類型作為變量名的前綴而m_作為成員變量的前綴。例如:
          string m_sName;int nAge;    
          然而,這種方式在.NET編碼規范中是不推薦的。所有變量都用camel 大小寫形式,而不是用數據類型和m_來作前綴。
        5. 用有意義的,描述性的詞語來命名變量

          - 別用縮寫。用name, address, salary等代替 nam, addr, sal
          - 別使用單個字母的變量象i, n, x 等. 使用 index, temp
          用于循環迭代的變量例外:
          for ( int i = 0; i < count; i++ ){ ...}
          如果變量只用于迭代計數,沒有在循環的其他地方出現,許多人還是喜歡用單個字母的變量(i) ,而不是另外取名。
          - 變量名中不使用下劃線 (_) 。
          - 命名空間需按照標準的模式命名
          ...

        6. 文件名要和類名匹配

          例如,對于類HelloWorld, 相應的文件名應為 helloworld.cs (或, helloworld.vb)

          縮進和間隔

        7. 縮進用 TAB . 不用 SPACES.。
        8. 注釋需和代碼對齊.。
        9. 花括弧 ( {} ) 需和括號外的代碼對齊.。
        10. 用一個空行來分開代碼的邏輯分組。.
           bool SayHello (string name) {  string fullMessage = "Hello " + name;  DateTime currentTime = DateTime.Now;  string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();  MessageBox.Show ( message );  if ( ... )  {   // Do something   // ...   return false;  }  return true; }              
          這段代碼看起來比上面的好::
           bool SayHello ( string name ) {  string fullMessage = "Hello " + name;  DateTime currentTime = DateTime.Now;  
          string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
          MessageBox.Show ( message );
          if ( ... ) { // Do something // ...
          return false; }
          return true; }
        11. 在一個類中,各個方法需用一空行,也只能是一行分開。
        12. 花括弧需獨立一行,而不象if, for 等可以跟括號在同一行。.
          好:
            if ( ... )   {   // Do something  }
          不好:
            if ( ... ) {   // Do something  }
        13. 在每個運算符和括號的前后都空一格。.

          好:
            if ( showResult == true )  {   for ( int i = 0; i < 10; i++ )   {    //   }  }
          不好:
            if(showResult==true)  {   for(int i= 0;i<10;i++)   {    //   }  }

          良好的編程習慣


          遵從以下良好的習慣以寫出好程序

        14. 避免使用大文件。如果一個文件里的代碼超過300~400行,必須考慮將代碼分開到不同類中。
        15. 避免寫太長的方法。一個典型的方法代碼在1~25行之間。如果一個方法發代碼超過25行,應該考慮將其分解為不同的方法。
        16. 方法名需能看出它作什么。別使用會引起誤解的名字。如果名字一目了然,就無需用文檔來解釋方法的功能了。

          好:
           void SavePhoneNumber ( string phoneNumber ) {  // Save the phone number. }

          不好:
           // This method will save the phone number. void SaveData ( string phoneNumber ) {  // Save the phone number. }
        17. 一個方法只完成一個任務。不要把多個任務組合到一個方法中,即使那些任務非常小。

          好:
           // Save the address. SaveAddress (  address );  // Send an email to the supervisor to inform that the address is updated. SendEmail ( address, email );    void SaveAddress ( string address ) {  // Save the address.  // ... }  void SendEmail ( string address, string email ) {  // Send an email to inform the supervisor that the address is changed.  // ... }

          不好:
           // Save address and send an email to the supervisor to inform that the address is updated. SaveAddress ( address, email ); void SaveAddress ( string address, string email ) {  // Job 1.  // Save the address.  // ...  // Job 2.  // Send an email to inform the supervisor that the address is changed.  // ... }
        18. 使用C# 或 VB.NET的特有類型,而不是System命名空間中定義的別名類型。

          好:
           int age; string name; object contactInfo;

          不好:
           Int16 age; String name; Object contactInfo; 
        19. 別在程序中使用固定數值,用常量代替。
        20. 別用字符串常數。用資源文件。
        21. 避免使用很多成員變量。聲明局部變量,并傳遞給方法。不要在方法間共享成員變量。如果在幾個方法間共享一個成員變量,那就很難知道是哪個方法在什么時候修改了它的值。
        22. 必要時使用enum 。別用數字或字符串來指示離散值。
          好:
           enum MailType {  Html,  PlainText,  Attachment } void SendMail (string message, MailType mailType) {  switch ( mailType )  {   case MailType.Html:    // Do something    break;   case MailType.PlainText:    // Do something    break;   case MailType.Attachment:    // Do something    break;   default:    // Do something    break;  } }            


          不好:
           void SendMail (string message, string mailType) {  switch ( mailType )  {   case "Html":    // Do something    break;   case "PlainText":    // Do something    break;   case "Attachment":    // Do something    break;   default:    // Do something    break;  } }
        23. 別把成員變量聲明為 public 或 protected。都聲明為 private 而使用 public/protected 的Properties.
        24. 不在代碼中使用具體的路徑和驅動器名。 使用相對路徑,并使路徑可編程。
        25. 永遠別設想你的代碼是在“C:”盤運行。你不會知道,一些用戶在網絡或“Z:”盤運行程序。
        26. 應用程序啟動時作些“自檢”并確保所需文件和附件在指定的位置。必要時檢查數據庫連接。出現任何問題給用戶一個友好的提示。
        27. 如果需要的配置文件找不到,應用程序需能自己創建使用默認值的一份。
        28. 如果在配置文件中發現錯誤值,應用程序要拋出錯誤,給出提示消息告訴用戶正確值。
        29. 不要使用匈牙利方法來命名變量

          以前,多數程序員喜歡它-把數據類型作為變量名的前綴而m_作為成員變量的前綴。例如:
          string m_sName;int nAge;    
          然而,這種方式在.NET編碼規范中是不推薦的。所有變量都用camel 大小寫形式,而不是用數據類型和m_來作前綴。
        30. 用有意義的,描述性的詞語來命名變量

          - 別用縮寫。用name, address, salary等代替 nam, addr, sal
          - 別使用單個字母的變量象i, n, x 等. 使用 index, temp
          用于循環迭代的變量例外:
          for ( int i = 0; i < count; i++ ){ ...}
          如果變量只用于迭代計數,沒有在循環的其他地方出現,許多人還是喜歡用單個字母的變量(i) ,而不是另外取名。
          - 變量名中不使用下劃線 (_) 。
          - 命名空間需按照標準的模式命名
          ...

        31. 文件名要和類名匹配

          例如,對于類HelloWorld, 相應的文件名應為 helloworld.cs (或, helloworld.vb)

          縮進和間隔

        32. 縮進用 TAB . 不用 SPACES.。
        33. 注釋需和代碼對齊.。
        34. 花括弧 ( {} ) 需和括號外的代碼對齊.。
        35. 用一個空行來分開代碼的邏輯分組。.
           bool SayHello (string name) {  string fullMessage = "Hello " + name;  DateTime currentTime = DateTime.Now;  string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();  MessageBox.Show ( message );  if ( ... )  {   // Do something   // ...   return false;  }  return true; }              
          這段代碼看起來比上面的好::
           bool SayHello ( string name ) {  string fullMessage = "Hello " + name;  DateTime currentTime = DateTime.Now;  
          string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
          MessageBox.Show ( message );
          if ( ... ) { // Do something // ...
          return false; }
          return true; }
        36. 在一個類中,各個方法需用一空行,也只能是一行分開。
        37. 花括弧需獨立一行,而不象if, for 等可以跟括號在同一行。.
          好:
            if ( ... )   {   // Do something  }
          不好:
            if ( ... ) {   // Do something  }
        38. 在每個運算符和括號的前后都空一格。.

          好:
            if ( showResult == true )  {   for ( int i = 0; i < 10; i++ )   {    //   }  }
          不好:
            if(showResult==true)  {   for(int i= 0;i<10;i++)   {    //   }  }

          良好的編程習慣


          遵從以下良好的習慣以寫出好程序

        39. 避免使用大文件。如果一個文件里的代碼超過300~400行,必須考慮將代碼分開到不同類中。
        40. 避免寫太長的方法。一個典型的方法代碼在1~25行之間。如果一個方法發代碼超過25行,應該考慮將其分解為不同的方法。
        41. 方法名需能看出它作什么。別使用會引起誤解的名字。如果名字一目了然,就無需用文檔來解釋方法的功能了。

          好:
           void SavePhoneNumber ( string phoneNumber ) {  // Save the phone number. }

          不好:
           // This method will save the phone number. void SaveData ( string phoneNumber ) {  // Save the phone number. }
        42. 一個方法只完成一個任務。不要把多個任務組合到一個方法中,即使那些任務非常小。

          好:
           // Save the address. SaveAddress (  address );  // Send an email to the supervisor to inform that the address is updated. SendEmail ( address, email );    void SaveAddress ( string address ) {  // Save the address.  // ... }  void SendEmail ( string address, string email ) {  // Send an email to inform the supervisor that the address is changed.  // ... }

          不好:
           // Save address and send an email to the supervisor to inform that the address is updated. SaveAddress ( address, email ); void SaveAddress ( string address, string email ) {  // Job 1.  // Save the address.  // ...  // Job 2.  // Send an email to inform the supervisor that the address is changed.  // ... }
        43. 使用C# 或 VB.NET的特有類型,而不是System命名空間中定義的別名類型。

          好:
           int age; string name; object contactInfo;

          不好:
           Int16 age; String name; Object contactInfo; 
        44. 別在程序中使用固定數值,用常量代替。
        45. 別用字符串常數。用資源文件。
        46. 避免使用很多成員變量。聲明局部變量,并傳遞給方法。不要在方法間共享成員變量。如果在幾個方法間共享一個成員變量,那就很難知道是哪個方法在什么時候修改了它的值。
        47. 必要時使用enum 。別用數字或字符串來指示離散值。
          好:
           enum MailType {  Html,  PlainText,  Attachment } void SendMail (string message, MailType mailType) {  switch ( mailType )  {   case MailType.Html:    // Do something    break;   case MailType.PlainText:    // Do something    break;   case MailType.Attachment:    // Do something    break;   default:    // Do something    break;  } }            


          不好:
           void SendMail (string message, string mailType) {  switch ( mailType )  {   case "Html":    // Do something    break;   case "PlainText":    // Do something    break;   case "Attachment":    // Do something    break;   default:    // Do something    break;  } }
        48. 別把成員變量聲明為 public 或 protected。都聲明為 private 而使用 public/protected 的Properties.
        49. 不在代碼中使用具體的路徑和驅動器名。 使用相對路徑,并使路徑可編程。
        50. 永遠別設想你的代碼是在“C:”盤運行。你不會知道,一些用戶在網絡或“Z:”盤運行程序。
        51. 應用程序啟動時作些“自檢”并確保所需文件和附件在指定的位置。必要時檢查數據庫連接。出現任何問題給用戶一個友好的提示。
        52. 如果需要的配置文件找不到,應用程序需能自己創建使用默認值的一份。
        53. 如果在配置文件中發現錯誤值,應用程序要拋出錯誤,給出提示消息告訴用戶正確值。
        54. 錯誤消息需能幫助用戶解決問題。永遠別用象"應用程序出錯", "發現一個錯誤" 等錯誤消息。而應給出象 "更新數據庫失敗。請確保登陸id和密碼正確。" 的具體消息。  
        55. 顯示錯誤消息時,除了說哪里錯了,還應提示用戶如何解決問題。不要用 象 "更新數據庫失敗。"這樣的,要提示用戶怎么做:"更新數據庫失敗。請確保登陸id和密碼正確。"
        56. 顯示給用戶的消息要簡短而友好。但要把所有可能的信息都記錄下來,以助診斷問題。

          注釋

        57. 別每行代碼,每個聲明的變量都做注釋。
        58. 需要的地方注釋。可讀性強的代碼需要很少的注釋。如果所有的變量和方法的命名都很有意義,會使代碼可讀性很強并無需太多注釋。
        59. 行數不多的注釋會使代碼看起來優雅。但如果代碼不清晰,可讀性差,那就糟糕。
        60. 如果應為某種原因使用了復雜艱澀的原理,為程序配備良好的文檔和重分的注釋。
        61. 對一個數值變量采用不是0,-1等的數值初始化,給出選擇該值的理由。
        62. 簡言之,要寫清晰,可讀的代碼以致無須什么注釋就能理解。
        63. 對注釋做拼寫檢查,保證語法和標點符號的正確使用。

          異常處理

        64. 不要“捕捉了異常卻什么也不做“。如果隱藏了一個異常,你將永遠不知道異常到底發生了沒有。
        65. 發生異常時,給出友好的消息給用戶,但要精確記錄錯誤的所有可能細節,包括發生的時間,和相關方法,類名等。
        66. 只捕捉特定的異常,而不是一般的異常。

          好:
           void ReadFromFile ( string fileName ) {  try  {   // read from file.  }  catch (FileIOException ex)  {   // log error.   //  re-throw exception depending on your case.   throw;  } }
          不好:
           void ReadFromFile ( string fileName ) {  try  {   // read from file.  }  catch (Exception ex)   {   // Catching general exception is bad... we will never know whether it   // was a file error or some other error.      // Here you are hiding an exception.    // In this case no one will ever know that an exception happened.   return "";    } }
        67. 不必在所有方法中捕捉一般異常。不管它,讓程序崩潰。這將幫助你在開發周期發現大多數的錯誤。
        68. 你可以用應用程序級(線程級)錯誤處理器處理所有一般的異常。遇到”以外的一般性錯誤“時,此錯誤處理器應該捕捉異常,給用戶提示消息,在應用程序關閉或 用戶選擇”忽略并繼續“之前記錄錯誤信息。
        69. 不必每個方法都用try-catch。當特定的異常可能發生時才使用。比如,當你寫文件時,處理異常FileIOException.
        70. 別寫太大的 try-catch 模塊。如果需要,為每個執行的任務編寫單獨的 try-catch 模塊。 這將幫你找出哪一段代碼產生異常,并給用戶發出特定的錯誤消息
        71. 顯示給用戶的消息要簡短而友好。但要把所有可能的信息都記錄下來,以助診斷問題。

          注釋

        72. 別每行代碼,每個聲明的變量都做注釋。
        73. 需要的地方注釋。可讀性強的代碼需要很少的注釋。如果所有的變量和方法的命名都很有意義,會使代碼可讀性很強并無需太多注釋。
        74. 行數不多的注釋會使代碼看起來優雅。但如果代碼不清晰,可讀性差,那就糟糕。
        75. 如果應為某種原因使用了復雜艱澀的原理,為程序配備良好的文檔和重分的注釋。
        76. 對一個數值變量采用不是0,-1等的數值初始化,給出選擇該值的理由。
        77. 簡言之,要寫清晰,可讀的代碼以致無須什么注釋就能理解。
        78. 對注釋做拼寫檢查,保證語法和標點符號的正確使用。

          異常處理

        79. 不要“捕捉了異常卻什么也不做“。如果隱藏了一個異常,你將永遠不知道異常到底發生了沒有。
        80. 發生異常時,給出友好的消息給用戶,但要精確記錄錯誤的所有可能細節,包括發生的時間,和相關方法,類名等。
        81. 只捕捉特定的異常,而不是一般的異常。

          好:
           void ReadFromFile ( string fileName ) {  try  {   // read from file.  }  catch (FileIOException ex)  {   // log error.   //  re-throw exception depending on your case.   throw;  } }
          不好:
           void ReadFromFile ( string fileName ) {  try  {   // read from file.  }  catch (Exception ex)   {   // Catching general exception is bad... we will never know whether it   // was a file error or some other error.      // Here you are hiding an exception.    // In this case no one will ever know that an exception happened.   return "";    } }
        82. 不必在所有方法中捕捉一般異常。不管它,讓程序崩潰。這將幫助你在開發周期發現大多數的錯誤。
        83. 你可以用應用程序級(線程級)錯誤處理器處理所有一般的異常。遇到”以外的一般性錯誤“時,此錯誤處理器應該捕捉異常,給用戶提示消息,在應用程序關閉或 用戶選擇”忽略并繼續“之前記錄錯誤信息。
        84. 不必每個方法都用try-catch。當特定的異常可能發生時才使用。比如,當你寫文件時,處理異常FileIOException.
        85. 別寫太大的 try-catch 模塊。如果需要,為每個執行的任務編寫單獨的 try-catch 模塊。 這將幫你找出哪一段代碼產生異常,并給用戶發出特定的錯誤消息
        86. 如果應用程序需要,可以編寫自己的異常類。自定義異常不應從基類SystemException派生,而要繼承于. IApplicationException
        87. posted on 2006-01-15 09:20 jacky wu 閱讀(110) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 喀什市| 衡水市| 阜康市| 漳浦县| 密云县| 南汇区| 明光市| 威宁| 从江县| 上犹县| 昌邑市| 交口县| 邢台县| 昌都县| 盱眙县| 阜城县| 岳阳市| 伊金霍洛旗| 修水县| 岚皋县| 固始县| 互助| 江山市| 天柱县| 措勤县| 巴彦淖尔市| 平南县| 广南县| 喀什市| 临洮县| 定兴县| 呼玛县| 石景山区| 瑞安市| 东乌珠穆沁旗| 磐安县| 府谷县| 梁河县| 宜黄县| 鲜城| 门源|