命名規則的重要性
- 遵守共通的命名規則可增加framework的使用頻率
- 可以讓更多開發者了解專案內容與元件說明
- 本章節的目的是為了讓
Following a consistent set of naming conventions in the development of a framework can be a major contribution to the framework’s usability. It allows the framework to be used by many developers on widely separated projects. Beyond consistency of form, names of framework elements must be easily understood and must convey the function of each element. The goal of this chapter is to provide a consistent set of naming conventions that results in names that make immediate sense to developers. Although adopting these naming conventions as general code development guidelines would result in more consistent naming throughout your code, you are required only to apply them to APIs that are publicly exposed (public or protected types and members, and explicitly implemented interfaces).
https://msdn.microsoft.com/en-us/library/ms229002(v=vs.110).aspx
駝峰式命名原則
已有意義的單字組成,每個單字的第一個字大寫。
包括下列兩種情況
- PascalCasing
- 首字大寫,用於大部分的命名
- camelCasing
- 首字小寫,用在傳入的參數名稱
避免使用特殊符號,例如 "_"
使用情況如下
Identifier | Casing | Example |
---|---|---|
Namespace | Pascal | namespace System.Security { ... } |
Type | Pascal | public class StreamReader { ... } |
Interface | Pascal | public interface IEnumerable { ... } |
Method | Pascal | public class Object {public virtual string ToString();} |
Property | Pascal | public class String { public int Length { get; }} |
Event | Pascal | public class Process { public event EventHandler Exited;} |
Field | Pascal | public class MessageQueue {public static readonly TimeSpan InfiniteTimeout;} public struct UInt32 { public const Min = 0;} |
Enum value | Pascal | public enum FileMode { Append,...} |
Parameter | Camel | public class Convert {public static int ToInt32(string value);} |
複合字
單字如果是複合字,根據每個單獨有意義的字做PascalCasing或camelCasing
不要使用可能有特殊意義的文字,like CLR 即使你的language沒有特殊意義,但其他語言可能有
https://msdn.microsoft.com/en-us/library/ms229043(v=vs.110).aspx
命名規範
單字選擇
選擇簡單易讀且有意義的單字
單字順序的組合上要容易閱讀
✓ DO HorizontalAlignment
X DO NOT AlignmentHorizontal
不要使用縮寫字
不要使用在程式中有意義的文字
https://msdn.microsoft.com/en-us/library/ms229045(v=vs.110).aspx