浏览器大全:是一个提供流行浏览器教程、在线学习分享的学习平台!

IDesign C#编程规范(二)

续之一,小鸡射手接着翻译了IDesign编码规范的第二章前部。

2 编码惯例
Coding Practices


1. 避免在一个文件中放多个类。
Avoid putting multiple classes in a single file.
2. 一个文件应该只对一个命名空间提供类型。避免在同一文件中有多个命名空间。
A single file should only contribute types to a single namespace. Avoid having multiple namespaces in the same file.
3. 避免文件长度超过500行(除了机器自动产生的代码)。
Avoid files with more than 500 lines (excluding machine-generated code).
4. 避免方法定义超过25行。
Avoid methods with more than 25 lines.
5. 避免超过5个参数的方法。使用结构传递多个参数。
Avoid methods with more than 5 arguments. Use structures for passing multiple arguments.
6. 每行应该不超过80个字符。
Lines should not exceed 80 characters.
7. 不要手工编辑任何机器生成的代码。
Do not manually edit any machine generated code.
a) 如果修改机器生成的代码,修改代码格式和风格以符合本编码标准。
If modifying machine generated code, modify the format and style to match this coding standard.
b) 尽可能采用partial类以分解出需要维护的部分。
Use partial classes whenever possible to factor out the maintained portions.
8. 避免对显而易见的内容作注释。
Avoid comments that explain the obvious.
a) 代码应该是自解释的。 由可读性强的变量和方法组成的好的代码应该不需要注释。
Code should be self explanatory. Good code with readable variable and method names should not require comments.
9. 仅对操作的前提、内在算法等写文档。
Document only operational assumptions, algorithm insights and so on.
10. 避免方法级的文档。
Avoid method-level documentation.
a) 对API文档采用大量的外部文档。
Use extensive external documentation for API documentation.
b) 方法级注释仅作为对其他开发人员的提示。
Use method-level comments only as tool tips for other developers.
11. 决不要硬编码数值, 而总是声明一个常量。
Never hard-code a numeric value, always declare a constant instead.
12. 仅对本来就是常量的值使用const修饰符,例如一周的天数。
Use the const directive only on natural constants such as the number of days of week.
13. 避免对只读变量使用const修饰符。在此情况下,采用readonly修饰符。
Avoid using const on read-only variables. For that, use the readonly directive.
public class MyClass
{
public readonly int Number;
public MyClass(int someValue)
{
Number = someValue;
}
public const int DaysInWeek = 7;
}
14. 对任何假设采用assert。
Assert every assumption.
a) 平均地,每5行中就有一行是断言。
On average, every fifth line is an assertion.
using System.Diagnostics;
object GetObject()
{
object obj = GetObject();
Debug.Assert(obj != null);
15. 每行代码应该经过白盒测试。
Every line of code should be walked through in a 搘hite box?testing manner.
16. 仅捕获已经显式处理了的异常。
Only catch exceptions for which you have explicit handling.
17. 在抛出异常的catch语句中,总是抛出最初异常以保持最初错误的堆栈位置。
In a catch statement that throws an exception, always throw the original exception to maintain stack location of original error.
catch(Exception exception)
{
MessageBox.Show(exception.Message);
throw; //Same as throw exception;
}
18. 避免将错误代码作为方法的返回值。
Avoid error code as methods return values.
19. 避免定义自定义的异常类。
Avoid defining custom exception classes.
20. 定义自定义异常时:
When defining custom exceptions:
a) 从ApplicationException继承
Derive the custom exception from ApplicationException.
b) 提供自定义的序列化。
Provide custom serialization.
21. 避免在一个程序集中有多个Main()方法。
Avoid multiple Main() methods in a single assembly.
22. 仅对最需要的类型标记为public,其他的标记为internal。
Make only the most necessary types public, mark others as internal.
23. 避免采用friend程序集,因为这样增加了程序集间的耦合度。
Avoid friend assemblies, as it increases inter-assembly coupling.
24. 避免使用依赖于从特定位置运行的程序集的代码。
Avoid code that relies on an assembly running from a particular location.
25. 尽量减少应用程序集(客户端EXE程序集)的代码。采用类库而不要包含业务逻辑层代码。
Minimize code in application assemblies (EXE client assemblies). Use class libraries instead to contain business logic.
26. 避免对枚举提供明确的值。
Avoid providing explicit values for enums .
//Correct
public enum Color
{
Red,Green,Blue
}
//Avoid
public enum Color
{
Red = 1,Green = 2,Blue = 3
}
27. 避免对枚举指定类型。
Avoid specifying a type for an enum.
//Avoid
public enum Color : long
{
Red,Green,Blue
}
28. if语句总是使用括号,即使它包含一句语句。
Always use a curly brace scope in an if statement, even if it conditions a single statement.
29. 避免使用?:条件算符。
Avoid using the trinary conditional operator.
30. 避免在布尔条件语句中调用函数。赋值到局部变量并检查它们的值。
Avoid function calls in Boolean conditional statements. Assign into local variables and check on them:
bool IsEverythingOK()
{...}
//避免:
//Avoid:
if(IsEverythingOK())
{...}
//采用:
//Instead:
bool ok = IsEverythingOK();
if(ok)
{...}
31. 总是使用从0开始的数组。
Always use zero-based arrays.
32. 总是使用一个for循环显式地初始化一个引用类型的数组。
Always explicitly initialize an array of reference types using a for loop.
public class MyClass
{}
MyClass[] array = new MyClass[100];
for(int index = 0; index < array.Length; index++)
{
array[index] = new MyClass();
}
33. 不用提供public或protected成员变量,而是使用属性。
Do not provide public or protected member variables. Use properties instead.


相关软件

2345加速浏览器官方版

2345加速浏览器官方版 | 56.2MB

2345加速浏览器官方版

新一代2345加速浏览器采用Chromium和IE双内核,主打极速与安全特性。基于Chromium深度定制,引入网页智能预加载技术,访问网页更快速..

QQ浏览器官方正式版

QQ浏览器官方正式版 | 49.67MB

QQ浏览器官方正式版

QQ浏览器秉承TT浏览器1-4系列方便易用的特点,但技术架构不同,交互和视觉表现也重新设计,采用Chromium内核+IE双内核,让浏览快速稳定...

百度浏览器最新版下载

百度浏览器最新版下载 | 13.3MB

百度浏览器最新版下载

q百度浏览器,是一款简洁轻快、智能懂你的浏览器。依靠百度强大的搜索平台,在满足用户浏览网页的基础上,它整合百度体系业务优势,带给用户更方便的浏览方式功能...

UC浏览器官方正式版

UC浏览器官方正式版 | 44.2MB

UC浏览器官方正式版

UC浏览器(UC Browser)是UC Mobile Limited在2004年8月开发的一款软件,分uc手机浏览器和uc浏览器电脑版。UC浏览器是全球使用量最大的第三方手机浏览器...

猎豹浏览器2022最新版下载

猎豹浏览器2022下载 | 45MB

猎豹浏览器2022最新版下载

猎豹安全浏览器对Chrome的Webkit内核进行了超过100项的技术优化,访问网页速度更快。其具有首创的智能切换引擎,动态选择内核匹配不同网页...

360安全浏览器官方版下载

360安全浏览器下载 | 21.4MB

360安全浏览器官方版下载

360安全浏览器拥有全国最大的恶意网址库,采用恶意网址拦截技术,可自动拦截挂马、欺诈、网银仿冒等恶意网址。独创沙箱技术,在隔离模式即使访问****也不会感染...