IDesign C#編程規範(二)

來源:互聯網
上載者:User
編程|規範 續之一,小雞射手接著翻譯了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.

之三


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.