「程序員、程序員」顧名思義就是來編程序的人員。他們和通常工做人員是同樣的,都須要合做,可能爲了一個大型項目程序會有十人以上或者百人以上甚至千人以上的團隊公司在一塊兒工做。編碼規範使程序規範化,易懂化,才能更好的進行合做。編程
開發程序的軟件不少。可是它們的檢查方式全是檢查語法,並無規定變量命名以及方法的命名,因此註釋是很必要的東西,不過若是你將變量命名的規範一些。 Java和C#裏的命名是最接近天然語言的 缺點是名字太長,你不喜歡你的老闆,可是你得從老闆手裏賺錢,這就是道理。api
喜歡是一回事,賺錢是另一回事,找既喜歡,又賺錢的事情作,太難了。命名實際上是越長越好的,由於表意性很好,你們看了像看文章同樣,一目瞭然。這樣纔會使得別人更加明白清晰的看清你寫程序的思路。數組
不少人忌諱寫長名字,主要緣由,可能仍是怕敲起來麻煩。如今咱們在學校用的visual studio 2005其實有很方便的拷貝功能,事實上,個人變量名,函數名,都只敲一遍,之後所有是拷貝+粘貼。安全
這樣還有一個好處,就是我寫的代碼,永遠不會有筆誤,不會由於我敲錯一個字符而致使bug。用一個好的習慣,解決整整一個方面的bug,你說划算不划算?若是你對英語並非特別熟悉,你能夠去看看金山詞霸,在裏面又背英語又進行編碼規範的訓練挺好。再說你選擇了程序員這條路,英語簡直是關鍵中的關鍵。session
編碼規範第1、能使你的代碼更加易於維護,程序並非一次性的產品,它須要擴展和修改還有維護的。可是進行此次操做的人並必定就是你了,因此你爲了你的接班人也要將規範編碼進行到底!框架
編碼規範第2、能夠提升代碼質量,誰編程都不是一次性完成的,是須要不斷的測試與調試,以各類狀態來修改本身的代碼,而將代碼規範化,就能對程序自己有更加清晰的結構思路,從而減小調試而成爲快捷高效的代碼。ide
編碼規範第3、也是最爲重要的是將你自己個性化溶於團隊化的過程,當你熟練運用編碼規範了,就等於在之後的職場的道路上更加寬廣。函數
編碼規範是一種習慣,因此一開始習慣不養好,永遠寫不出工程型代碼。工具
1.1術語定義
1.1.1 Pascal 大小寫
將標識符的首字母和後面鏈接的每一個單詞的首字母都大寫。能夠對三字符或更多字符的標識符使用Pascal 大小寫。例如: BackColor
1.1.2 Camel 大小寫
標識符的首字母小寫,而每一個後面鏈接的單詞的首字母都大寫。例如:backColor
2.1 列寬
代碼列寬控制在110字符左右。
2.2 換行
當表達式超出或即將超出規定的列寬,遵循如下規則進行換行
一、在逗號後換行;
二、在操做符前換行;
三、規則1優先於規則2。
2.3 縮進
縮進應該是每行一個Tab(4個空格),不要在代碼中使用Tab字符。
2.4 空行
空行是爲了將邏輯上相關聯的代碼分塊,以便提升代碼的可閱讀性。
在代碼中,不能包含多個空行。
在如下狀況下使用一個空行
一、方法與方法、屬性與屬性之間。
二、方法中變量聲明與語句之間。
三、方法與方法之間。
四、方法中不一樣的邏輯塊之間。
五、方法中的返回語句與其餘的語句之間。
六、屬性與方法、屬性與字段、方法與字段之間。
七、註釋與它註釋的語句間不空行,但與其餘的語句間空一行。
2.5 空格
在如下狀況中要使用到空格
一、 關鍵字和左括符 「(」 應該用空格隔開。如while (true)
注意:在方法名和左括符 「(」 之間不要使用空格,這樣有助於辨認代碼中的方法調用與關鍵字。
二、 多個參數用逗號隔開,每一個逗號後都應加一個空格。
三、 除了 . 以外,全部的二元操做符都應用空格與它們的操做數隔開。一元操做符、++及--與操做數間不須要空格。如
a += c + d; a = (a + b) / (c * d); while (d++ = s++) { n++; } PrintSize(「size is 「 + size + 「\n」); |
四、 語句中的表達式之間用空格隔開。如
for (expr1; expr2; expr3) |
2.6 括號 - ()
一、 左括號「(」 沒關係靠關鍵字,中間用一個空格隔開。
二、 左括號「(」 與方法名之間不要添加任何空格。
三、 沒有必要的話不要在返回語句中使用()。如
if (condition) Array.Remove(1) return 1 |
2.7 花括號 - {}
一、左花括號 「{」 放於關鍵字或方法名的下一行並與之對齊。如
if (condition) { } public int Add(int x, int y) { } |
二、 左花括號 「{」 要與相應的右花括號 「}」對齊。
三、一般狀況下左花括號 「{」單獨成行,不與任何語句並列一行。
四、 if、while、do語句後必定要使用{},即便{}號中爲空或只有一條語句。如
if (somevalue == 1) { somevalue = 2; } |
五、 右花括號 「}」 後建議加一個註釋以便於方便的找到與之相應的 {。如
while (1) { if (valid) { } else { } // if } // while |
3.1 註釋概述
一、修改代碼時,老是使代碼周圍的註釋保持最新。
二、在每一個例程的開始,提供標準的註釋樣本以指示例程的用途、假設和限制頗有幫助。註釋樣本應該是解釋它爲何存在和能夠作什麼的簡短介紹.
三、避免在代碼行的末尾添加註釋;行尾註釋使代碼更難閱讀。不過在批註變量聲明時,行尾註釋是合適的;在這種狀況下,將全部行尾註釋在公共製表位處對齊。
4 、避免雜亂的註釋,如一整行星號。而是應該使用空白將註釋同代碼分開。
5 、避免在塊註釋的周圍加上印刷框。這樣看起來可能很漂亮,可是難於維護。
6 、在部署發佈以前,移除全部臨時或無關的註釋,以免在往後的維護工做中產生混亂。
7 、若是須要用註釋來解釋複雜的代碼節,請檢查此代碼以肯定是否應該重寫它。盡一切可能不註釋難以理解的代碼,而應該重寫它。儘管通常不該該爲了使代碼更簡單以便於人們使用而犧牲性能,但必須保持性能和可維護性之間的平衡。
8 、在編寫註釋時使用完整的句子。註釋應該闡明代碼,而不該該增長多義性。
9 、在編寫代碼時就註釋,由於之後極可能沒有時間這樣作。另外,若是有機會複查已編寫的代碼,在今天看來很明顯的東西六週之後或許就不明顯了。
10 、避免多餘的或不適當的註釋,如幽默的不主要的備註。
十一、 使用註釋來解釋代碼的意圖。它們不該做爲代碼的聯機翻譯。
十二、 註釋代碼中不十分明顯的任何內容。
13 、爲了防止問題反覆出現,對錯誤修復和解決方法代碼老是使用註釋,尤爲是在團隊環境中。
14 、對由循環和邏輯分支組成的代碼使用註釋。這些是幫助源代碼讀者的主要方面。
15 、在整個應用程序中,使用具備一致的標點和結構的統同樣式來構造註釋。
16 、用空白將註釋同註釋分隔符分開。在沒有顏色提示的狀況下查看註釋時,這樣作會使註釋很明顯且容易被找到。
17 、在全部的代碼修改處加上修改標識的註釋。
18 、爲了是層次清晰,在閉合的右花括號後註釋該閉合所對應的起點。
namespace Langchao.Procument.Web { } // namespace Langchao.Procument.Web |
3.2 文件註釋
在每一個文件頭必須包含如下注釋說明
// <copyright file="文件名.cs" company="HP"> // Copyright (c) HP. All rights reserved. // </copyright> // <author>×××</author> // <date> yyyy-mm-dd </date> // <summary>文件功能描述</summary> // <modify> // 修改人:××× // 修改時間:yyyy-mm-dd // 修改描述:××× // 版本:1.0 // </modify> |
注意:文件功能描述只需簡述,具體詳情在類的註釋中描述。建立標識和修改標識由建立或修改人員的拼音或英文名。如:Zhangsan。一天內有多個修改的只需作一個在註釋說明中作一個修改標識就夠了。在全部的代碼修改處加上修改標識的註釋。
3.3 文檔型註釋
該類註釋採用.Net已定義好的Xml標籤來標記,在聲明接口、類、方法、屬性、字段都應該使用該類註釋,以便代碼完成後直接生成代碼文檔,讓別人更好的瞭解代碼的實現和接口。如
一、 類、接口註釋
/// <summary> /// 類功能的說明 /// </summary> /// <see cref=""></see> /// <remarks> /// 建立人:Zhangsan /// 建立日期:yyyy-mm-dd /// 修改人:Lisi /// 修改日期:yyyy-mm-dd /// 修改備註:無 /// 版本:1.0 /// </remarks> public class CountersModuleInitializer : ModuleInitializer { } |
注意:<see cref=""></see>標籤根據具體狀況選擇有無
二、 方法、事件註釋
/// <summary> /// 根據員工編號得到員工信息 /// </summary> /// <param name="employeeId">員工編號</param> /// <exception cref="System.Exception">系統異常</exception> /// <returns>員工姓名</returns> /// <remarks> /// 建立人:Zhangsan /// 建立日期:yyyy-mm-dd /// 修改人:Lisi /// 修改日期:yyyy-mm-dd /// 修改備註:無 /// 版本:1.1 /// </remarks> public string GetEmployeeNameById(int employeeId) { try { return "ddd"; } catch (System.Exception) { throw; } } |
注意:該方法註釋中的<param></param>、<exception cref=" "></exception>、<returns></returns>等標籤根據具體狀況選擇有無,方法初始版本爲1.0,每次改動增長0.1
三、 屬性、常量註釋
/// <summary> /// session id /// </summary> public const string SESSION_ID = ""; |
3.3 單行註釋
該類註釋用於
1 方法內的代碼註釋。如變量的聲明、代碼或代碼段的解釋。註釋示例:
// 註釋語句 private int number; |
2 方法內變量的聲明或花括號後的註釋, 註釋示例:
if ( 1 == 1) // always true { statement; } else // always false { } |
3.4 JavaScript註釋
a) 註釋符號
‘//’
不容許使用‘/**/’做註釋符。
b) 函數註釋
每一個函數都應該描述該函數的名稱、功能、做用範圍、入口參數的類型和傳值方式及參數含義、返回值類型及返回值的含義。格式以下:
// //Function: 函數名 //Purpose: 用途 //Scope: 做用範圍 //Args: 入口參數(列表) 類型傳值方式含義 //Returns: 返回值類型 (可肯定值列表) 含義 // |
c) 非函數註釋
註明該模塊的做用
格式以下:
// //功能: // |
d) 程序行間註釋
在程序行的每個處理單元前做註釋
格式以下:
//註釋
e) 註釋舉例
// //Function: F_FindObject //Purpose: 按照空間名在可視化主對象中查找住對象內的可視化控件 //Scope: Public //Args: is_name String value:要查找的空間名 // ipbo_object Object value: 可視化主對象 //Returns: Boolean True 表示找到該控件 // False 表示沒有找到該控件 // function F_FindObject(is_name, ipbo_object) { //得到顯示學生信息的GreeView控件 var gv_student = document.getElementById("GVStudent"); *********** //返回true return true; } |
3.5 註釋標籤
標籤 | 用法 | 做用 |
<c> | c>text</c> text 但願將其指示爲代碼的文本。 |
爲您提供了一種將說明中的文本標記爲代碼的方法。使用 <code> 將多行指示爲代碼 |
<para> | <para>content</para> content段落文本。 |
用於諸如 <remarks> 或 <returns> 等標記內,使您得以將結構添加到文本中。 |
<param> | <param name='name'>description</param> name 爲方法參數名。將此名稱用單引號括起來 (' ')。 |
應當用於方法聲明的註釋中,以描述方法的一個參數。 |
<paramref> | <paramref name="name"/> name 要引用的參數名。將此名稱用雙引號括起來 (" ")。 |
<paramref> 標記爲您提供了一種指示詞爲參數的方法。能夠處理 XML 文件,從而用某種獨特的方法格式化該參數。 |
<see> | <see cref="member"/> cref = "member" 對能夠經過當前編譯環境進行調用的成員或字段的引用。編譯器檢查到給定代碼元素存在後,將 member 傳遞給輸出 XML 中的元素名。必須將 member 括在雙引號 (" ") 中。 |
使您得以從文本內指定連接。使用 <seealso> 指示但願在「請參閱」一節中出現的文本。 |
<seealso> | <seealso cref="member"/> cref = "member" 對能夠經過當前編譯環境進行調用的成員或字段的引用。編譯器檢查到給定代碼元素存在後,將 member 傳遞給輸出 XML 中的元素名。必須將 member 括在雙引號 (" ") 中 |
使您得以指定但願在「請參閱」一節中出現的文本。使用 <see> 從文本 |
<example> | <example>description</example> description 代碼示例的說明。 |
使用 <example> 標記能夠指定使用方法或其餘庫成員的示例。通常狀況下,這將涉及到 <code> 標記的使用。 |
<code> | <code>content</code> content 爲但願將其標記爲代碼的文本。 |
記爲您提供了一種將多行指示爲代碼的方法。使用 <c> 指示應將說明中的文本標記爲代碼 |
<summary> | <summary>description</summary> 此處description 爲對象的摘要。 |
應當用於描述類型成員。使用 <remarks> 以提供有關類型自己的信息。 |
<exception> | <exception cref="member">description</exception> cref = "member" 對可從當前編譯環境中獲取的異常的引用。編譯器檢查到給定異常存在後,將 member 轉換爲輸出 XML 中的規範化元素名。必須將 member 括在雙引號 (" ") 中。 description 說明。 |
<exception> 標記使您能夠指定類可以引起的異常。 |
<include> | <include file='filename' path='tagpath[@name="id"]' /> filename 包含文檔的文件名。該文件名可用路徑加以限定。將 filename 括在單引號中 (' ')。 Tagpath:filename 中指向標記名的標記路徑。將此路徑括在單引號中 (' ')。 name 註釋前邊的標記中的名稱說明符;名稱具備一個 id。 id 位於註釋以前的標記的 id。將此 id 括在雙引號中 (" ")。 |
<include> 標記使您得以引用描述源代碼中類型和成員的另外一文件中的註釋。這是除了將文檔註釋直接置於源代碼文件中以外的另外一種可選方法。 <include> 標記使用 XML XPath 語法。有關自定義 <include> 使用的方法,請參閱 XPath 文檔。 |
<list> | <list type="bullet" | "number" | "table"> <listheader> <term>term</term> <description>description</description> </listheader> <item> <term>term</term> <description>description</description> </item> </list> term 定義的項,該項將在 text 中定義。 description 目符號列表或編號列表中的項或者 term 的定義。 |
<listheader> 塊用於定義表或定義列表中的標題行。定義表時,只需爲標題中的項提供一個項。 列表中的每一項用 <item> 塊指定。建立定義列表時,既須要指定 term 也須要指定 text。可是,對於表、項目符號列表或編號列表,只需爲 text 提供一個項。 列表或表所擁有的 <item> 塊數能夠根據須要而定。 |
<permission> | <permission cref="member">description</permission> cref = "member" 對能夠經過當前編譯環境進行調用的成員或字段的引用。編譯器檢查到給定代碼元素存在後,將 member 轉換爲輸出 XML 中的規範化元素名。必須將 member 括在雙引號 (" ") 中。 description 成員的訪問的說明。 |
<permission> 標記使您得以將成員的訪問記入文檔。 System.Security.PermissionSet 使您得以指定對成員的訪問。 |
<remarks> | <remarks>description</remarks> description 成員的說明。 |
<remarks> 標記是能夠指定有關類或其餘類型的概述信息的位置。<summary> 是能夠描述該類型的成員的位置。 |
<returns> | <returns>description</returns> description 返回值的說明。 |
<returns> 標記應當用於方法聲明的註釋,以描述返回值。 |
<value> | <value>property-description</value> property-description 屬性的說明。 |
<value> 標記使您得以描述屬性。請注意,當在 Visual Studio .NET 開發環境中經過代碼嚮導添加屬性時,它將會爲新屬性添加 <summary> 標記。而後,應該手動添加 <value> 標記以描述該屬性所表示的值。 |
4.1 每行聲明數
一行只做一個聲明,如
int level; //推薦 int size; //推薦 int x, y; //不推薦 |
4.2 初始化
建議在變量聲明時就對其作初始化。
4.3 位置
變量建議置於塊的開始處,不要老是在第一次使用它們的地方作聲明。如
void MyMethod() { int int1 = 0; // beginning of method block if (condition) { int int2 = 0; // beginning of "if" block ... } } |
不過也有一個例外
for (int i = 0; i < maxLoops; i++) { ... } |
應避免不一樣層次間的變量重名,如
int count; ... void MyMethod() { if (condition) { int count = 0; // 避免 ... } ... } |
4.4 類和接口的聲明
1 在方法名與其後的左括號間沒有任何空格。
2 左花括號 「{」 出如今聲明的下行並與之對齊,單獨成行。
3 方法間用一個空行隔開。
4.5 字段的聲明
不要使用是 public 或 protected 的實例字段。若是避免將字段直接公開給開發人員,能夠更輕鬆地對類進行版本控制,緣由是在維護二進制兼容性時字段不能被更改成屬性。考慮爲字段提供 get 和set 屬性訪問器,而不是使它們成爲公共的。 get 和 set 屬性訪問器中可執行代碼的存在使得能夠進行後續改進,如在使用屬性或者獲得屬性更改通知時根據須要建立對象。下面的代碼示例闡釋帶有get 和 set 屬性訪問器的私有實例字段的正確使用。 示例:
public class Control: Component { private int handle; public int Handle { get { return handle; } } } |
5.1 命名概述
名稱應該說明「什麼」而不是「如何」。經過避免使用公開基礎實現(它們會發生改變)的名稱,能夠保留簡化複雜性的抽象層。例如,可使用 GetNextStudent(),而不是 GetNextArrayElement()。
命名原則是:
選擇正確名稱時的困難可能代表須要進一步分析或定義項的目的。使名稱足夠長以便有必定的意義,而且足夠短以免冗長。惟一名稱在編程上僅用於將各項區分開。表現力強的名稱是爲了幫助人們閱讀;所以,提供人們能夠理解的名稱是有意義的。不過,請確保選擇的名稱符合適用語言的規則和標準。
如下幾點是推薦的命名方法。
一、避免容易被主觀解釋的難懂的名稱,如方面名 AnalyzeThis(),或者屬性名 xxK8。這樣的名稱會致使多義性。
二、在類屬性的名稱中包含類名是多餘的,如 Book.BookTitle。而是應該使用 Book.Title。
三、只要合適,在變量名的末尾或開頭加計算限定符(Avg、Sum、Min、Max、Index)。
四、在變量名中使用互補對,如 min/max、begin/end 和 open/close。
五、布爾變量名應該包含 Is,這意味着 Yes/No 或 True/False 值,如 fileIsFound。
六、在命名狀態變量時,避免使用諸如 Flag 的術語。狀態變量不一樣於布爾變量的地方是它能夠具備兩個以上的可能值。不是使用 documentFlag,而是使用更具描述性的名稱,如 documentFormatType。 (此項只供參考)
七、即便對於可能僅出如今幾個代碼行中的生存期很短的變量,仍然使用有意義的名稱。僅對於短循環索引使用單字母變量名,如 i 或 j。 可能的狀況下,儘可能不要使用原義數字或原義字符串,如
For i = 1 To 7。而是使用命名常數,如 For i = 1 To NUM_DAYS_IN_WEEK 以便於維護和理解。
八、用於事件處理的委託添加「EventHandler」後綴
九、用於事件處理以外的那些委託添加「Callback」後綴
十、不要給委託添加「Delegate」後綴
十一、用名詞或名詞詞組來給類型命名,在少數狀況下也能夠用形容詞詞組來給類型命名
十二、用動詞或動詞詞組來命名方法
1三、用名詞、名詞詞組或形容詞來命名屬性
1四、要用動詞或動詞短語來命名事件
1五、要用名詞或名詞短語來命名字段
5.2 大小寫規則
大寫
標識符中的全部字母都大寫。僅對於由兩個或者更少字母組成的標識符使用該約定。例如:
System.IO System.Web.UI |
下表彙總了大寫規則,並提供了不一樣類型的標識符的示例。
標識符 | 大小寫 | 樣例 |
名字空間 | Pascal | namespace System.Security { … } |
類型 | Pascal | public class StreamReader { … } |
接口 | Pascal | public interface IEnumerable { … } |
方法 | Pascal | public class Object { public virtual string ToString(); } |
屬性 | Pascal | public class String { public int Length { get; } } |
事件 | Pascal | public class Process{ public event EventHandler Exited; } |
字段(私有實例) | Camel | private string userName; |
字段(公共靜態) | Pascal | public static readonly string UserId; |
枚舉 | Pascal | enum FileMode { Append, … } |
參數 | Camel | public class Convert { public static int ToInt32(string userId); } |
5.3 縮寫
爲了不混淆和保證跨語言交互操做,請遵循有關區縮寫的使用的下列規則:
1 不要將縮寫或縮略形式用做標識符名稱的組成部分。例如,使用 GetWindow,而不要使用 GetWin。
2 不要使用計算機領域中未被廣泛接受的縮寫。
3 在適當的時候,使用衆所周知的縮寫替換冗長的詞組名稱。例如,用 UI 做爲 User Interface 縮寫,用 OLAP 做爲 On-line Analytical Processing 的縮寫。
4在使用縮寫時,對於超過兩個字符長度的縮寫請使用 Pascal 大小寫或 Camel 大小寫。例如,使用 HtmlButton 或 HTMLButton。可是,應當大寫僅有兩個字符的縮寫,如,System.IO,而不是 System.Io。
5 不要在標識符或參數名稱中使用縮寫。若是必須使用縮寫,對於由多於兩個字符所組成的縮寫請使用Camel 大小寫,雖然這和單詞的標準縮寫相沖突。
5.4 命名空間
一、給命名空間命名時的通常性規則是使用公司名稱,後跟技術名稱和可選的功能與設計,以下所示。
CompanyName.TechnologyName[.Feature][.Design]
例如:
namespace JadeBird.StudentManager //學員系統
namespace JadeBird.StudentManager.Register //北學員註冊模塊
二、命名空間使用Pascal大小寫,用點號分隔。
三、TechnologyName 指的是該項目的英文縮寫,或軟件名。
四、命名空間和類不能使用一樣的名字。例如,有一個類被命名爲Debug後,就不要再使用Debug做爲一個名稱空間名。
5.5 文件命名
1 文件名聽從Pascal命名法,無特殊狀況,擴展名小寫。
2 使用統一而又通用的文件擴展名:
文件類型 | 擴展名 |
C#類 | .cs |
Asp.net頁面 | .aspx |
… | 使用默認擴展名 |
5.6 類
一、使用 Pascal 大小寫。
二、用名詞或名詞短語命名類。
三、使用全稱避免縮寫,除非縮寫已經是一種公認的約定,如URL、HTML
4 、不要使用類型前綴,如在類名稱上對類使用 C 前綴。例如,使用類名稱 FileStream,而不是CFileStream。
5 、不要使用下劃線字符 (_)。
6 、有時候須要提供以字母 I 開始的類名稱,雖然該類不是接口。只要 I 是做爲類名稱組成部分的整個單詞的第一個字母,這即是適當的。例如,類名稱 IdentityStore 是適當的。在適當的地方,使用複合單詞命名派生的類。派生類名稱的第二個部分應當是基類的名稱。例如,ApplicationException 對於從名爲 Exception 的類派生的類是適當的名稱,緣由ApplicationException 是一種Exception。請在應用該規則時進行合理的判斷。例如,Button 對於從 Control 派生的類是適當的名稱。儘管按鈕是一種控件,可是將 Control 做爲類名稱的一部分將使名稱沒必要要地加長。
public class FileStream public class Button public class String |
5.7 接口
如下規則概述接口的命名指南:
一、用名詞或名詞短語,或者描述行爲的形容詞命名接口。例如,接口名稱 IComponent 使用描述性名詞。接口名稱 ICustomAttributeProvider 使用名詞短語。名稱 IPersistable 使用形容詞。
二、使用 Pascal 大小寫。
三、少用縮寫。
四、給接口名稱加上字母 I 前綴,以指示該類型爲接口。在定義類/接口對(其中類是接口的標準實現)時使用類似的名稱。兩個名稱的區別應該只是接口名稱上有字母 I 前綴。
五、不要使用下劃線字符 (_)。
六、當類是接口的標準執行時,定義這一對類/接口組合就要使用類似的名稱。兩個名稱的不一樣之處只是接口名前有一個I前綴。
如下是正確命名的接口的示例。
public interface IServiceProvider
public interface IFormatable
如下代碼示例闡釋如何定義 IComponent 接口及其標準實現 Component 類。
public interface IComponent { // Implementation code goes here. } public class Component: IComponent { // Implementation code goes here. } |
5.8 枚舉 (Enum)
枚舉 (Enum) 值類型從 Enum 類繼承。如下規則概述枚舉的命名指南:
1 對於 Enum 類型和值名稱使用 Pascal 大小寫。
2 少用縮寫。
3 不要在 Enum 類型名稱上使用 Enum 後綴。
4 對大多數 Enum 類型使用單數名稱,可是對做爲位域的 Enum 類型使用複數名稱。
5 老是將 FlagsAttribute 添加到位域 Enum 類型。
6不要命名「Reserved」枚舉值。
5.9 參數
如下規則概述參數的命名指南:
一、使用描述性參數名稱。參數名稱應當具備足夠的描述性,以便參數的名稱及其類型可用於在大多數狀況下肯定它的含義。
二、對參數名稱使用 Camel 大小寫。
三、 使用描述參數的含義的名稱,而不要使用描述參數的類型的名稱。開發工具將提供有關參數的類型的有意義的信息。所以, 經過描述意義,能夠更好地使用參數的名稱。少用基於類型的參數名稱,僅在適合使用它們的地方使用它們。
四、不要使用保留的參數。保留的參數是專用參數,若是須要,能夠在將來的版本中公開它們。相反,若是在類庫的將來版本中須要更多的數據,請爲方法添加新的重載。
五、不要給參數名稱加匈牙利語類型表示法的前綴。
如下是正確命名的參數的示例。
Type GetType(string typeName)
string Format(string format, args() As object)
5.10 方法
如下規則概述方法的命名指南:
1 使用動詞或動詞短語命名方法。
2 使用 Pascal 大小寫。
3 如下是正確命名的方法的實例。
RemoveAll()
GetCharArray()
Invoke()
5.11 屬性 (property)
如下規則概述屬性的命名指南:
1 使用名詞或名詞短語命名屬性。
2 使用 Pascal 大小寫。
3 不要使用匈牙利語表示法。
4 考慮用與屬性的基礎類型相同的名稱建立屬性。例如,若是聲明名爲 Color 的屬性,則屬性的類型一樣應該是 Color。請參閱本主題中後面的示例。
如下代碼示例闡釋正確的屬性命名。
public class SampleClass { public Color BackColor { // Code for Get and Set accessors goes here. } } |
如下代碼示例闡釋提供其名稱與類型相同的屬性。
public enum Color { // Insert code for Enum here. } public class Control { public Color Color { get { // Insert code here. } set { // Insert code here.} } } |
如下代碼示例不正確,緣由是 Color 屬性是 Int 類型的。
public enum Color { // Insert code for Enum here. } public class Control { public int Color { // Insert code here } } |
在不正確的示例中,不可能引用 Color 枚舉的成員。Color.Xxx 將被解釋爲訪問一個成員, 該成員首先獲取 Color 屬性( C# 中爲 int 類型)的值,而後再訪問該值的某個成員(該成員必須是 System.Int32 的實例成員)。
5.12 事件
如下規則概述事件的命名指南:
一、對事件處理程序名稱使用 EventHandler 後綴。
二、指定兩個名爲 sender 和 e 的參數。sender 參數表示引起事件的對象。sender 參始終是object 類型的,即便在可使用更爲特定的類型時也如此。與事件相關聯的狀態封裝在名爲 e 的事件類的實例中。對 e 參數類型使用適當而特定的事件類。
三、用 EventArgs 後綴命名事件參數類。
四、考慮用動詞命名事件。
五、使用動名詞(動詞的「ing」形式)建立表示事件前的概念的事件名稱,用過去式表示事件後。例如,能夠取消的 Close 事件應當具備 Closing 事件和 Closed 事件。不要使用 BeforeXxx/AfterXxx 命名模式。
六、不要在類型的事件聲明上使用前綴或者後綴。例如,使用 Close,而不要使用 OnClose。
七、一般狀況下,對於能夠在派生類中重寫的事件,應在類型上提供一個受保護的方法(稱爲 OnXxx)。此方法只應具備事件參數 e,由於發送方老是類型的實例。
如下示例闡釋具備適當名稱和參數的事件處理程序。
public delegate void MouseEventHandler(object sender, MouseEventArgs e);
如下示例闡釋正確命名的事件參數類。
public class MouseEventArgs : EventArgs { int x; int y; public MouseEventArgs(int x, int y) { this.x = x; this.y = y; } public int X { get { return x; } } public int Y { get { return y; } } } |
5.13 常量 (const)
如下規則概述常量的命名指南:
全部單詞大寫,多個單詞之間用 "_" 隔開。 如
public const string PAGE_TITLE = "Welcome";
5.14 字段
如下規則概述字段的命名指南:
一、private、protected 使用 Camel 大小寫。
二、public 使用 Pascal 大小寫。
三、拼寫出字段名稱中使用的全部單詞。僅在開發人員通常都能理解時使用縮寫。字段名稱不要使用大寫字母。下面是正確命名的字段的示例。
class SampleClass { string url; string destinationUrl; } |
四、不要對字段名使用匈牙利語表示法。好的名稱描述語義,而非類型。
五、不要對字段名或靜態字段名應用前綴。具體說來,不要對字段名稱應用前綴來區分靜態和非靜態字段。例如,應用 g_ 或 s_ 前綴是不正確的。
六、對預約義對象實例使用公共靜態只讀字段。若是存在對象的預約義實例,則將它們聲明爲對象自己的公共靜態只讀字段。使用 Pascal 大小寫,緣由是字段是公共的。下面的代碼示例闡釋公共靜態只讀字段的正確使用。
public struct Color { public static readonly Color Red = new Color(0x0000FF); public Color(int rgb) { // Insert code here.} public Color(byte r, byte g, byte b) { // Insert code here. } public byte RedValue { get { return Color; } } } |
5.15 靜態字段
如下規則概述靜態字段的命名指南:
一、使用名詞、名詞短語或者名詞的縮寫命名靜態字段。
二、使用 Pascal 大小寫。
三、對靜態字段名稱使用匈牙利語表示法前綴。
四、建議儘量使用靜態屬性而不是公共靜態字段。
5.16 集合
集合是一組組合在一塊兒的相似的類型化對象,如哈希表、查詢、堆棧、字典和列表,集合的命名建議用複數。
5.17 措詞
避免使用與經常使用的 .NET 框架命名空間重複的類名稱。例如,不要將如下任何名稱用做類名稱:
System、Collections、Forms 或 UI。有關 .NET 框架命名空間的列表,請參閱類庫。
另外,避免使用和如下關鍵字衝突的標識符。
6.1 每行一個語句
每行最多包含一個語句。如
a++; //推薦 b--; //推薦 a++; b--; //不推薦 |
6.2 複合語句
複合語句是指包含"父語句{子語句;子語句;}"的語句,使用複合語句應遵循如下幾點
1 、子語句要縮進。
2 、左花括號「{」 在複合語句父語句的下一行並與之對齊,單獨成行。
3 、即便只有一條子語句要不要省略花括號「 {}」。 如
while (d + = s++) { n++; } |
6.3 return 語句
return語句中不使用括號,除非它能使返回值更加清晰。如
return; return myDisk.size(); return (size ? size : defaultSize); |
6.4 if、 if-else、if else-if 語句
if、 if-else、if else-if 語句使用格式
if (condition) { statements; } if (condition) { statements; } else { statements; } if (condition) { statements; } else if (condition) { statements; } else { statements; } |
6.4 for、foreach 語句
for 語句使用格式
for (initialization; condition; update) { statements; } |
空的 for 語句(全部的操做都在initialization、condition 或 update中實現)使用格式
for (initialization; condition; update); // update user id |
foreach 語句使用格式
foreach (object obj in array) { statements; } |
注意 1.在循環過程當中不要修改循環計數器。
2.對每一個空循環體給出確認性註釋。
6.5 while 語句
while 語句使用格式
while (condition) { statements; } |
空的 while 語句使用格式
while (condition); |
6.7. do - while 語句
do - while 語句使用格式
do { statements; } while (condition); |
6.8. switch - case 語句
switch - case 語句使用格式
switch (condition) { case 1: statements; break; case 2: statements; break; default: statements; break; } |
注意:
一、語句switch中的每一個case各佔一行。
二、語句switch中的case按字母順序排列。
三、爲全部switch語句提供default分支。
四、全部的非空 case 語句必須用 break; 語句結束。
6.9. try - catch 語句
try - catch 語句使用格式
try { statements; } catch (ExceptionClass e) { statements; } finally { statements; } |
6.10. using 塊語句
using 塊語句使用格式
using (object) { statements; } |
6.11. goto 語句
goto 語句使用格式
goto Label1: statements; Lable1: statements; |
7.1 命名方法
控件名簡寫+英文描述,英文描述首字母大寫
7.2 主要控件名簡寫對照表
控件名 | 簡寫 | 控件名 | 簡寫 |
Label | lbl | TextBox | txt |
Button | btn | LinkButton | lnkbtn |
ImageButton | imgbtn | DropDownList | ddl |
ListBox | lst | DataGrid | dg |
DataList | dl | CheckBox | chk |
CheckBoxList | chkls | RadioButton | rdo |
RadioButtonList | rdolt | Image | img |
Panel | pnl | Calender | cld |
AdRotator | ar | Table | tbl |
RequiredFieldValidator | rfv | CompareValidator | cv |
RangeValidator | rv | RegularExpressionValidator | rev |
ValidatorSummary | vs | CrystalReportViewer | rptvew |
代碼格式檢查使用微軟內部代碼檢查工具 StyleCop 版本4.3.2.1,它會根據預約義的C#代碼格式的最佳實踐,對源代碼進行檢查,並給出不符合編碼風格的錯誤提示(版本語言英文)。
8.1 檢查分類
檢查規則分爲7個部分,分別是
「文檔規則(Documentation Rules)」
「佈局規則(LayoutRules)」
「可維護性規則(Maintanability Rules)」
「命名規則(Naming Rules)」
「代碼順序規則(Ordering Rules)」
「可讀性規則(Readability Rules)」
「間距規則(Spacing Rules)」
8.2 安裝及使用
安裝:安裝程序位於附件,Install文件夾中。該軟件屬於Visual Studio插件,安裝後在工具菜單欄中。
使用:運行該功能後,能夠根據「錯誤列表」中的警告信息的信息Id,從幫助文件(位於附件,Document文件夾中,文件名StyleCop.chm)找到相應編碼格式,進行修復。
開發文檔:位於附件,Document文件夾中,文件名StyleCopSDK.chm)
對於如下狀況 「是否檢查」一項中爲「√」的內容不能違反。
8.3 文檔規則(Documentation Rules)
標識及提示 | 中文及示例 | 是否檢查 |
SA1600: Elements MustBe Documented | 元素必須被註釋 /// <summary> /// Joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName">The first name to join.</param> /// <param name="lastName">The last name to join.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { return firstName + " " + lastName; } |
√
|
SA1601:Partial Elements MustBe Documented | /// <summary> /// Documentation for the first part of Class1. /// </summary> public partial class Class1 { } /// <summary> /// Documentation for the second part of Class1. /// </summary> public partial class Class1 { } |
√
|
SA1602:Enumeration Items Must Be Documented | 枚舉項必須被註釋 /// <summary> /// Types of animals. /// </summary> public enum Animals { /// <summary> /// Represents a dog. /// </summary> Dog, /// <summary> /// Represents a cat. /// </summary> Cat, /// <summary> /// Represents a horse. /// </summary> Horse } |
√
|
SA1603:Documentation Must Contain Valid Xml | 文檔必須包含有效的XML註釋 /// <summary> /// An example of badly formed Xml. /// </summa3ry> public class Example { } |
√
|
SA1604: Element Documentation Must Have Summary | 文檔註釋必須包含Summary /// <summary> /// Represents a customer in the database. /// </summary> public class Customer { } |
√
|
SA1605:Partial Element Documentation Must Have Summary | 部分文檔註釋必須包含Summary /// <summary> /// Documentation for the first part of Class1. /// </summary> public partial class Class1 { } /// <summary> /// Documentation for the second part of Class1. /// </summary> public partial class Class1 { } |
√
|
SA1606: Element Documentation Must Have Summary Text | 文檔註釋Summary必須有內容 /// <summary> </summary> /// <param name="customerId">The ID of the customer to find.</param> /// <returns>The customer, or null if the customer could not be /// found.</returns> public Customer FindCustomer(int customerId) { // ... finds the customer ... } |
√
|
SA1607:Partial Element Documentation Must Have Summary Text | 部分文檔註釋Summary必須有內容 /// <summary> </summary> /// <param name="customerId">The ID of the customer to find.</param> /// <returns>The customer, or null if the customer could not be found.</returns> public Customer FindCustomer(int customerId) { // ... finds the customer ... } 修復代碼 /// <summary>Attempts to locate a record for the customer with the given ID. </summary> /// <param name="customerId">The ID of the customer to find.</param> /// <returns>The customer, or null if the customer could not be found.</returns> public Customer FindCustomer(int customerId) { // ... finds the customer ... } |
√
|
SA1608:Element Documentation Must Not Have Default Summary | 文檔註釋不能有默認的Summary /// <summary> /// Summary description for the Example class. /// </summary> public class Example { } |
√
|
SA1609: Property Documentation Must Have Value | 屬性註釋必須有值 /// <summary> /// Gets the name of the customer. /// </summary> /// <value>The name of the customer.</value> public bool Name { get { return this.name; } } |
√
|
SA1610: Property Documentation Must Have Value Text | 屬性註釋必須有值內容 /// <summary> /// Gets the name of the customer. /// </summary> /// <value>The name of the customer.</value> public bool Name { get { return this.name; } } |
√
|
SA1611:Element Parameters Must Be Documented | 元素的參數必須註釋 /// <summary> /// Joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName">The first name to join.</param> /// <param name="lastName">The last name to join.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { return firstName + " " + lastName; } |
√
|
SA1612:Element Parameter Documentation Must Match Element Parameters | 元素的參數註釋必須與元素參數成對 /// <summary> /// Joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName">The first name to join.</param> /// <param name="lastName">The last name to join.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { return firstName + " " + lastName; } |
√
|
SA1613:Element Parameter Documentation Must Declare Parameter Name | 元素的參數註釋必須定義參數名 /// <summary> /// Joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName">The first name to join.</param> /// <param name="lastName">The last name to join.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { return firstName + " " + lastName; } |
√
|
SA1614: Element Parameter Documentation Must Have Text | 元素的參數註釋必須有值 /// <summary> /// Joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName">The first name to join.</param> /// <param name="lastName">The last name to join.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { return firstName + " " + lastName; } |
√
|
SA1615: Element Return Value Must Be Documented | 元素的返回值必須被註釋 /// <summary> /// Joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName">The first name to join.</param> /// <param name="lastName">The last name to join.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { return firstName + " " + lastName; } |
√
|
SA1616: Element Return Value Documentation Must Have Value | 元素的返回值註釋必須有值 /// <summary> /// Joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName">The first name to join.</param> /// <param name="lastName">The last name to join.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { return firstName + " " + lastName; } |
√
|
SA1617: Void Return Value Must Not Be Documented | 元素空返回值不能註釋 /// <summary> /// Prints the given name. /// </summary> /// <param name="firstName">The first name.</param> /// <param name="lastName">The last name.</param> public void PrintNames(string firstName, string lastName) { Console.WriteLine(firstName + " " + lastName); } |
√
|
SA1618:Generic Type Parameters Must Be Documented | 泛型參數必須被註釋 /// <summary> /// A sample generic class. /// </summary> /// <typeparam name="S">The first generic type parameter.</typeparam> /// <typeparam name="T">The second generic type parameter.</typeparam> public class Class1<S, T> { } |
√
|
SA1619:Generic Type Parameters Must Be Documented Partial Class | 泛型參數在部分類中必須被註釋 /// <summary> /// A sample generic class. /// </summary> /// <typeparam name="S">The first generic type parameter.< /// <typeparam name="T">The second generic type parameter.</typeparam> public class Class1<S, T> { } |
√
|
SA1620:Generic Type Parameter Documentation Must Match Type Parameters | 泛型參數註釋必須與參數類型對應 /// <summary> /// A sample generic class. /// </summary> /// <typeparam name="S">The first generic type parameter.</typeparam> /// <typeparam name="T">The second generic type parameter.</typeparam> public class Class1<S, T> { } |
√
|
SA1621:GenericType Parameter Documentation Must Declare Parameter Name | 泛型參數註釋必須定義參數名 /// <summary> /// A sample generic class. /// </summary> /// <typeparam name="S">The first generic type parameter.</typeparam> /// <typeparam name="T">The second generic type parameter.</typeparam> public class Class1<S, T> { } |
√
|
SA1622:Generic Type Parameter Documentation Must Have Text | 泛型參數註釋必須有內容 /// <summary> /// A sample generic class. /// </summary> /// <typeparam name="S">The first generic type parameter.</typeparam> /// <typeparam name="T">The second generic type parameter.</typeparam> public class Class1<S, T> { } |
√
|
SA1623: Property Summary Documentation Must Match Accessors | 屬性摘要文檔必須和訪問者對應 (參考幫助文檔) |
√
|
SA1624: Property Summary Documentation Must OmitSet Accessor With Restriced Access | 屬性摘要文檔必須必須省略設置訪問器約束訪問 (參考幫助文檔) |
√
|
SA1625: Element Documentation Must Not Be Copied And Pasted | 元素註釋不能被拷貝和粘貼 /// <summary> /// Joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName">The first name to join.</param> /// <param name="lastName">The last name to join.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { return firstName + " " + lastName; } |
√
|
SA1626: Single Line Comments Must NotUse Documentation Style Slashes | 單行註釋不能使用斜線樣式 /// <summary> /// Joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName">Part of the name.</param> /// <param name="lastName">Part of the name.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { A legal comment beginning with two slashes: // Join the names together. string fullName = firstName + " " + lastName; An illegal comment beginning with three slashes: /// Trim the name. fullName = fullName.Trim(); A line of commented-out code beginning with four slashes: ////fullName = asfd; return fullName; } |
√
|
SA1627: Documentation Text Must Not Be Empty | 註釋內容不能爲空 /// <summary> /// Joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName"> </param> /// <param name="lastName">Part of the name.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { ... } |
√
|
SA1628: Documentation Tex tMust Begin With A Capital Letter | 註釋內容首字母必須大寫 /// <summary> /// joins a first name and a last name together into a single string. /// </summary> /// <param name="firstName">The first name.</param> /// <param name="lastName">The last name.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { ... } |
√
|
SA1629: Documentation Text Must End With A Period | 註釋內容必須用句號結尾 /// <summary> /// Joins a first name and a last name together into a single string /// </summary> /// <param name="firstName">The first name.</param> /// <param name="lastName">The last name.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { ... } |
√
|
SA1630: Documentation Text Must Contain Whitespace | 註釋內容單詞之間必須包含空白符 /// <summary> /// Joinsnames /// </summary> /// <param name="firstName">First</param> /// <param name="lastName">Last</param> /// <returns>Name</returns> public string JoinNames(string firstName, string lastName) { ... } |
|
SA1631: Documentation Text Must Meet Character Percentage | 註釋內容必須知足字符比例特殊字符不能過多 /// <summary> /// @)$(*A name-------- /// </summary> public class Name { ... } |
|
SA1632: Documentation Text Must Meet Minimum Character Length | 註釋內容必須知足小寫字符長度 /// <summary> /// A name /// </summary> public class Name { ... } |
√
|
SA1633: FileMust Have Header | 文件必須有文件頭部註釋 //----------------------------------------------------------- // <copyright file="NameOfFile.cs" company="CompanyName"> // Company copyright tag. // </copyright> //------------------------------------------------------------ For example, a file called Widget.cs from a fictional company called Sprocket Enterprises should contain a file header similar to the following: //------------------------------------------------------------ // <copyright file="Widget.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> //------------------------------------------------------------- The dashed lines at the top and bottom of the header are not strictly necessary,so the header could be written as: // <copyright file="Widget.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> It is possible to add additional tags, although they will not be checked or enforced by StyleCop: //------------------------------------------------------------- // <copyright file="Widget.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> // <author>John Doe</author> //------------------------------------------------------------- |
√
|
SA1634: File Header Must Sho wCopyright | 文件頭部註釋必須顯示版權信息標識 //--------------------------------------------------------- // <Tag>A file header which does not contain a copyright tag</Tag> //-------------------------------------------------------- A file header should include a copyright tag, as follows: //-------------------------------------------------------- // <copyright file="Widget.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> //------------------------------------------------------- |
√
|
SA1635: File Header Must Have Copyright Text | 文件頭部註釋必須有版權信息表示內容 //----------------------------------------------------------- // <copyright file="Widget.cs" company="Sprocket Enterprises"> // </copyright> //----------------------------------------------------------- A file header should include copyright text, as follows: //----------------------------------------------------------- // <copyright file="Widget.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> //---------------------------------------------------------- |
√
|
SA1636: File Header Must Contain File Name | 文件頭部註釋必須包含文件名 //----------------------------------------------------- // <copyright file="Widget.cs" company="My Company"> // Custom company copyright tag. // </copyright> //------------------------------------------------------- |
√
|
SA1637: File Heade rMust Contain File Name | 文件頭部註釋必須包含文件名 //----------------------------------------------------------- // <copyright company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> //------------------------------------------------------------ //------------------------------------------------------------ // <copyright file="Widget.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> //--------------------------------------------------------- |
√
|
SA1638: File Header File Name Documentation Must Match File Name | 文件頭部註釋文件名註釋必須與文件名對應 //----------------------------------------------------------- // <copyright file="File2.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> //----------------------------------------------------------- A violation of this rule would occur, since the file tag does not contain the name of the file. The header should be written as: //----------------------------------------------------------- // <copyright file="File1.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> //---------------------------------------------------------- |
√
|
SA1639: File Header Must Have Summary | 文件頭部註釋不準有摘要 //---------------------------------------------------------- // <copyright file="Widget.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> //---------------------------------------------------------- If this rule is enabled, the file header should contain a summary tag. For example: //---------------------------------------------------------- // <copyright file="Widget.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> // <summary>Defines the Widget class.</summary> //---------------------------------------------------------- |
√
|
SA1640: File Header Must Have Valid Company Text | 文件頭部註釋必須有正確的公司信息 //----------------------------------------------------------- // <copyright file="Widget.cs" company="Sprocket Enterprises"> // Copyright (c) Sprocket Enterprises. All rights reserved. // </copyright> //-------------------------------------------------------------- |
√
|
SA1641: File Header Company Name Text Must Match | 文件頭部註釋公司內容必須對應 //--------------------------------------------------------- // <copyright file="Widget.cs" company="My Company"> // Custom company copyright tag. // </copyright> //---------------------------------------------------------- |
√
|
SA1642: Constructor Summary Documentation Must Begin With Standard Text | 構造器的Summary註釋必須由標準內容開始 (參考幫助文檔) |
√
|
SA1643: Destructor Summary Documentation Must Begin With Standard Text | 析構器的Summary註釋必須由標準內容開始 /// <summary> /// Initializes a new instance of the <see cref="Customer`1"/> class. /// </summary> public Customer() { } /// <summary> /// Initializes a new instance of the <see cref="Customer{T}"/> class. /// </summary> public Customer() { } |
√
|
SA1644: Documentation Headers Must Not Contain Blank Lines | 註釋頭部不能包含空白行 /// <summary> /// <para> /// Joins a first name and a last name together into a single string. /// </para><para> /// Uses a simple form of string concatenation. /// </para> /// </summary> /// <param name="firstName">The first name to join.</param> /// <param name="lastName">The last name to join.</param> /// <returns>The joined names.</returns> public string JoinNames(string firstName, string lastName) { return firstName + " " + lastName; } |
√
|
SA1645: Included Documentation File Does Not Exist | 導入的文檔註釋文件不存在 ///<include file="IncludedDocumentation.xml" path="root/EnabledMethodDocs"/> public bool Enabled(bool true) { } |
√
|
SA1646: Included Documentation XPath Does Not Exist | 導入的文檔註釋的XPath不存在 ///<include file="IncludedDocumentation.xml" path="root/EnabledMethodDocs"/> public bool Enabled(bool true) { } |
√
|
SA1647: Include Node Does Not Contain Valid FileAnd Path | 導入的結點不存在正確的文件和路徑 ///<include file="IncludedDocumentation.xml" path="root/EnabledMethodDocs"/> public bool Enabled(bool true) { } |
√
|
8.4 佈局規則(LayoutRules)
標識及提示 | 中文及示例 | 是否檢查 |
SA1500: CurlyBracketsForMultiLineStatementsMustNotShareLine | 當多行代碼使用{}時"{"與"}"必須單獨佔一行 |
√
|
SA1501: StatementMustNotBeOnSingleLine | {}不能與代碼寫在同一行 |
√
|
SA1502: ElementMustNotBeOnSingleLine | 元素定義不能在同一行 |
√
|
SA1503:CurlyBracketsMustNotBeOmitted | {}符號不能被省略 |
√
|
SA1504: AllAccessorMustBeMultiLineOrSingleLine | 全部的訪問器代碼,必須是多行或一行;(不能Get多行,Set單行) |
√
|
SA1505:OpeningCurlyBracketsMustNotBeFollowedByBlankLine | { 符號下面不能跟空行 |
√
|
SA1506:ElementDocumentationHeadersMustNotBeFollowedByBlankLine | 元素註釋頭部不能跟空行 |
√
|
SA1507: CodeMustNotContainMultipleBlankLinesInARow | 代碼不能包含連續的多個空行 |
√
|
SA1508: ClosingCurlyBracketsMustNotBePrecededByBlankLine | } 符號上方不能有空行 |
√
|
SA1509: OpeningCurlyBracketsMustNotBePrecedededByBlankLine | { 符號與代碼之間不能有空行 |
√
|
SA1510: ChainedStatementBlocksMustNotBePrecededByBlankLine | 使用{} 符號而且鏈接的代碼之間不能有空行 |
√
|
SA1511: WhileDoFooterMustNotBePrecededByBlankLine | Do While 代碼之間不能有空行 |
√
|
SA1512: SingleLineCommentsMustNotBeFollowedByBlankLine | 單行註釋與代碼之間不能有空行 |
√
|
SA1513: ClosingCurlyBracketMustBeFollowedByBlankLine | } 符號後面必須有空行 |
√
|
SA1514: ElementDocumentationHeaderMustBePrecededByBlankLine | 元素註釋頭部必須加空行 |
√
|
SA1515: SingleLineCommentMustBePrecededByBlankLine | 單行註釋前面必須加空行 |
√
|
SA1516: ElementsMustBeSeparatedByBlankLine | 元素必須用空行分隔 |
√
|
8.5 可維護性規則(Maintanability Rules)」
標識及提示 | 中文及示例 | 是否檢查 |
SA1300:ElementMustBeginWithUpperCaseLetter | 元素必須首字母大寫 | √ |
SA1301:ElementMustBeginWithLowerCaseLetter | 元素必須首字母小寫 | √ |
SA1302: InterfaceNamesMustBeginWithI | 命名接口必須以I開頭 | √ |
SA1303:ConstFieldNamesMustBeginWithUpperCaseLetter | 常量字段命名必須首字母大寫 | √ |
SA1304:NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter | 非私有隻讀字段必須首字母大寫 | √ |
SA1305:FieldNamesMustNotUseHungarianNotation | 字段名不能使用匈牙利命名法 | √ |
SA1306:FieldNamesMustBeginWithLowerCaseLetter | 字段名必須首字母小寫 | √ |
SA1307: AccessibleFieldsMustBeginWithUpperCaseLetter | 可訪問字段必須首字母大寫 | √ |
SA1308: VariableNamesMustNotBePrefixed | 變量名不能加前綴 | √ |
SA1309: FieldNamesMustNotBeginWithUnderscore | 字段名不能以"_"開頭 | √ |
SA1310:FieldNamesMustNotContainUnderscore | 字段名不能包含"_" | √ |
使用Visual Studio自身的代碼分析功能,檢查內容以下表,分爲
其中「是否檢查」一項中爲「√」的內容不能違反。需在Visual Studio中設置爲錯誤
9.1.安全性規則
標識 | 詳細信息 | 是否檢查 |
CA2100 | 檢查Sql查詢中是否有安全漏洞 | √ |
CA2104 | 不要聲明只讀可變引用類型 | √ |
CA2105 | 數組字段不該爲只讀 | √ |
CA2121 | 靜態構造函數應爲私有 | √ |
9.2.可靠性規則
標識 | 詳細信息 | 是否檢查 |
CA2000 | 超出範圍前釋放對象 | √ |
9.3.可維護性規則
標識 | 詳細信息 | 是否檢查 |
CA1500 | 變量名不該與字段名相同 | √ |
CA1501 | 避免過分繼承 | √ |
CA1502 | 避免過分複雜 | √ |
9.4.命名規則
標識 | 詳細信息 | 是否檢查 |
CA1700 | 不要將枚舉值命名爲「Reserved」 | √ |
CA1705 | 較長的首字母縮略詞應採用Pascal大小寫格式。 | √ |
CA1706 | 較短的首字母縮略詞應所有大寫 | √ |
CA1707 | 標識符不該包含下劃線 | √ |
CA1709 | 標識符的大小寫應該正確 | √ |
CA1710 | 標識符應具備正確的後綴 | √ |
CA1711 | 標識符應採用正確的後綴 | √ |
CA1712 | 不要將類型名用做枚舉值的前綴 | √ |
CA1713 | 事件不該具備 before 或 after 前綴 | √ |
CA1715 | 標識符應具備正確的前綴 | √ |
CA1716 | 標識符不該與關鍵字衝突 | √ |
CA1718 | 避免在參數中使用特定於語言的類型名 | √ |
CA1719 | 參數名不該與成員名衝突 | √ |
CA1720 | 標識符不該包含類型名 | √ |
CA1721 | 屬性名不該與 get 方法衝突 | √ |
CA1722 | 標識符應採用正確的前綴 | √ |
CA1724 | 類型名不該與命名空間衝突 | √ |
CA1725 | 參數名應與基方法中的聲明保持一致 | √ |
9.5.性能規則
標識 | 詳細信息 | 是否檢查 |
CA1800 | 避免進行沒必要要的強制轉換 | √ |
CA1804 | 移除未使用的局部變量 | √ |
CA1805 | 避免進行沒必要要的初始化 | √ |
CA1809 | 避免過多的局部變量 | √ |
CA1812 | 避免未實例化的內部類 | √ |
CA1813 | 避免未密封的屬性 | √ |
CA1819 | 屬性不該返回數組 | √ |
CA1823 | 避免未使用的私有字段 | √ |
9.6.用法規則
標識 | 詳細信息 | 是否檢查 |
CA1801 | 檢查未使用的參數 | √ |
CA2202 | 不要屢次釋放對象 | √ |
CA2211 | 很是數字段不該該是可見的 | √ |
CA2218 | 重寫 Equals 時重寫 GetHashCode | √ |
CA2219 | 不要在異常子句中引起異常 | √ |
CA2222 | 不要下降繼承成員的可見性 | √ |
CA2230 | 對個數可變的參數使用 params | √ |
CA2233 | 運算不該溢出 | √ |