日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

.NET代码编写规范 整理

發(fā)布時(shí)間:2024/4/17 asp.net 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .NET代码编写规范 整理 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

.NET代碼編寫規(guī)范 - [ASP.NET]

2009-02-26 | Tag:

版權(quán)聲明:轉(zhuǎn)載時(shí)請(qǐng)以超鏈接形式標(biāo)明文章原始出處和作者信息及本聲明
http://lenspe.blogbus.com/logs/35762383.html

下面是根據(jù)FxCop整理的.NET代碼編寫規(guī)范,僅供參考。
一、 Design(設(shè)計(jì))
1. Abstract types should not have constructors?
??????? 抽象類不應(yīng)該聲明構(gòu)造方法
2. Assemblies should have valid strong names?
??????? 程序集應(yīng)該具有強(qiáng)名稱
3. Avoid empty interfaces?
??????? 避免使用空的接口
4. Avoid excessive parameters on generic types?
??????? 避免在泛型類中使用過(guò)多的類型參數(shù)
5. Avoid namespaces with few types?
??????? 避免讓名字空間含有過(guò)少的類型
6. Avoid out parameters?
??????? 避免使用 out類型的參數(shù)
7. Collections should implement generic interface?
??????? 集合類應(yīng)該實(shí)現(xiàn)泛型接口
8. Consider passing base types as parameters?
??????? 盡量使用基本類型作為參數(shù)
9. Declare event handlers correctly?
??????? 正確的聲明事件處理器,事件處理器不應(yīng)該具有返回值
10. Declare types in namespaces?
??????? 應(yīng)該在名字空間里面定義類型,而不是外面
11. Default parameters should not be used?
??????? 不應(yīng)該使用參數(shù)默認(rèn)值(C#沒(méi)有參數(shù)默認(rèn)值)
12. Define accessors for attribute arguments?
??????? 應(yīng)該為特性(特性)的構(gòu)造方法參數(shù)定義訪問(wèn)器,其名字跟構(gòu)造方法參數(shù)僅首字母大小寫不一樣
13. Do not catch general exception types?
??????? 不要捕捉普通的異常(即System.Exception)
14. Do not declare protected members in sealed types?
??????? 不要在封閉類型中定義受保護(hù)的成員
15. Do not declare static members on generic types?
??????? 不要在泛型類型中使用靜態(tài)成員
16. Do not declare virtual members in sealed types?
??????? 不要在封閉類型中定義虛成員
17. Do not declare visible instance fields?
??????? 不要定義可見(jiàn)的(public/internal)實(shí)例域變量
18. Do not expose generic lists?
??????? 不要直接暴露范型表
19. Do not hide base class methods
??????? 不要隱藏(使用或者不使用new)基類的方法
20. Do not nest generic types in member signatures?
??????? 不要在成員的簽名(參數(shù)或者返回值)中嵌套泛型類
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?
??????? 枚舉應(yīng)該是 Int32 類型的
24. Enumerators should be strongly typed?
??????? 枚舉器應(yīng)該是強(qiáng)類型的
25. Enums should have zero value
??????? 枚舉應(yīng)該具有0值
26. Generic methods should provide type parameter?
??????? 泛型類的方法應(yīng)該提供類型參數(shù)
27. ICollection implementations have strongly typed members?
??????? 集合接口的實(shí)現(xiàn)中應(yīng)該使用強(qiáng)類型的成員
28. Implement standard exception constructors
??????? 自定義的異常應(yīng)該實(shí)現(xiàn)異常類的四個(gè)標(biāo)準(zhǔn)構(gòu)造方法
29. Indexers should not be multidimensional?
??????? 索引不應(yīng)該是多維的
30. Interface methods should be callable by child types
??????? 接口方法應(yīng)該可以被子類調(diào)用
31. Lists are strongly typed
??????? 表應(yīng)該是強(qiáng)類型的
32. Mark assemblies with assembly version
??????? 用程序集版本標(biāo)示程序集
33. Mark assemblies with CLSCompliant
??????? 使用CLSCompliant特性標(biāo)示程序集
34. Mark assemblies with ComVisible?
??????? 使用 System.Runtime.InteropServices.ComVisibleAttribute 特性標(biāo)示程序集
35. Mark attributes with AttributeUsageAttribute
??????? 使用 AttributeUsageAttribute 特性標(biāo)示特性類
36. Mark enums with FlagsAttribute
??????? 含有組合的枚舉應(yīng)該使用FlagsAttribute特性標(biāo)示,相反則不應(yīng)該
37. Members should not expose certain concrete types?
??????? 成員(返回值或者參數(shù))不應(yīng)該暴露具體類型,盡量使用接口
38. Move pinvokes to native methods class
??????? 將調(diào)用移到本地方法類(不是很理解)
39. Nested types should not be visible?
??????? 嵌套類型不應(yīng)該是可見(jiàn)的
40. Override methods on comparable types
??????? 可比較類型應(yīng)該重寫 equals 等方法
41. Override operator equals on overriding add and subtract?
??????? 在重寫+和-運(yùn)算的時(shí)候應(yīng)該同時(shí)重寫==操作符
42. Properties should not be write only
??????? 屬性不應(yīng)該是只寫的
43. Provide ObsoleteAttribute message?
??????? 過(guò)時(shí)的成員應(yīng)該使用ObsoleteAttribute特性標(biāo)示,并提供相應(yīng)的Message提示使用者
44. Replace repetitive arguments with params array
??????? 使用參數(shù)數(shù)組代替重復(fù)的參數(shù)
45. Static holder types should be sealed?
??????? 僅含有靜態(tài)成員的類型應(yīng)該聲明為封閉的
46. Static holder types should not have constructors
??????? 僅含有靜態(tài)成員的類型應(yīng)該具有構(gòu)造方法
47. String uri overloads call system uri overloads
??????? 使用string類型的uri參數(shù)的重載應(yīng)調(diào)用系統(tǒng)的使用URI類型參數(shù)的重載
48. Types should not extend certain base types
??????? 類型不應(yīng)該從具體的類(已經(jīng)過(guò)派生的類)繼承,比如異常類不應(yīng)該從ApplicationException繼承,而應(yīng)該從System.Exception繼承
49. Types that own disposable fields should be disposable?
??????? 含有可釋放成員的類型應(yīng)該是可以釋放的(實(shí)現(xiàn)IDisposable接口)
50. Types that own native resources should be disposable
??????? 使用了非托管資源的類型應(yīng)該是可以釋放的(實(shí)現(xiàn)IDisposable接口)
51. Uri parameters should not be strings?
??????? Uri 參數(shù)不應(yīng)該是string類型的
52. Uri properties should not be strings?
??????? Uri 屬性不應(yīng)該是string類型的
53. Uri return values should not be strings?
??????? Uri 類型的返回值不應(yīng)該是string類型的
54. Use events where appropriate
??????? 在適當(dāng)?shù)臅r(shí)候使用事件
55. Use generic event handler instances?
??????? 使用泛型的事件處理器實(shí)例
56. Use generics where appropriate?
??????? 在適當(dāng)?shù)臅r(shí)候使用范型
57. Use integral or string argument for indexers?
??????? 索引器應(yīng)該使用整數(shù)或者字符串類型的參數(shù)
58. Use properties where appropriate?
??????? 在適當(dāng)?shù)臅r(shí)候使用屬性(而不是以Get或者Set開(kāi)頭的方法)
59. Validate arguments of public methods
??????? 對(duì)public的方法的參數(shù)應(yīng)該在方法開(kāi)頭處進(jìn)行檢驗(yàn)(比如是否為null的檢驗(yàn))

