一直想寫一個自己用的代碼標准,經過一段時間的優秀開源源碼的觀察和看其他人寫的標准,感覺好的代碼給人感覺就是舒服,也非常重要。所以把它們記錄歸納總結,以備以后忘記,另外平時寫代碼的時候可以拿來參考下。下面的示例主要以Microsoft的代碼為准。
命名約定
- PascalCasing
PascalCasing 每一個單詞第一個字母大寫,其余字母均小寫。例如:FileAccess,ArraySegment等。
除了參數、變量、常量外,所有命名空間名稱、類、函數、接口、屬性、事件、枚舉等名稱的命名,使用 Pascal 風格。
-
camelCasing
camelCasing 第一個單詞首字母小寫,其余單詞首字母大寫。例如:propertyName,filePath等。
參數與變量的命名使用camelCasing.
-
SCREAMING_CAPS
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、當不理解一個方法時,可以把它們分解為更小部分,並為它們取恰當名稱。
