C# naming convention design guideline
A consistent naming pattern is one of the most important elements of predictability and discoverability in a managed class library. Widespread use and understanding of these naming guidelines should eliminate unclear code and make it easier for developers to understand shared code.
Pascal case: The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized.
Camel case: The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.
Upper case: Only use all upper case separated by underscore "_" for identifiers.
-
Namespace naming convention: CompanyName.SolutionName.ProjectName.ModualName (Pascal Case)
- MicNets.Blog.Web
- MicNets.Blog.DAL
- MicNets.Blog.Biz
- Class naming convention: Use a noun or noun phrase to name a class, and avoid using the underscore character "_" (Pascal Case)
- Account
- BookStore
- Department
- Interface naming convention: Prefix Interface name with a character "I", and avoid using the underscore character "_" (Pascal Case)
- IAccount
- IStore
- IDepartment
- Method parameters & Local method variables naming convention: Use descriptive parameter name (Camel Case)
- depositAmount
- days
- isWeekend
- Constants naming convention: Use all uppercase with noun words separated by underscore "_" to name constant (Upper Case)
- MAXIMUM_DAILY_TRASACTION_AMOUNT
- HOURS_OF_DAY
- MINNUTES_OF_HOUR
- Enumerations naming convention: Use a singlton noun begins with an "N" prefix (Pascal Case)
- NAccountType
- NCategory
- NFontSize
- Private Member variables naming convention: Prefix private member variable with an underscore "_" (Camel Case)
- _price
- _balance
- _description
- Properties naming convention: Must exactly match the member variable name without an underscore "_" prefix (Pascal Case)
private decimal _price; public decimal Price { get { return _price; } set { _price = value; } }
- Methods/Functions naming convention: Use verbs or verb phrases to name methods or functions (Pascal Case)
- LoadAllApplication()
- InsertOneCustomer()
- ReserveTicket()
- Code blocks opening and closing braces: Code blocks always be opened on the next line as a statement.
for (int i = 0; i < 10; i++) { // do something; }
- Spacing: Spaces improve readability by decreasing code density.
- Use a space after a comma between function parameters
- Bad: GetCustomerList(int CountryID,int StateID,bool blnMale)
- Good: GetCustomerList(int CountryID, int StateID, bool blnMale)
- Use a space before flow control statements
- Bad: while(true)
- Good: while (true)
- Use a space before and after comparison operators
- Bad: if (amountDeposit>=amountWithdraw)
- Good: if (amountDeposit >= amountWithdraw)
- Do not use spaces before or after the parenthesis and function parameters
- Bad: SelectAllBook( string storeName )
- Good: SelectAllBook(string storeName)
- Do not use spaces between a function name and parenthesis
- Bad: InsertOne ()
- Good: InsertOne()
- Do not use spaces inside brackets
- Bad: Customer[ "Index" ]
- Good: Customer["Index"]
- Use a space after a comma between function parameters
- Magic number: No magic number should be used in coding, except -1, 0 and 1. Using descriptive constant or variable to hold the value.
- Bad: if (NumOfPaymentPerYear == 12)
- Good: int const MONTHLY_NUM_PAYMENT_PER_YEAR = 12; if (NumOfPaymentPerYear == MONTHLY_NUM_PAYMENT_PER_YEAR)
- Bad: for (int i = 0; i < 10; i++)
- Good: int maxCount = 10; (int i = 0; i < maxCount; i++)
- Local variable: Local variable names should be descriptive enough. And initialize local variables as soon as they are declared
- string errorMeg = string.Empty;
- decimal interestRate = decimal.Zero;
- int numOfPayment = 0;
- bool blnIsWeekend = false;
- The length of class and method: Class and method should be kept them short and neat.
- Any classes with more than 2000 lines of coding should be broken down to other sub classes.
- Any methods that have more than 30 lines of coding should be broken down to other sub methods.
No comments:
Post a Comment