二、 Globalization(全球化)
1. Avoid duplicate accelerators
??????? 避免在頂層控件中使用重復(fù)的快捷鍵(加速鍵)
2. Do not hardcode locale specific strings?
??????? 不要對(duì)本地的特殊字符串(比如特殊的系統(tǒng)路徑)進(jìn)行硬編碼
3. Do not pass literals as localized parameters?
??????? 不要把文本作為需要本地化的參數(shù)直接傳遞(盡量使用資源文件)
4. Set locale for data types
??????? 為某些數(shù)據(jù)類型設(shè)定區(qū)域和語(yǔ)言屬性(DataSet和DataTable的locale屬性)
5. Specify CultureInfo?
??????? 指定文化信息(地域和語(yǔ)言信息),在調(diào)用接受System.Globalization.CultureInfo 類型參數(shù)的方法時(shí)應(yīng)該傳遞文化信息
6. Specify IFormatProvider?
??????? 指定格式供應(yīng)器,在調(diào)用接受System.IFormatProvider 類型參數(shù)的方法時(shí)應(yīng)該傳遞格式供應(yīng)器
7. Specify MessageBoxOptions
??????? 指定MessageBox的選項(xiàng),在調(diào)用MessageBox.Show方法時(shí)應(yīng)該傳遞System.Windows.Forms.MessageBoxOptions,特別在某些從右向左閱讀習(xí)慣的區(qū)域

