Naming Convention - Coding (Part 6)

The following points applies for all the sections that follows:

  • Never use all caps.
  • Only use Pascal or camel casing.
  • Never use Hungarian Notation.
  • Again, avoid using abbreviations unless the name is too long (exceptions: common abbreviations, medical abbreviations, banking abbreviations, etc…).
  • Avoid abbreviations longer than 5 characters.
  • Use uppercase for 2 letter abbreviation and Pascal case for longer ones.
              // Example
CountryEU
CountryGcc
  • Never add redundant or meaningless prefixes and suffixes.
// Bad Practice
            public enum LanguageEnum
            public class CEmployee
public struct ChangeStruct

  • Never include the class name in the property name.
// Bad Practice
            Customer.customerName
// Good Practice
            Customer.name
  • Always prefix Boolean variables and properties with either Can, Is or Has.

Classes or Structs

  • Use Pascal casing only.
  • Use a noun or noun phrase for class naming.
// Example
      Customer
   Employee
   StudentExam
   UniversityDegree

Methods

  • Use either Pascal or camel casing (Pascal recommended) for methods naming.
  • Prefix methods with either Can, Is or Has, if and only if the method’s purpose is to perform true – false checks.
  • Otherwise, use a verb or verb-object pair.
// Example
private void Execute()
private bool ReleaseSalaries()
public Customer GetCustomerInformation()
public bool IsUserBlocked(User currentUser)
public bool AddNewUser(string userName, 
                       SecureString password, 
                       string name, 
                       bool isActive)

Parameters (Methods, Constructors, etc...)

  • Use camel casing only.
  • Again, always prefix parameters of Boolean type with either Can, Is or Has.

Properties

  • Use either Pascal or camel casing (Pascal recommended).
  • Never prefix properties with get or set.
  • Should represent the entity it returns.
// Bad Practice
public int Employee { get; set; }
public int Customer { get; set; }
// Good Practice
public decimal Salary { get; set; }
public int EmployeeID { get; set; }
// Or Advanced Practice
public int EmployeeID { get; set; }
public Employee Employee
              {
                     get
                     {
                           if (EmployeeID != null)
                           {
                                  return new Employee(EmployeeID);
                           }
                           else
                           {
                                  return null;
                           }
                     }
                     set
                     {
                           this.Employee = value;
                           this.EmployeeID = value.EmployeeID;
                     }
}

Enums

  • Use Pascal casing for type, while either Pascal or camel casing (Pascal recommended) for options.
  • Use plural context for type.
  • Use a noun or noun phrase for type naming.
  • You may use all caps only for common abbreviations, medical abbreviations, banking abbreviations, etc…
// Example
public enum UniversityDegrees
              {
                     Bachelor,
                     Masters,
                     Phd
}
public enum Laguages
              {
                     Arabic = 1025,
                     Bosnian = 5146,
                     English = 1033,
                     Hindi = 1081,
                     Norwegian = 1044
}
public enum MicrosoftCertificates
              {
                     MCP,
                     MCAD,
                     MCTS,
                     MCITP,
                     MCT
}

Variables (Local, Instances and Class Variables)

  • Use camel casing only.
  • Use the underscore (_) at the beginning of your class variables naming.
  • Declare each variable independently – not in the same statement.
  • Never! Use one-character variable names (e.g. i, j, x).
  • Again:
  1. Never add redundant or meaningless prefixes and suffixes.
  2. Never include the class name in the variable name (whether local, instance or class variable).
  3. Always prefix Boolean variables with either Can, Is or Has.

General Coding Practices

  • Braces {} should always be on a new line.
// Example
if (!IsPostBack)
              {
               
              }
              else
              {

}
  • Always use braces in conditional if or if-else statements, even if it’s one line.
  • Use the following model to region-group class implementation parts:
  1. Member variables.
  2. Constructors.
  3. Enums.
  4. Properties.
  5. Methods.
  • Within the region-groups mentioned in the previous point, additionally region-group the inner definitions and/or instantiations according to their access modifiers PublicPrivate and Protected.
  • Never omit access modifiers, even though the default access modifier is private, it’s highly recommended to write it explicitly.
  • Use string.Empty to initialize strings or while assigning a string variable as empty.

Summary

We all know that this is a huge topic, something you can write books about. However, what I have tried to do in my posts series, is to highlight the main practices for self-documenting code, additionally to have a fast track (sort of) on the subject.
I hope you all enjoyed, Cheers!

Comments

Popular posts from this blog

Counting Down to ESPC21 Online

Machine Learning Explained!

Building JSON Objects in Microsoft Flow