8.2.3 枚举命名指导方针
· 用Pascal情况命名枚举值名字和类型名字
· 枚举类型和枚举值不要前缀
· 对于枚举用单一名字
· 对于位领域用复数名字
8.2.4 只读和常量命名
· 用名词,名词短语或名词的缩写命名静态领域
· 使用Pascal 情况(参考
8.2.5 参数/非常量领域命名
· 一定要用描述性名字,应该能够足够表现变量的意义和它的类型。但一个好的名字应该基于参数的意义。
· 使用Camel情况(参考
8.2.6 变量命名
· 计数变量当用在琐碎的计数循环式更适宜叫i, j, k, l, m, n。(参考10.2例如对全局计数的更智能命名等等)—
· 使用Camel情况(参考8.1.2)
8.2.7 方法命名
· 用动词或动词短语命名方法。
· 使用Pascal(参考
8.2.8 属性命名
· 用名词或名词短语命名属性
· 使用Pascal 情况(参考
· 考虑用与其类型相同的名字命名一个属性
8.2.9 事件命名
· 用事件处理器后缀命名事件处理器
· 用sender 和 e命名两个参数
· 使用Pascal情况(参考
· 用EventArgs 后缀命名事件参数
· 用现在和过去时态命名有前缀和复制概念的事件名字。
· 考虑用一个动词命名事件。
8.2.10 大写总结
Type |
Case |
Notes |
Class / Struct |
Pascal Casing |
|
Interface |
Pascal Casing |
Starts with I |
Enum values |
Pascal Casing |
|
Enum type |
Pascal Casing |
|
Events |
Pascal Casing |
|
Exception class |
Pascal Casing |
End with Exception |
public Fields |
Pascal Casing |
|
Methods |
Pascal Casing |
|
Namespace |
Pascal Casing |
|
Property |
Pascal Casing |
|
Protected/private Fields |
Camel Casing |
|
Parameters |
Camel Casing |
|
9. 编程习惯
9.1 可见性
不要任何公共实例或类变量,让它们为私有的。对于私有成员最好不用“private”作修饰语什么都不写。私有是默认情况,每个C#程序员都应该知道这一点。
用属性代替。你可以用公共静态(或常量)对于这个规则是以例外,带它不应该是规则。
9.2 没有“幻”数
不要用幻数,也就是在源代码中直接用常数值。替代这些后者以防变化(比方说,你的应用程序可以处理3540用户代替427你的代码在50行中通过分散25000LOC)是错误和没有收益的。声明一个带有数的常量来代替:
public class MyMath
{
public const double PI = 3.14159...
}
10. 编码举例
10.1 Brace placement example
namespace ShowMeTheBracket
{
public enum Test {
TestMe,
TestYou
}
public class TestMeClass
{
Test test;
public Test Test {
get {
return test;
}
set {
test = value;
}
}
void DoSomething()
{
if (test == Test.TestMe) {
//...stuff gets done
} else {
//...other stuff gets done
}
}
}
}
括弧应该在以下情况之后以新行开始:
· 命名空间声明(注意这在0.3版本中是新添的与0.2版本不同)
· 类/接口/结构声明
· 方法声明
10.2 变量命名举例
代替:
for (int i = 1; i < num; ++i) {
meetsCriteria[i] = true;
}
for (int i = 2; i < num / 2; ++i) {
int j = i + i;
while (j <= num) {
meetsCriteria[j] = false;
j += i;
}
}
for (int i = 0; i < num; ++i) {
if (meetsCriteria[i]) {
Console.WriteLine(i + " meets criteria");
}
}
try intelligent naming :
for (int primeCandidate = 1; primeCandidate < num; ++primeCandidate)
{
isPrime[primeCandidate] = true;
}
for (int factor = 2; factor < num / 2; ++factor) {
int factorableNumber = factor + factor;
while (factorableNumber <= num) {
isPrime[factorableNumber] = false;
factorableNumber += factor;
}
}
for (int primeCandidate = 0; primeCandidate < num; ++primeCandidate)
{
if (isPrime[primeCandidate]) {
Console.WriteLine(primeCandidate + " is prime.");
}
}
注意:索引变量通常叫i, j, k 等等。但Note: Indexer variables generally should be called i, j, k etc. But 万一像这样,使得重新考虑这个原则更有意义。一般来说,当同一个计数器或索引器被重用,给它们有意义的名字。
评论