三、 Interoperability(互操作性)
1. Auto layout types should not be ComVisible
??????? 自動(dòng)布局的類型不應(yīng)該對(duì)Com可見(jiàn)(設(shè)置System.Runtime.InteropServices.ComVisibleAttribute特性為false)
2. Avoid int64 arguments for VB6 clients?
??????? 避免使用int64類型,如果成員可能被Visual Basic 6 COM clients調(diào)用
3. Avoid non-public fields in ComVisible value types
??????? 避免在一個(gè)標(biāo)記有ComVisible特性的值類型里面包含非公有的實(shí)例域

4. Avoid overloads in ComVisible interfaces?
??????? 避免在一個(gè)標(biāo)記有ComVisible特性的接口內(nèi)聲明重載
5. Avoid static members in ComVisible types?
??????? 避免在一個(gè)標(biāo)記有ComVisible特性的類型
6. Call GetLastError immediately after pinvoke?
??????? 進(jìn)行pinvoke以后應(yīng)該立即使用GetLastError讀取錯(cuò)誤信息
7. Com registration methods should be matched?
??????? Com注冊(cè)方法(標(biāo)記有System.Runtime.InteropServices.ComRegisterFunctionAttribute特性的方法)應(yīng)該是配對(duì)的(同時(shí)具有一個(gè)標(biāo)記有 System.Runtime.InteropServices.ComUnregisterFunctionAttribute的方法與之匹配)
8. Com registration methods should not be visible?
??????? Com注冊(cè)方法應(yīng)該是不可見(jiàn)的
9. Com visible type base types should be ComVisible?
??????? 標(biāo)記有ComVisible特性的類型的基類同樣應(yīng)從標(biāo)記有ComVisible特性的類繼承
10. Com visible types should be creatable?
??????? 標(biāo)記有ComVisible特性的類型應(yīng)該能夠使用默認(rèn)構(gòu)造器構(gòu)造
11. Declare PInvokes correctly?
??????? 正確定義PInvokes
12. Do not use AutoDual ClassInterfaceType?
??????? 不要把System.Runtime.InteropServices.ClassInterfaceAttribute特性的值設(shè)置為 System.Runtime.InteropServices.ClassInterfaceType.AutoDual
13. Mark boolean pinvoke arguments with MarshalAs?
??????? 布爾型的pinvoke參數(shù)應(yīng)該使用System.Runtime.InteropServices.MarshalAsAttribute特性標(biāo)記
14. Mark ComSource interfaces as IDispatch?
??????? 將System.Runtime.InteropServices.ComSourceInterfacesAttribute特性標(biāo)記為 System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch.
15. PInvoke entry points should exist?
??????? Pinvoke應(yīng)該存在入口點(diǎn)
16. PInvokes should not be visible
??????? Pinvoke應(yīng)該是可見(jiàn)的
四、 Naming(命名)
1. Avoid language specific type names in parameters?
??????? 避免在參數(shù)中使用與特定語(yǔ)言相關(guān)的類型(用Uint16代替Ushort)
2. Avoid type names in parameters?
??????? 避免在外部可見(jiàn)的參數(shù)中使用類型名
3. Compound words should be cased correctly?
??????? 復(fù)合詞應(yīng)該使用正確的大小寫(不要將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?
??????? 事件的名稱不應(yīng)該包含before和after前綴(盡量使用ing和ed的后綴)
7. Flags enums should have plural names?
??????? 標(biāo)記有System.FlagsAttribute特性的枚舉應(yīng)該使用復(fù)數(shù)形式的名稱
8. Identifiers should be cased correctly?
??????? 標(biāo)示符(名字空間、類名、屬性名、接口名、方法名等)應(yīng)該使用正確的大小寫(通常以大寫開(kāi)頭,以后的每個(gè)單詞都首字母大寫)
9. Identifiers should be spelled correctly?
??????? 標(biāo)示符應(yīng)該可以被正確的劃分為不同的單詞
10. Identifiers should differ by more than case?
??????? 標(biāo)示符應(yīng)該不止有大小寫上的不同(因?yàn)槟承┱Z(yǔ)言是不區(qū)分大小寫的)
11. Identifiers should have correct prefix?
??????? 標(biāo)示符應(yīng)該使用正確的前綴(接口應(yīng)該使用字母I開(kāi)頭)
12. Identifiers should have correct suffix?
??????? 標(biāo)示符應(yīng)該使用正確的后綴
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?
??????? 標(biāo)示符不應(yīng)該使用下劃線
14. Identifiers should not have incorrect prefix?
??????? 標(biāo)示符不應(yīng)該使用不正確的前綴(比如不應(yīng)使用一個(gè)字母作為前綴)
15. Identifiers should not have incorrect suffix?
??????? 標(biāo)示符不應(yīng)該使用不正確的后綴(不要在不正確的地方使用12中提及的后綴名,和Delegate、Enum、Flags for an enumeration、Impl等后綴名)
16. Identifiers should not match keywords?
??????? 標(biāo)示符不應(yīng)該與系統(tǒng)關(guān)鍵字沖突
17. Long acronyms should be pascal-cased?
??????? 長(zhǎng)度大于等于3的縮寫詞應(yīng)該使用pascal的命名規(guī)則,即首字母大寫
18. Only FlagsAttribute enums should have plural names?
??????? 只有標(biāo)記有System.FlagsAttribute特性的枚舉的名稱才應(yīng)該使用復(fù)數(shù),其他時(shí)候應(yīng)該使用單數(shù)
19. Parameter names should match base declaration?
??????? 派生項(xiàng)的參數(shù)名應(yīng)該同基項(xiàng)相吻合(派生類重寫或?qū)崿F(xiàn)的方法應(yīng)該同基項(xiàng)具有相同的參數(shù)名)
20. Parameter names should not match member names?
??????? 方法的參數(shù)名不應(yīng)該同類或接口的成員名一樣
21. Property names should not match get methods?
??????? 屬性名字不應(yīng)該同Get開(kāi)頭的方法的名稱的后半部分相同
22. Resource string compound words should be cased correctly?
??????? 包含符合單詞的資源字符串應(yīng)該使用正確的大小寫(每個(gè)單詞的首字母大寫)
23. Resource strings should be spelled correctly?
??????? 資源字符串應(yīng)該正確的拼寫
24. Short acronyms should be uppercase?
??????? 短的首字母縮寫詞應(yīng)該全部大寫(比如DB,CR)
25. Type names should not match namespaces

