Saturday, April 13, 2013

C# Naming Convention Design Guideline

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.

  1. Namespace naming convention: CompanyName.SolutionName.ProjectName.ModualName (Pascal Case)
    • MicNets.Blog.Web
    • MicNets.Blog.DAL
    • MicNets.Blog.Biz
  2. Class naming convention: Use a noun or noun phrase to name a class, and avoid using the underscore character "_" (Pascal Case)
    • Account
    • BookStore
    • Department
  3. Interface naming convention: Prefix Interface name with a character "I", and avoid using the underscore character "_" (Pascal Case)
    • IAccount
    • IStore
    • IDepartment
  4. Method parameters & Local method variables naming convention: Use descriptive parameter name (Camel Case)
    • depositAmount
    • days
    • isWeekend
  5. 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
  6. Enumerations naming convention: Use a singlton noun begins with an "N" prefix (Pascal Case)
    • NAccountType
    • NCategory
    • NFontSize
  7. Private Member variables naming convention: Prefix private member variable with an underscore "_" (Camel Case)
    • _price
    • _balance
    • _description
  8. 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; }
        }
    
  9. Methods/Functions naming convention: Use verbs or verb phrases to name methods or functions (Pascal Case)
    • LoadAllApplication()
    • InsertOneCustomer()
    • ReserveTicket()
  10. 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;
        }
    
  11. 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"]
  12. 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++)
  13. 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;
  14. 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