標籤:
一直想寫一個自己用的代碼標準,經過一段時間的優秀開源源碼的觀察和看其他人寫的標準,感覺好的代碼給人感覺就是舒服,也非常重要。所以把它們記錄歸納總結,以備以後忘記,另外平時寫代碼的時候可以拿來參考下。下面的樣本主要以Microsoft的代碼為準。
命名規範
PascalCasing 每一個單詞第一個字母大寫,其餘字母均小寫。例如:FileAccess,ArraySegment等。
除了參數、變數、常量外,所有命名空間名稱、類、函數、介面、屬性、事件、枚舉等名稱的命名,使用 Pascal 風格。
camelCasing 第一個單詞首字母小寫,其餘單字首大寫。例如:propertyName,filePath等。
參數與變數的命名使用camelCasing.
SCREAMING_CAPS每個單詞的所有字母都大寫,單詞與單詞之間用"_"串連,該風格目前在c#中只用於const常量。
如:public const string DEFAULT_PAGE = "default.aspx";
Private 的私人變數使用底線"_"或"m_"+camelCasing的大小寫規則,以便快速確認該變數的範圍。
如: private int _userId;
private int m_userId;
一、命名規範與風格
1、類型名與方法名用pascal命名規範:
public class StringReader : TextReader { public override String ReadToEnd() { ... } }
2、局部變數與方法參數使用camel命名規範:
internal static String InternalCopy(String sourceFileName, String destFileName, bool checkHost) { string fullSourceFileName = Path.GetFullPathInternal(sourceFileName); ... }
3、介面名加首碼I:
public interface IComparer { // Compares two objects. An implementation of this method must return a // value less than zero if x is less than y, zero if x is equal to y, or a // value greater than zero if x is greater than y. int Compare(Object x, Object y); }
4、私人成員變數前加首碼m_或_:
unsafe internal class PathHelper { // maximum size, max be greater than max path if contains escape sequence private int m_capacity; // current length (next character position) private int m_length; // max path, may be less than capacity private int _maxPath; .... }
5、常量使用pascal或全部大寫表示:
private const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; const string RsaKeyValue = XmlSignatureConstantsNamespace + "RSAKeyValue";
6、自訂屬性類別後加尾碼Attribute:
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
[ComVisible(true)]
public sealed class DebuggerVisualizerAttribute: Attribute
7、自訂異常類加尾碼Exception:
[Serializable]
public class ArgumentException : SystemException, ISerializable
8、方法命名使用“動詞/對象”對,如 GetHashCode(...),
public unsafe static Array CreateInstance(Type elementType, int length)
9、有傳回值的方法名中需要描述此傳回值,如GetObjectState()。
10、使用描述性變數名。
a.避免使用單個字母作為變數名,如i或t,用index或temp來代替。
b.避免對公用成員或受保護的成員使用匈牙利標記法。
c.避免使用縮寫(如把number寫成num)。
11、不要混淆使用c#中的預定義類型與CLR中的類型:
如object 與Object, string與String,int與Int32
12、對於泛型,使用該類型的首字母代替,當使用.NET的Type類型時才使用Type作為尾碼:
public class Dictionary<K,V>
{...}
13、使用有意見的命名空間名稱,如產品名或公司名。
14、避免使用完全限定類型名。用using語句代替。
15、把所有的framework命名空間一起放在最上面,把自訂放在最下面,第三方命名空間放中間:
using System;using System.Collections;using System.Collections.Generic;using ThreadPartyLibrary;using MyCompanyName;
16、採用委託推斷,不要顯示執行個體化委託:
delegate void SomeDelegate();public void SomeMethod(){...}SomeDelegate someDelegate = SomeMethod;
17、使用Tab來進行縮排。
18、所有的成員變數都必須在頂部聲明,用一行把它們與屬性或方法隔開。
19、定義局部變數時,盡量使它靠近第一次使用它的地方。
20、檔案名稱能夠反應它使用的類。
21、使用左大括弧({)時,換一行。
22、if下面即使只有一行代碼,也要加{}包圍起來:
23、為每一個命名空間建一個目錄。如 MyProject.TestSuite.TestTier 使用MyProject/TestSuite/TestTier 作為路徑。
24、當一行中的代碼太長時,建議使用下面的方式進行斷行:
- Break after a comma----逗號後面
- Break after an operator----操作符後面
- Prefer higher-level breaks to lower-level breaks----
- Align the new line with the beginning of the expression at the same level on the previous line.
如 longMethodCall(expr1, expr2,
expr3, expr4, expr5);
Examples of breaking an arithmetic expression:
PREFER:
var = a * b / (c - g + f) + 4 * z; var = a * b / (c - g + f) + 4 * z; BAD STYLE – AVOID: var = a * b / (c - g + f) + 4 * z;
25、聲明變數時就進行初始化。
26、從 Stream 繼承的 Framework 類型以 Stream 結尾:
如FileStream, MemoryStream等。
27、當不理解一個方法時,可以把它們分解為更小部分,把為它們取恰當名稱。
C# 編碼通訊協定(一)