C程式設計規範

2022-12-06 01:33:05 字數 4121 閱讀 8341

沒有規矩,不成方圓",c#是.net中乙個功能強大的語言,這意味著你用c#可以編寫強大的程式,也可以產生一堆垃圾,在你沒有自信成為絕頂的高手、輕鬆駕馭c#這匹野馬之前,遵循一定的程式設計規範和標準起碼可以讓你和你的團隊輕鬆交流,並且和他們看起來保持同乙個水平。規範可以一定程度上保證軟體質量、保證專案進度。

目錄內容如下:

1 命名規則和風格 naming conventions and style

2 編碼慣例 coding practices

3 專案設定和結構 project settings and structure

4 framework特別指導 framework specific guidelines

4.1 資料訪問 data access

4.2 和web service and web services

4.3 序列化 serialization

4.4 多執行緒 multithreading

4.5 remoting remoting

4.6 安全 security

4.7 服務元件 enterprise services

一、命名規則和風格

1. 類和方法名採用pascal風格

use pascal casing for type and method names

public class someclass

}2. 區域性變數和方法引數採用camel風格

use camel casing for local variable names and method arguments

int number;

void mymethod(int somenumber)

{}3. 介面名採用i作為字首

prefix inte***ce name with i

inte***ce imyinte***ce

4. 私有成員變數採用m_作為字首

prefix private member variables with m_

public class someclass

5. 自定義屬性類名採用attribute作為字尾

suffix custom attribute classes with attribute.

6. 自定義異常類名採用exception作為字尾

suffix custom exception classes with exception.

7. 採用動詞-物件對命名方法,例如showdialog()

name methods using verb-object pair, such as showdialog()

8. 有返回值的方法應該取名表示其返回值,例如getobjectstate()

methods with return values should h**e a name describing the value returned, such as getobjectstate().

9. 採用描述性的變數名。

use descriptive variable names.

a) 避免採用單字母的變數名,如i或t;而是採用index或temp。

**oid single character variable names, such as i or t. use index or temp instead.

b) 對public和protected成員避免採用用匈牙利命名法。

**oid using hungarian notation for public or protected members.

c) 不要採用縮寫(例如將number縮寫為num)。

do not abbreviate words (such as num instead of number).

10. 總是使用c#預定義的型別,而不是使用system命名空間中的別名。例如:採用object不用object,採用string不用string,採用int不用int32。

always use c# predefined types rather than the aliases in the system namespace.

for example:

object not object

string not string

int not int32

11. 對於泛型,型別採用大寫字母。當處理.net型別type時保留字尾type。

with generics, use capital letters for types. reserve suffixing type when dealing with the .net type type.

// 正確:

//correct:

public class linkedlist

// 避免使用:

//**oid:

public class linkedlist

12. 採用有意義的命名空間名,例如產品名稱或公司名稱。

use meaningful namespaces such as the product name or the company name.

13. 避免使用類的全稱,而是採用using語句。

**oid fully qualified type names. use the using statement instead.

14. 避免在命名空間內使用using語句。

**oid putting a using statement inside a namespace.

15. 將所有framework命名空間名放在一起,後面放自定義或第三方的命名空間名。

group all framework namespaces together and put custom or third party namespaces underneath.

using system;

using

using

using

using mycompany;

using mycontrols;

16. 採用委託推斷,不要顯式例項化委託。

use delegate inference instead of explicit delegate instantiation

delegate void somedelegate();

public void somemethod()

{}somedelegate somedelegate = somemethod;

17. 嚴格遵守縮排格式。

maintain strict indentation.

a) 縮排採用3個空格。

use 3 spaces for indentation.

b) 不用採用tab或非標準的縮排,如1、2或4個空格。

do not use tabs or non-standard indentation like 1, 2 or 4 spaces.

18. 注釋縮排和其注釋的**在同一層次。

indent comment at the same level of indentation as the code you are documenting.

19. 所有注釋要經過拼寫檢查。拼寫錯誤的注釋表明開發的草率。

all comments should pass spell checking. misspelled comments indicate sloppy development.

20. 所有成員變數應該定義在前面,和屬性或方法間空開一行。

all member variables should be declared at the top, with one line separating them from the properties or methods.

public class myclass

21. 區域性變數的定義盡可能靠近它的初次使用。

declare a local variable as close as possible to its first use.

22. 檔名應該體現其包含的類。

a file name should reflect the class it contains.

C 程式設計規範

1.簡介 1 1.1 基本原則 1 1.2 適用範圍 1 1.3 使用常識 1 1.4 參考資料 1 2.組織 2 2.1 專案組織風格 2 2.1.1 專案取名與最終的可執行檔名一致 2 2.1.2 專案目錄設定 2 2.2 整合環境內的專案目錄設定 3 2.3 檔案宣告 3 2.3.1 類宣告與...

C語言程式設計規範

編碼規範 1.標頭檔案編碼規範 3 2.函式編寫規範 3 3.識別符號命名與定義 3 3.1通用命名規則 3 3.2 變數命名規則 4 3.3函式命名規則 4 3.4 巨集的命名規則 4 4.變數 4 5.巨集 常量 5 6.質量保證 5 7.程式效率 6 8.注釋 6 9.排版與格式 7 10.表...

C語言程式設計規範

湖南理工學院創新基地 作者彭巍 目錄1 程式的版式 2 1.1 空行 2 1.2 行 3 1.3 空格 3 1.4 長行拆分 4 2 程式的注釋 4 3 命名規則 5 3.1 共性規則 5 3.2 建議的簡單命名規則 6 4 表示式和基本語句 6 4.1 運算子的優先順序 6 4.2 if 語句 7...