??????? 類型的名字不應(yīng)該與名字空間的名字相同

26. Use preferred terms
??????? 優(yōu)先使用某些項(xiàng)目或者名稱,以下這些,后者為優(yōu)先使用的
ComPlus/EnterpriseServices
Cancelled/Canceled
Indices/Indexes
LogIn/LogOn
LogOut/LogOff
SignOn/SignIn
SignOff/SignOut
Writeable/Writable
五、 Performance(性能規(guī)則)
1. Avoid calls that require unboxing?
??????? 避免調(diào)用一個(gè)方法,它返回object類型,而你需要的是一個(gè)值類型(需要對(duì)返回值進(jìn)行拆箱操作)
2. Avoid costly calls where possible?
??????? 盡可能的避免進(jìn)行代價(jià)高昂的調(diào)用
3. Avoid excessive locals
??????? 避免使用過(guò)多的局部變量(多于64個(gè),部分可能是編譯器生成的)
4. Avoid uncalled private code
??????? 避免聲明在程序集內(nèi)從來(lái)未被調(diào)用的私有成員(private和internal),以下除外:
??????? 明確的接口成員
??????? 靜態(tài)構(gòu)造方法
??????? 靜態(tài)的Main方法(不含參數(shù)或僅包含一個(gè)string數(shù)組的參數(shù)的)
序列化構(gòu)造方法
??????? 標(biāo)記有System.Runtime.InteropServices.ComRegisterFunctionAttribute或者 System.Runtime.InteropServices.ComUnregisterFunctionAttribute.特性的
重寫的方法
5. Avoid uninstantiated internal classes
??????? 避免聲明不會(huì)被實(shí)例化的內(nèi)部類,以下情況除外
??????? 值類型
??????? 抽象類型
??????? 枚舉
??????? 委托
??????? 編譯器生成的數(shù)組類型
??????? 僅含有靜態(tài)成員的內(nèi)部類
6. Avoid unnecessary string creation?
??????? 避免創(chuàng)建不必要的string實(shí)例(猶指‘通過(guò)ToLower和ToUpper創(chuàng)建的string’),含以下情況
??????? 對(duì)于同一個(gè)string實(shí)例多次調(diào)用ToLower和ToUpper(建議:將返回值賦給一個(gè)局部變量,然后使用此局部變量)
??????? 使用equals,’==‘,!=比較‘通過(guò)ToLower和ToUpper創(chuàng)建的string’(建議:使用String.Compare比較)
??????? 向一個(gè)System.Collections.Specialized.HybridDictionary類型的成員傳遞‘通過(guò)ToLower和 ToUpper創(chuàng)建的string’(建議:HybridDictionary具有一個(gè)指示是否忽略大小寫的參數(shù)的構(gòu)造方法重載,使用此重載并傳遞一個(gè) true值進(jìn)去)
7. Avoid unsealed attributes?
??????? 避免聲明未封閉的特性(attributes)(建議:聲明為sealed/ NotInheritable-vb.net或者abstract)
8. Avoid unused parameters
??????? 避免在方法聲明中包含不會(huì)被使用的參數(shù),以下情況除外
??????? 代理引用的方法
??????? 作為事件處理程序的方法
??????? 抽象方法(abstract)
??????? 虛方法(virtual)
??????? 重寫的方法(override)
??????? 外部方法(extern)
9. Dispose methods should call SuppressFinalize
??????? Dispose方法應(yīng)該調(diào)用SuppressFinalize,以請(qǐng)求系統(tǒng)不要調(diào)用其Finalize方法
10. Do not call properties that clone values in loops?
??????? 不要在循環(huán)中使用‘返回一個(gè)Clone的對(duì)象的屬性’(每次返回‘引用不同’的對(duì)象,會(huì)導(dǎo)致創(chuàng)建大量的相同的對(duì)象)
11. Do not cast unnecessarily?
??????? 不要進(jìn)行不必要的類型轉(zhuǎn)換(特別是嘗試性的轉(zhuǎn)換,建議:在轉(zhuǎn)換前可以使用is操作符來(lái)判斷轉(zhuǎn)換能夠成功)
12. Do not concatenate strings inside loops?
??????? 不要在循環(huán)內(nèi)串聯(lián)string(建議:使用StringBuilder代替string)
13. Do not ignore method results?
??????? 不要忽略方法的返回值(通常調(diào)用string的方法會(huì)返回新的string)
14. Do not initialize unnecessarily
??????? 不要進(jìn)行不必要的初始化(比如將類成員初始化為它的默認(rèn)值)
15. Initialize reference type static fields inline
??????? 在靜態(tài)成員聲明的時(shí)候直接初始化或者調(diào)用靜態(tài)方法初始化(不要使用靜態(tài)構(gòu)造方法來(lái)初始化靜態(tài)成員,靜態(tài)構(gòu)造方法會(huì)影響性能),以下情況除外:
??????? 初始化對(duì)全局狀態(tài)的影響是代價(jià)高昂的,而且類型在使用前不需要進(jìn)行初始化的
??????? 在不需要訪問(wèn)該類型的靜態(tài)成員的情況下,全局狀態(tài)的影響就可以被訪問(wèn)到的
16. Override equals and operator equals on value types
??????? 對(duì)于公有的值類型,重寫equals方法和’==‘操作符(如果你期望用戶對(duì)實(shí)例進(jìn)行比較或者排序,或者作為哈希表的鍵)
17. Prefer jagged arrays over multidimensional?
??????? 使用鋸齒形數(shù)組代替多維數(shù)組(當(dāng)數(shù)組各元素的長(zhǎng)度可能不一致時(shí))
??????? 注意:公共語(yǔ)言規(guī)范(CLS)不支持鋸齒數(shù)組
18. Properties should not return arrays?
??????? 公有類型的屬性不應(yīng)該返回?cái)?shù)組(數(shù)組類型的屬性無(wú)法進(jìn)行寫保護(hù),即使是只讀的,除非每次返回不同的拷貝,但是這樣會(huì)讓調(diào)用者產(chǎn)生迷惑。建議:改成方法)
19. Remove empty finalizers
??????? 移除空的終結(jié)器
20. Remove unused locals
??????? 移除未使用過(guò)的局部變量
21. Test for empty strings using string length
??????? 使用length屬性測(cè)試字符串是否為空(原因:不要使用==””、==String..Empty、Equals(“”)等方法,使用Length屬性的效率是最高的;null==empty比較不會(huì)拋出異常;在DotNetFrameWork2里面可以使用IsNullOrEmpty方法來(lái)判斷字符串是否為null或者empty)
22. Use literals where appropriate
??????? 在適當(dāng)?shù)臅r(shí)候使用const代替static readonly(const是編譯時(shí)賦值的

轉(zhuǎn)載于:https://www.cnblogs.com/yaoshiyou/archive/2010/05/11/1732987.html

總結(jié)

以上是生活随笔為你收集整理的.NET代码编写规范 整理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。