.NET代码编写规范 整理
.NET代碼編寫規范 - [ASP.NET]
2009-02-26 | Tag:
版權聲明:轉載時請以超鏈接形式標明文章原始出處和作者信息及本聲明
http://lenspe.blogbus.com/logs/35762383.html
下面是根據FxCop整理的.NET代碼編寫規范,僅供參考。
一、 Design(設計)
1. Abstract types should not have constructors?
??????? 抽象類不應該聲明構造方法
2. Assemblies should have valid strong names?
??????? 程序集應該具有強名稱
3. Avoid empty interfaces?
??????? 避免使用空的接口
4. Avoid excessive parameters on generic types?
??????? 避免在泛型類中使用過多的類型參數
5. Avoid namespaces with few types?
??????? 避免讓名字空間含有過少的類型
6. Avoid out parameters?
??????? 避免使用 out類型的參數
7. Collections should implement generic interface?
??????? 集合類應該實現泛型接口
8. Consider passing base types as parameters?
??????? 盡量使用基本類型作為參數
9. Declare event handlers correctly?
??????? 正確的聲明事件處理器,事件處理器不應該具有返回值
10. Declare types in namespaces?
??????? 應該在名字空間里面定義類型,而不是外面
11. Default parameters should not be used?
??????? 不應該使用參數默認值(C#沒有參數默認值)
12. Define accessors for attribute arguments?
??????? 應該為特性(特性)的構造方法參數定義訪問器,其名字跟構造方法參數僅首字母大小寫不一樣
13. Do not catch general exception types?
??????? 不要捕捉普通的異常(即System.Exception)
14. Do not declare protected members in sealed types?
??????? 不要在封閉類型中定義受保護的成員
15. Do not declare static members on generic types?
??????? 不要在泛型類型中使用靜態成員
16. Do not declare virtual members in sealed types?
??????? 不要在封閉類型中定義虛成員
17. Do not declare visible instance fields?
??????? 不要定義可見的(public/internal)實例域變量
18. Do not expose generic lists?
??????? 不要直接暴露范型表
19. Do not hide base class methods
??????? 不要隱藏(使用或者不使用new)基類的方法
20. Do not nest generic types in member signatures?
??????? 不要在成員的簽名(參數或者返回值)中嵌套泛型類
21. Do not override operator equals on reference types?
??????? 不要在引用類型中重載==操作符
22. Do not pass types by reference?
??????? 不要使用引用(ref or out)傳遞類型
23. Enum Storage should be Int32?
??????? 枚舉應該是 Int32 類型的
24. Enumerators should be strongly typed?
??????? 枚舉器應該是強類型的
25. Enums should have zero value
??????? 枚舉應該具有0值
26. Generic methods should provide type parameter?
??????? 泛型類的方法應該提供類型參數
27. ICollection implementations have strongly typed members?
??????? 集合接口的實現中應該使用強類型的成員
28. Implement standard exception constructors
??????? 自定義的異常應該實現異常類的四個標準構造方法
29. Indexers should not be multidimensional?
??????? 索引不應該是多維的
30. Interface methods should be callable by child types
??????? 接口方法應該可以被子類調用
31. Lists are strongly typed
??????? 表應該是強類型的
32. Mark assemblies with assembly version
??????? 用程序集版本標示程序集
33. Mark assemblies with CLSCompliant
??????? 使用CLSCompliant特性標示程序集
34. Mark assemblies with ComVisible?
??????? 使用 System.Runtime.InteropServices.ComVisibleAttribute 特性標示程序集
35. Mark attributes with AttributeUsageAttribute
??????? 使用 AttributeUsageAttribute 特性標示特性類
36. Mark enums with FlagsAttribute
??????? 含有組合的枚舉應該使用FlagsAttribute特性標示,相反則不應該
37. Members should not expose certain concrete types?
??????? 成員(返回值或者參數)不應該暴露具體類型,盡量使用接口
38. Move pinvokes to native methods class
??????? 將調用移到本地方法類(不是很理解)
39. Nested types should not be visible?
??????? 嵌套類型不應該是可見的
40. Override methods on comparable types
??????? 可比較類型應該重寫 equals 等方法
41. Override operator equals on overriding add and subtract?
??????? 在重寫+和-運算的時候應該同時重寫==操作符
42. Properties should not be write only
??????? 屬性不應該是只寫的
43. Provide ObsoleteAttribute message?
??????? 過時的成員應該使用ObsoleteAttribute特性標示,并提供相應的Message提示使用者
44. Replace repetitive arguments with params array
??????? 使用參數數組代替重復的參數
45. Static holder types should be sealed?
??????? 僅含有靜態成員的類型應該聲明為封閉的
46. Static holder types should not have constructors
??????? 僅含有靜態成員的類型應該具有構造方法
47. String uri overloads call system uri overloads
??????? 使用string類型的uri參數的重載應調用系統的使用URI類型參數的重載
48. Types should not extend certain base types
??????? 類型不應該從具體的類(已經過派生的類)繼承,比如異常類不應該從ApplicationException繼承,而應該從System.Exception繼承
49. Types that own disposable fields should be disposable?
??????? 含有可釋放成員的類型應該是可以釋放的(實現IDisposable接口)
50. Types that own native resources should be disposable
??????? 使用了非托管資源的類型應該是可以釋放的(實現IDisposable接口)
51. Uri parameters should not be strings?
??????? Uri 參數不應該是string類型的
52. Uri properties should not be strings?
??????? Uri 屬性不應該是string類型的
53. Uri return values should not be strings?
??????? Uri 類型的返回值不應該是string類型的
54. Use events where appropriate
??????? 在適當的時候使用事件
55. Use generic event handler instances?
??????? 使用泛型的事件處理器實例
56. Use generics where appropriate?
??????? 在適當的時候使用范型
57. Use integral or string argument for indexers?
??????? 索引器應該使用整數或者字符串類型的參數
58. Use properties where appropriate?
??????? 在適當的時候使用屬性(而不是以Get或者Set開頭的方法)
59. Validate arguments of public methods
??????? 對public的方法的參數應該在方法開頭處進行檢驗(比如是否為null的檢驗)
二、 Globalization(全球化)
1. Avoid duplicate accelerators
??????? 避免在頂層控件中使用重復的快捷鍵(加速鍵)
2. Do not hardcode locale specific strings?
??????? 不要對本地的特殊字符串(比如特殊的系統路徑)進行硬編碼
3. Do not pass literals as localized parameters?
??????? 不要把文本作為需要本地化的參數直接傳遞(盡量使用資源文件)
4. Set locale for data types
??????? 為某些數據類型設定區域和語言屬性(DataSet和DataTable的locale屬性)
5. Specify CultureInfo?
??????? 指定文化信息(地域和語言信息),在調用接受System.Globalization.CultureInfo 類型參數的方法時應該傳遞文化信息
6. Specify IFormatProvider?
??????? 指定格式供應器,在調用接受System.IFormatProvider 類型參數的方法時應該傳遞格式供應器
7. Specify MessageBoxOptions
??????? 指定MessageBox的選項,在調用MessageBox.Show方法時應該傳遞System.Windows.Forms.MessageBoxOptions,特別在某些從右向左閱讀習慣的區域
三、 Interoperability(互操作性)
1. Auto layout types should not be ComVisible
??????? 自動布局的類型不應該對Com可見(設置System.Runtime.InteropServices.ComVisibleAttribute特性為false)
2. Avoid int64 arguments for VB6 clients?
??????? 避免使用int64類型,如果成員可能被Visual Basic 6 COM clients調用
3. Avoid non-public fields in ComVisible value types
??????? 避免在一個標記有ComVisible特性的值類型里面包含非公有的實例域
4. Avoid overloads in ComVisible interfaces?
??????? 避免在一個標記有ComVisible特性的接口內聲明重載
5. Avoid static members in ComVisible types?
??????? 避免在一個標記有ComVisible特性的類型
6. Call GetLastError immediately after pinvoke?
??????? 進行pinvoke以后應該立即使用GetLastError讀取錯誤信息
7. Com registration methods should be matched?
??????? Com注冊方法(標記有System.Runtime.InteropServices.ComRegisterFunctionAttribute特性的方法)應該是配對的(同時具有一個標記有 System.Runtime.InteropServices.ComUnregisterFunctionAttribute的方法與之匹配)
8. Com registration methods should not be visible?
??????? Com注冊方法應該是不可見的
9. Com visible type base types should be ComVisible?
??????? 標記有ComVisible特性的類型的基類同樣應從標記有ComVisible特性的類繼承
10. Com visible types should be creatable?
??????? 標記有ComVisible特性的類型應該能夠使用默認構造器構造
11. Declare PInvokes correctly?
??????? 正確定義PInvokes
12. Do not use AutoDual ClassInterfaceType?
??????? 不要把System.Runtime.InteropServices.ClassInterfaceAttribute特性的值設置為 System.Runtime.InteropServices.ClassInterfaceType.AutoDual
13. Mark boolean pinvoke arguments with MarshalAs?
??????? 布爾型的pinvoke參數應該使用System.Runtime.InteropServices.MarshalAsAttribute特性標記
14. Mark ComSource interfaces as IDispatch?
??????? 將System.Runtime.InteropServices.ComSourceInterfacesAttribute特性標記為 System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch.
15. PInvoke entry points should exist?
??????? Pinvoke應該存在入口點
16. PInvokes should not be visible
??????? Pinvoke應該是可見的
四、 Naming(命名)
1. Avoid language specific type names in parameters?
??????? 避免在參數中使用與特定語言相關的類型(用Uint16代替Ushort)
2. Avoid type names in parameters?
??????? 避免在外部可見的參數中使用類型名
3. Compound words should be cased correctly?
??????? 復合詞應該使用正確的大小寫(不要將Mutlipart寫成MultiPart,也不要將ToolBar寫成Toolbar,FileName寫成Filename)
4. Do not name enum values 'Reserved'?
??????? 不要在枚舉值中使用保留字
5. Do not prefix enum values with type name?
??????? 不要在枚舉值使用類型前綴(比如不要使用Digital之類的前綴)
6. Events should not have before or after prefix?
??????? 事件的名稱不應該包含before和after前綴(盡量使用ing和ed的后綴)
7. Flags enums should have plural names?
??????? 標記有System.FlagsAttribute特性的枚舉應該使用復數形式的名稱
8. Identifiers should be cased correctly?
??????? 標示符(名字空間、類名、屬性名、接口名、方法名等)應該使用正確的大小寫(通常以大寫開頭,以后的每個單詞都首字母大寫)
9. Identifiers should be spelled correctly?
??????? 標示符應該可以被正確的劃分為不同的單詞
10. Identifiers should differ by more than case?
??????? 標示符應該不止有大小寫上的不同(因為某些語言是不區分大小寫的)
11. Identifiers should have correct prefix?
??????? 標示符應該使用正確的前綴(接口應該使用字母I開頭)
12. Identifiers should have correct suffix?
??????? 標示符應該使用正確的后綴
System.Attribute/Attribute
System.EventArgs/EventArgs
System.Exception/Exception
System.Collections.ICollection/Collection
System.Collections.IDictionary/Dictionary
System.Collections.IEnumerable/Collection
System.Collections.Queue/Collection or Queue
System.Collections.Stack/Collection or Stack
System.Collections.Generic.ICollection/Collection
System.Collections.Generic.IDictionary/Dictionary
System.Data.DataSet/DataSet
System.Data.DataTable/Collection or DataTable
System.IO.Stream/Stream
System.Security.IPermission/Permission
System.Security.Policy.IMembershipCondition/Condition
An event-handlerdelegate./EventHandler
13. Identifiers should not contain underscores?
??????? 標示符不應該使用下劃線
14. Identifiers should not have incorrect prefix?
??????? 標示符不應該使用不正確的前綴(比如不應使用一個字母作為前綴)
15. Identifiers should not have incorrect suffix?
??????? 標示符不應該使用不正確的后綴(不要在不正確的地方使用12中提及的后綴名,和Delegate、Enum、Flags for an enumeration、Impl等后綴名)
16. Identifiers should not match keywords?
??????? 標示符不應該與系統關鍵字沖突
17. Long acronyms should be pascal-cased?
??????? 長度大于等于3的縮寫詞應該使用pascal的命名規則,即首字母大寫
18. Only FlagsAttribute enums should have plural names?
??????? 只有標記有System.FlagsAttribute特性的枚舉的名稱才應該使用復數,其他時候應該使用單數
19. Parameter names should match base declaration?
??????? 派生項的參數名應該同基項相吻合(派生類重寫或實現的方法應該同基項具有相同的參數名)
20. Parameter names should not match member names?
??????? 方法的參數名不應該同類或接口的成員名一樣
21. Property names should not match get methods?
??????? 屬性名字不應該同Get開頭的方法的名稱的后半部分相同
22. Resource string compound words should be cased correctly?
??????? 包含符合單詞的資源字符串應該使用正確的大小寫(每個單詞的首字母大寫)
23. Resource strings should be spelled correctly?
??????? 資源字符串應該正確的拼寫
24. Short acronyms should be uppercase?
??????? 短的首字母縮寫詞應該全部大寫(比如DB,CR)
25. Type names should not match namespaces
??????? 類型的名字不應該與名字空間的名字相同
26. Use preferred terms
??????? 優先使用某些項目或者名稱,以下這些,后者為優先使用的
ComPlus/EnterpriseServices
Cancelled/Canceled
Indices/Indexes
LogIn/LogOn
LogOut/LogOff
SignOn/SignIn
SignOff/SignOut
Writeable/Writable
五、 Performance(性能規則)
1. Avoid calls that require unboxing?
??????? 避免調用一個方法,它返回object類型,而你需要的是一個值類型(需要對返回值進行拆箱操作)
2. Avoid costly calls where possible?
??????? 盡可能的避免進行代價高昂的調用
3. Avoid excessive locals
??????? 避免使用過多的局部變量(多于64個,部分可能是編譯器生成的)
4. Avoid uncalled private code
??????? 避免聲明在程序集內從來未被調用的私有成員(private和internal),以下除外:
??????? 明確的接口成員
??????? 靜態構造方法
??????? 靜態的Main方法(不含參數或僅包含一個string數組的參數的)
序列化構造方法
??????? 標記有System.Runtime.InteropServices.ComRegisterFunctionAttribute或者 System.Runtime.InteropServices.ComUnregisterFunctionAttribute.特性的
重寫的方法
5. Avoid uninstantiated internal classes
??????? 避免聲明不會被實例化的內部類,以下情況除外
??????? 值類型
??????? 抽象類型
??????? 枚舉
??????? 委托
??????? 編譯器生成的數組類型
??????? 僅含有靜態成員的內部類
6. Avoid unnecessary string creation?
??????? 避免創建不必要的string實例(猶指‘通過ToLower和ToUpper創建的string’),含以下情況
??????? 對于同一個string實例多次調用ToLower和ToUpper(建議:將返回值賦給一個局部變量,然后使用此局部變量)
??????? 使用equals,’==‘,!=比較‘通過ToLower和ToUpper創建的string’(建議:使用String.Compare比較)
??????? 向一個System.Collections.Specialized.HybridDictionary類型的成員傳遞‘通過ToLower和 ToUpper創建的string’(建議:HybridDictionary具有一個指示是否忽略大小寫的參數的構造方法重載,使用此重載并傳遞一個 true值進去)
7. Avoid unsealed attributes?
??????? 避免聲明未封閉的特性(attributes)(建議:聲明為sealed/ NotInheritable-vb.net或者abstract)
8. Avoid unused parameters
??????? 避免在方法聲明中包含不會被使用的參數,以下情況除外
??????? 代理引用的方法
??????? 作為事件處理程序的方法
??????? 抽象方法(abstract)
??????? 虛方法(virtual)
??????? 重寫的方法(override)
??????? 外部方法(extern)
9. Dispose methods should call SuppressFinalize
??????? Dispose方法應該調用SuppressFinalize,以請求系統不要調用其Finalize方法
10. Do not call properties that clone values in loops?
??????? 不要在循環中使用‘返回一個Clone的對象的屬性’(每次返回‘引用不同’的對象,會導致創建大量的相同的對象)
11. Do not cast unnecessarily?
??????? 不要進行不必要的類型轉換(特別是嘗試性的轉換,建議:在轉換前可以使用is操作符來判斷轉換能夠成功)
12. Do not concatenate strings inside loops?
??????? 不要在循環內串聯string(建議:使用StringBuilder代替string)
13. Do not ignore method results?
??????? 不要忽略方法的返回值(通常調用string的方法會返回新的string)
14. Do not initialize unnecessarily
??????? 不要進行不必要的初始化(比如將類成員初始化為它的默認值)
15. Initialize reference type static fields inline
??????? 在靜態成員聲明的時候直接初始化或者調用靜態方法初始化(不要使用靜態構造方法來初始化靜態成員,靜態構造方法會影響性能),以下情況除外:
??????? 初始化對全局狀態的影響是代價高昂的,而且類型在使用前不需要進行初始化的
??????? 在不需要訪問該類型的靜態成員的情況下,全局狀態的影響就可以被訪問到的
16. Override equals and operator equals on value types
??????? 對于公有的值類型,重寫equals方法和’==‘操作符(如果你期望用戶對實例進行比較或者排序,或者作為哈希表的鍵)
17. Prefer jagged arrays over multidimensional?
??????? 使用鋸齒形數組代替多維數組(當數組各元素的長度可能不一致時)
??????? 注意:公共語言規范(CLS)不支持鋸齒數組
18. Properties should not return arrays?
??????? 公有類型的屬性不應該返回數組(數組類型的屬性無法進行寫保護,即使是只讀的,除非每次返回不同的拷貝,但是這樣會讓調用者產生迷惑。建議:改成方法)
19. Remove empty finalizers
??????? 移除空的終結器
20. Remove unused locals
??????? 移除未使用過的局部變量
21. Test for empty strings using string length
??????? 使用length屬性測試字符串是否為空(原因:不要使用==””、==String..Empty、Equals(“”)等方法,使用Length屬性的效率是最高的;null==empty比較不會拋出異常;在DotNetFrameWork2里面可以使用IsNullOrEmpty方法來判斷字符串是否為null或者empty)
22. Use literals where appropriate
??????? 在適當的時候使用const代替static readonly(const是編譯時賦值的
轉載于:https://www.cnblogs.com/yaoshiyou/archive/2010/05/11/1732987.html
總結
以上是生活随笔為你收集整理的.NET代码编写规范 整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fckeditor配置 for ASP.
- 下一篇: WF4.0 应用篇(四) IActivi