.net代码编写规范

发表于:2008-4-02 14:24

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

#
.NET
        下面是根据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的检验)
31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号