目前分類:程式開發規範 (2)

瀏覽方式: 標題列表 簡短摘要

Real-World Sample Code

 

 

參考http://www.dofactory.com/Patterns/Patterns.aspx

Demo

常用

Creational Patterns

 

5

  Abstract Factory

  Creates an instance of several families of classes

 

2

  Builder

  Separates object construction from its representation

 

5

  Factory Method

  Creates an instance of several derived classes

 

3

  Prototype

  A fully initialized instance to be copied or cloned

Singleton.aspx

4

  Singleton

  A class of which only a single instance can exist

 

Demo

 

  Structural Patterns

 

4

  Adapter

  Match interfaces of different classes

 

3

  Bridge

  Separates an object’s interface from its implementation

 

4

  Composite

  A tree structure of simple and composite objects

 

3

  Decorator

  Add responsibilities to objects dynamically

Facade.aspx

5

  Facade

  A single class that represents an entire subsystem

 

1

  Flyweight

  A fine-grained instance used for efficient sharing

 

4

  Proxy

  An object representing another object

 

Demo

 

  Behavioral Patterns

 

2

  Chain of Resp.

  A way of passing a request between a chain of objects

 

4

  Command

  Encapsulate a command request as an object

 

1

  Interpreter

  A way to include language elements in a program

 

5

  Iterator

  Sequentially access the elements of a collection

 

2

  Mediator

  Defines simplified communication between classes

 

1

  Memento

  Capture and restore an object's internal state

 

5

  Observer

  A way of notifying change to a number of classes

 

3

  State

  Alter an object's behavior when its state changes

 

4

  Strategy

  Encapsulates an algorithm inside a class

 

3

  Template Method

  Defer the exact steps of an algorithm to a subclass

 

1

  Visitor

  Defines a new operation to a class without change

 

 

 

michaelfang 發表在 痞客邦 PIXNET 留言(0) 人氣()

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 規則相同,防止不一致的縮寫


 

michaelfang 發表在 痞客邦 PIXNET 留言(0) 人氣()