【正文】
t {...}命名空間Pascal規(guī)范名詞253。 一定不要 使用下劃線,連字號,或其他任何非字母數(shù)字的字符。對于公共類型,線程過程,窗口過程,和對話框過程函數(shù),為“ThreadProc”, “DialogProc”, “WndProc” 等使用公共后綴。 您不應該在標識符名中使用縮短或縮略形式的詞。其命名應能反映其作用。 Microsoft Corporation 2010)]將AssemblyCompany 設置為 Microsoft Corporation[assembly: AssemblyCompany(Microsoft Corporation)]將AssemblyTitle 和 AssemblyProduct 設置為當前示例名[assembly: AssemblyTitle(CSNamedPipeClient)][assembly: AssemblyProduct(CSNamedPipeClient)] 命名規(guī)范 綜合命名規(guī)范254。StandardExample將 copyright設置為Copyright 169。比如, MainForm 類應該在 文件內(nèi),而 ListT 類應該在 文件內(nèi)。 254。 一定不要 在一個源文件內(nèi)擁有一個以上的Public類型,除非它們只有泛型參數(shù)個數(shù)的差別,或者具有嵌套關系。因此,建議你同時參照這兩份文檔 。 類庫開發(fā)設計規(guī)范MSDN上的類庫開發(fā)設計規(guī)范 對如何編寫優(yōu)秀的托管代碼進行了細致的討論。通過作用域或者功能性分類,將大量代碼分組,會改善代碼易讀性和結構。 代碼塊254。 一定不要 在已發(fā)布的代碼示例中使用TODO 待辦注釋。// [or]// Demo the second solution.//DemoSolution2()。 使用 [or] 來分隔多個方法。 將代碼注釋掉當您用多種方法實現(xiàn)某些任務時,將代碼注釋掉便是必須的。當出現(xiàn)副作用時,在編寫代碼時應該有清楚的理由。 }*//// summary/// The function checks whether the primary access token of the process /// belongs to user account that is a member of the local Administrators /// group, even if it currently is not elevated./// /summary/// param name=tokenThe handle to an access token/param/// returns/// Returns true if the primary access token of the process belongs to /// user account that is a member of the local Administrators group. /// Returns false if the token does not./// /returns/// exception cref=/// When any native Windows API call fails, the function throws a /// Win32Exception with the last error code./// /exception任何調用失敗會帶來副作用的方法或函數(shù),都需要清楚地在注釋中交代那些副作用的后果。 else wprintf (LUser is not a member of the Administrators group\n)。39。會拋出異常的方法應使用exception 元素來告知使用者哪些異常會被拋出。 C 和 使用 .NET 描述性 XML 文檔化 注釋。 您應該 為所有重要的Public或非Public函數(shù)作注釋 。C 類注釋模板:/// summary/// Class description/// /summary舉例, //// NAME: class CodeExample// DESCRIPTION: The CodeExample class represents an example of code, and // tracks the length and plexity of the example.//class CodeExample{ ...}。 C 和 使用 .NET 描述性 XML 文檔化 注釋。 您應該為 每一個重要的類或結構體作注釋。 一定請 為每一份手寫代碼文件加入文件頭注釋。您應該為那些僅通過閱讀很難理解其意圖的代碼添加注釋。 pBestWrinkle = pWrinkle。amp。 i++){ Wrinkle *pWrinkle = apWrinkles[i]。 // Remember this wrinkle as well }}更好的實現(xiàn)如下:Good:// Loop through each item in the wrinkles array, find the Wrinkle with // the largest impact that is new, and store it in ‘pBestWrinkle’.for (int i = 0。 // Process if it’s a new wrinkle nMaxImpact pWrinkleGetImpact()) // And it has the biggest impact { nMaxImpact = pWrinkleGetImpact()。 // Get the next wrinkle if (pWrinkleIsNew() amp。 i = nLastWrinkle。單行注釋應該用于代碼的行為并不是那么明顯易懂的情況。 您不應該 在代碼中留有過多注釋。 // The accuracy of the example}。 // Continue if not finished. }private: int m_length。在本例中,最好將所有變量的注釋對齊。 // The value is out of range, we cannot continue. return E_INVALIDARG。其之前和之后都有一個空行。其之后不需要空行。intialize the ponents on the Windows FormInitializeComponent() 內(nèi)聯(lián)代碼注釋內(nèi)聯(lián)注釋應該置于獨立行,并與所描述的代碼具有相同的縮進。 Intialize the ponents on the Windows Form.InitializeComponent()Bad://intialize the ponents on the Windows Form.InitializeComponent()。Good:// Intialize the ponents on the Windows Form.InitializeComponent()。 254。 // Determine whether system is running Windows Vista or later operating // systems (major version = 6) because they support linked tokens, but // previous versions (major version 6) do not.if ( = 6){}254。 您應該使用 ‘//’ 注釋方式,而不是 ‘/* */’ 來為 C++ 和 C 代碼作注釋。一定不要 讓注釋僅僅是重復代碼。 注釋254。Good: // C++ / C sample:if (x 5){ y = 0。 您應該 在即使是單行條件式的情況下也使用大括號。}Bad (in AllInOne Code Framework samples):// C++ / C sample:if (x 5) { y = 0。大括號內(nèi)的語句縮進一個等級。Allman 風格是以Eric Allman命名的,有時也被稱為ANSI 風格。 // Spaces inside bracketswhile(x == y) // No space before flow control statementsif (x==y) // No space separates operators 大括號254。 // No spaces after masCreateFoo( myChar, 0, 1 )。 // No spaces inside bracketswhile (x == y) // Single space before flow control statementsif (x == y) // Single space separates operatorsBad:// C++ / C sample:CreateFoo ()。 // No space between function name and parenthesisMethod(myChar, 0, 1)。 您應該 像如下般在一行代碼中使用空格 。 空格空格通過降低代碼密度以增加可讀性 。254。 item){ int counter = 0。 item){ int counter = 0。 比如,您可以在變量聲明和代碼之間有一行空行。 您應該使用空行來分隔相關語句塊 。if ((flags amp。 該檢查運行結果正確,但是有一處例外,便是對于0值的檢查,它的布爾表達式結果恒為true。 您不應該 在標志枚舉中使用0值,除非它代表 “所有標志被清除了” ,并被恰當?shù)拿麨轭愃啤癗one”的名字。然而,如果一個標志枚舉中的某些值組合起來是非法的,您就不應該創(chuàng)建這樣的標志枚舉。 位運算屬于高級任務,所以在簡單任務中無需使用它們。舉例,// C sample:[Flags]public enum AttributeTargets { Assembly = 0x0001, Class = 0x0002, Struct = 0x0004, ...}254。254。 一定要 將 應用于標志枚舉。標志枚舉通常用于表示選項。}Bad: // C sample:if (!(typeof(Color), c)){ throw new InvalidEnumArgumentException(...)。第二,它存在版本的問題。 一定不要 來檢查枚舉范圍。如果這個名稱對于特定的枚舉并不合適,可以自行定義為更準確的名稱。254。}253。 public const int Green = 1。如果遵守這些規(guī)范,定義枚舉類型,而不是帶有靜態(tài)常量的結構體,您便會得到額外的編譯器和反射支持。 一定請在合適的情況下盡量使用枚舉類型,而不是靜態(tài)常量或 “define” 值 。 一定請 將代表某些值集合的強類型參數(shù),屬性和返回值聲明為枚舉類型。 b = 2。b = 2。 這會使得調試器的單步調試變得更為困難。 代碼語句253。當設計一系列具有相同參數(shù)的函數(shù)時,在各函數(shù)內(nèi)使用一致的順序。在參數(shù)組內(nèi),按照能夠幫助程序員輸入正確值的原則來將參數(shù)排序。254。函數(shù)或方法調用時的參數(shù)列表同樣需按照這一格式。將參數(shù)列表置于多行代碼時,每一個參數(shù)應該整齊排列于前一個參數(shù)的下方。例如,單行格式:// C function call sample:hr = DoSomeFunctionCall(param1, param2, param3)。如果帶有過多參數(shù)不能置于一行代碼,可以進行換行:多個參數(shù)一行或者一個參數(shù)一行。 函數(shù)的聲明和調用 函數(shù)或方法的名稱,返回值,參數(shù)列表可以有多種形式。這種誤寫實際上等同于:CodeExample *pFirst。 // Pointer of the second element.Bad:CodeExample *pFirst, *pSecond。例如 Visual C++ 示例,Good:CodeExample *pFirst = NULL。 一定不要 在同一行中聲明多個變量 。int val = 。這減少了代碼的垂直空間,確保了變量不會處在未初始化的狀態(tài)。254。254。 一定請在最小的,包含該局部變量的作用域塊內(nèi)聲明它。如果您需要修改全局變量,您應該將其作為一個輸出參數(shù),或返回其一份全局變量的拷貝。永遠不要在函數(shù)或類內(nèi)部直接引用全局變量,因為這會引起一個副作