Rules, Rules, all are rules

Rules, Rules, all are rules

Microsoft FxCop,对,就是那个用无数的Rules来检查你的装配件的东东,(http://www.gotdotnet.com/team/fxcop/)。无数的Rules。。。小小的程序,硬是扫出五十几个Error\Warning来。大多是重复的,整理如下:

Design Rules:

(1) Assemblies should have valid strong name
装配件必须有强名称。
Only exclude it if the assembly is used in an enviroment where tampering with the contents is not a concern.
只有在不在意装配件内容是否被修改的情况下才能忽略此规则。

(2) Properties should not be write only
不要使用只写属性
Write-only properties usually indicate a flawed design
只写属性往往表示设计有缺陷
design guidelines prohibit the use of write-only properties
设计指南禁止使用只写属性
add a get accessor, or if the behavior is necessary, coverting the property to a method
(若需改变)增加一个写属性(Get),如果必须实现这种行为(只能写),就使用方法(而不是只写属性)来实现

(3) Do not catch Exceptions or SystemExceptions
不要直接捕获Exception或System.Exception
Generic exceptions should not be caught.
Catch a more specific exception, or re-throw the generic exception as the last statement in the catch block.
捕获更有针对性的异常,或在Catch模块的最后重新抛出此异常.(注:可能会损坏Stack信息)
Do not exclude. Catching generic exception types can hide run-time problems from the library user, and can complicate debugging.
请勿违反此规则,捕获一般化的异常类型可能会使得运行时的问题对此Library的用户隐藏起来,也可能会影响排错.
(注:个人理解,如果是直接面向用户的程序的话,可以捕获Generic Exception,毕竟,由你给出的异常报告(或者根本不报告)比MS弹出的异常要友好得多 )

(4) Mark assembilies with CLSCompliant
将装配件打上CLSCompliant标记
Assemblies should explicitly state their CLs compliance using the CLSCompliant attribute
装配件应该要明确的标出其CLs compliance属性(在AssemblyInfo中加入[assembly:CLSCompliant(true)])
Assemblies can be CSLCompliant, even if some parts of them are not CSLCompliant

Globalization Rules


(1) Do not pass literals as localized prarmeters
不要传递"直接的"字符串(如"string")作为区域参数
String literals that are embedded in source code are difficult to localize
"直接的"字符串很可能不能被正确的区域化(本地化)
to fix it, replace the string literal with a string retrieved through an instance of the System.Resources.ResourceManager class
要解决这个问题,可以使用System.Resources.ResourceManager class,请参阅MSDN
if code library will not be localized or string is not exposed to end user or developer, you can exclude it
如果不会对最终用户或其他用户造成影响,可以忽略此规则

(2) Specify IFormatProvider
指定IFormatProvider
When a CultureInfo or IFormatProvider object is not supplied, the default value supplied by the overloaded member might not have the desired effect in all locals
如果不指定转换方式,默认的转换可能会不正确
.NET Framework members choose default culture and formatting based on assumptions that might not be correct for your code

Naming Rules


(1) Identifiers should not contain underscores
标志符不要包含下划线

(2) Identifiers should be spelled correctly
标志符应该正确的拼写

(3) Long acronyms should be pascal-cased
长的缩写词应该使用Pascal命名法

(4) Type names should not match namespaces
类型名称不应该与命名空间重名

(5) Compound words should be cased correctly
混合词格式应该正确

(6) Identifiers should be cased correctly
标志符名称格式应该正确
使用Pascal-cased或Camel-cased

Performance Rules


(1) Remove unused locals
去掉没有使用的局部变量定义
Remove locals that are not used or are only assiged to in method implementations

(2) Avoid calls that box value types
避免调用需要Box(装箱)的值类型

(3) Do not initialize unnecessarily
不要进行不必要的初始化(编译器会做此动作)

(4) Test for empty strings using string length
使用length属性判断字符串是否为空串(指为""而非null)
Test for for empty strings, check if string.Length is equal to zero. Constructs such as "".Equals(string) and stirng.Empty.Equals(string) are less efficient than testing length. Replace these with checks for str.Length == 0
Null string: "Length" -> throw a exception "NullReferenceException"
 Equals("") -> return a false

Usage Rules


(1) Do not raise reserved exception types
不要直接抛出Exception类型的异常
User code should not create and raise exceptions of certain types that are reserved by the runtime or which are of a too general exception type

(2) Rethrow to preserve stack details
重新抛出(而非构造新的)异常将有利于保护堆信息细节
use IL rethrow to preserve original stack details when re-raising a caught exception
 
(3) Literals should be spelled correctly
字符串也要正确的拼写

(4) Non-constant fields should not be visible
非Const成员变量不要暴露出来
Static fields that are neither constants nor read-only are not thread safe
Applications should not expose any fields
应用程序不要暴露任何内部成员
make the static field constant or read-only, if not, machanism such as a thread-safe property
将此成员变量改为Const或Readonly,如果不行,使用线程安全的属性来代替
Be aware that issues such as lock contention and deadlocks might impact the performance and behavior of the library

 

原文地址:https://www.cnblogs.com/zjp8023/p/CSharpStyle02.html