dofactory.com c# 程式碼規範
http://www.dofactory.com/reference/csharp-coding-standards.aspx
其他參考:微軟官方/開發類別庫的設計方針
http://msdn.microsoft.com/zh-tw/library/ms229042(v=vs.100).aspx
1.do
2.do not
3.avoid
do
1-1.Class,Method:Pascal
use PascalCasing for class names and method names
sample:
public class ClientActivity
public void ClearStatistics()
Reason:
與.net 規則相同,易讀
1-2 Class,Method,Property,Field 類別/方法/屬性/欄位:Pascal
use PascalCasing for 字母以上的單字,若只有兩個字母則兩個都大寫
sample:
HtmlHelper htmlHelper;
FtpTransfer ftpTranfer;
UIControl uiControl;
Reason:
與.net 規則相同,UI較清楚
2-1.參數/變數:駝峰
use camelCasing for method arguments and local variables.
sample:
public class UserLog{
public void Add(LogEvent logEvent) {
int itemCount = logEvent.Items.Count;
// ...
}
}
Reason: 與.net 規則相同,易讀
3-1 使用預先訂義好的變數,取代Int16,Single,
do use predefined type names instead of system type names like Int16, Single, UInt64, etc
// Correct
string firstName;
int lastIndex;
bool isSaved;
// Avoid
String firstName;
Int32 lastIndex;
Boolean isSaved;
Reason:
與.net 規則相同,易讀
4.使用var 給local變數使用,除了內建的資料型別(int,string,double)
use implicit type var for local variable declarations. Exception: (int, string,
double, etc) use predefined names.
Sample;
var stream = File.Create(path);
var customers = new Dictionary<int?, Customer>();
// Exceptions
int index = 100;
string timeSheet;
bool isCompleted;
Reason:移除混亂或特別的複雜型別
Why: removes clutter, particularly with complex generic types. Type is easily detected with Visual Studio tooltips.
5.使用名詞或名詞片語 來命名class
do use noun or noun phrases to name a class.
public class Employee{}p
ublic class BusinessLocation{}
public class DocumentCollection{}
Reason:
與.net 規則相同,好記
6.介面前置詞為I,介免使用名詞或形容詞
doprefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.
public interface IShape{}
public interface IShapeCollection{} //名詞
public interface IGroupable{} //形容詞
Reason:
與.net 規則相同
7.class名稱盡量跟檔案名稱相符,除了IDE自動產生對應的檔案之外(desingner)
do name source files according to their main classes. Exception: file names with partial classes
reflect their source or purpose, e.g. designer, generated, etc.
// Located in Task.cs
public partial class Task{
//...
}
// Located in Task.generated.cs
public partial class Task{
//...
}
Reason:
與.net 的實例相同,檔案或partial class 排序會接近
8.將namespace分類清楚
do organize namespaces with a clearly defined structure
// Examples
namespace Company.Product.Module.SubModule
namespace Product.Module.Component
namespace Product.Layer.Module.Group
Reason:
與.net 規則相同,並可維持好的原始碼基礎
9.原始碼垂直要對齊
do vertically align curly brackets.
// Correct
class Program{
static void Main(string[] args)
{
}
}
Reason:
微軟有不一樣的標準,但開發者一般喜歡對齊
10.宣告所有的member 變數於class上方 ,static的變數 為最上方
do declare all member variables at the top of a class, with static variables at the very top.
// Correct
public class Account{
public static string BankName;
public static decimal Reserves;
public string Number {get; set;}
public DateTime DateOpened {get; set;}
public DateTime DateClosed {get; set;}
public decimal Balance {get; set;}
// Constructor
public Account() {
// ...
}
}
Reason: 一般可以接受的實例,避免要尋找變數
11.使用單字for enums, 例外:bit
do use singular names for enums. Exception: bit field enums.
// Correct
public enum Color{
Red,
Green,
Blue,
Yellow,
Magenta,
Cyan}
// Exception
[Flags]
public enum Dockings{
None = 0,
Top = 1,
Right = 2,
Bottom = 4,
Left = 8
}
Reason:
與.net 規則相同,易讀,(複數的flag可以用or運算)
do not
1.不要使用:匈牙利命名
do not use Hungarian notation or any other type identification in identifiers
Sample:
// Correct
int counter;string name;
// Avoid
int iCounter;string strName
Reason: 與.net 規則相同,IDE裡要判別型別很容易,不需要加上型別
2.不要使用全大寫 (常數或readonly)
do notuse Screaming Caps for constants or readonly variables
// Correct
public static const string ShippingType = "DropShip";
// Avoid
public static const string SHIPPINGTYPE = "DropShip";
Reason:與.net 規則相同,大寫太容易引人注意
3.不要使用底線,例外(private 變數可以用底線開頭)
do notuse Underscores in identifiers. Exception: you can prefix private static variables
with an underscore.
// Correct
public DateTime clientAppointment;
public TimeSpan timeLeft;
// Avoid
public DateTime client_Appointment;
public TimeSpan time_Left;
// Exception
private DateTime _registrationDate;
Reason:與.net 規則相同,容易閱讀,避免印刷模糊,或底線格式下看不到
4.enum不要指定型態(例外,bit)
do not
explicitly specify a type of an enum or values of enums (except bit fields)
// Don't
public enum Direction : long{ North = 1, East = 2, South = 3, West = 4}
// Correct
public enum Direction{ North, East, South, West}
Reason:會混淆到底是依賴name or value
5.enum的名字中 不要加Enum字眼
do not suffix enum names with Enum
// Don't
public enum CoinEnum{ Penny, Nickel, Dime, Quarter, Dollar}
// Correct
public enum Coin{ Penny, Nickel, Dime, Quarter, Dollar}
reason:與.net 規則相同,不加入資料型別在宣告名稱內
avoid
1.避免使用縮寫(除非是常用的縮寫)
avoidusing Abbreviations. Exceptions: abbreviations commonly used as names,
such as Id, Xml, Ftp, Uri
// Correct
UserGroup userGroup;
Assignment employeeAssignment;
// Avoid
UserGroup usrGrp;
Assignment empAssignment;
// Exceptions
CustomerId customerId;
XmlDocument xmlDocument;
FtpHelper ftpHelper;
UriPart uriPart;
Reason: 與.net 規則相同,防止不一致的縮寫
留言列表