Coding conventions serve the following purposes:

  • They create a consistent look to the code, so that readers can focus on content,
    not layout.
  • They enable readers to understand the code more quickly by making assumptions based
    on previous experience.
  • They facilitate copying, changing, and maintaining the code.

Before we look into Naming convention its important to understand difference between Pascal and camel casing.

As per Pascal casing starting character of every word must be in upper case. For e.g.BreakFast. As per camel casing all character in the first word must be lowercase and remaining word starting character must be uppercase. For e.g. breakFast.

 

Use PascalCasing for class names and method names.


public class Triangle
{
    public void CalculateArea()
   {
        //...
    }
}

 

Use camelCasing for method arguments and local variables.


public class UserLog
{
    public void Add(LogEvent logEvent)
    {
        int itemCount = logEvent.Items.Count;
        // ...
    }
}

 

Avoid using Abbreviations.

// Correct
UserGroup userGroup;
Assignment employeeAssignment;
 
// Avoid
UserGroup usrGrp;
Assignment empAssignment;

 

Do not use Underscores in identifiers. Exception: you can prefix private
variables with an underscore.

// Correct
public DateTime clientAppointment;
public TimeSpan timeLeft;
 
// Avoid
public DateTime client_Appointment;
public TimeSpan time_Left;
 
// Exception
private DateTime _registrationDate;

 

Do prefix interfaces with the letter I


public interface IShape
{
}
public interface IShapeCollection
{
}
public interface IGroupable
{
}

 

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()
    {
        // ...
    }
}

3 thoughts on “Naming Convention Best Practices

  1. Nice way to keep code organize. When I started coding in my Senior Year I used to give any random name to variable and as my program got bigger I find it hard to read it.

    This is simply great – Oliver @@

Leave a Reply

Your email address will not be published. Required fields are marked *