Software Design - Variable, Object and Control Naming Guidelines

When programming it is very highly recommended and good practice to follow good variable, object and control naming guidelines for quality.

By Tim Trott | Software Engineering | March 21, 2010

When programming it is very highly recommended and good practice to name all classes, variables and components properly and meaningfully.

Variable Naming Guidelines

Variables should always be given a meaningful name, but not too verbose. The maximum recommended variable length is 31 characters, although 12-14 should be the realistic maximum.

Names such as Temp should be avoided, even if it is just a temp holding variable. Instead, you should use tmpCounter or tmpFilename.

Reserved words cannot be used as variable names.

You should never use single-letter identifiers, except for i, j and k which should only be used for loop control, and when coding mathematical formulae where the letters should reflect the mathematical notation.

C#
for (int i = 0; i < 10; i++)
{
  for (int j = 0; j < 10; j++)
  {
    for (int k = 0; k < 20; k++)
    {
  }
}
C#
int e;
int m = 200;
int c = 3000000;
int c2 = Math.Pow(c, 2); / c squared
e = m * c2;

Use of capitalisation should reflect an agreed naming convention or use of an existing convention such as Hungarian notation  or Camel Case . The code on this website uses Pascal Style or CamelCase for public methods and properties, with camelBack used for private and local.

Microsoft recommends CamelCase for most identifiers, and camelBack for parameters and variables and this is a shared convention for all the .NET languages. Microsoft further recommends that no type prefix hints (also known as Hungarian notation) are used, such as strMyString, intMyInt etc.

Object Naming Guidelines

Objects should be given meaningful names that follow the above rules.

Class and Interface Naming Guidelines

Classes and interfaces should also follow the above rules.

Interfaces should start with a capital I followed by the variable name, for example, IInterface or ISomething. When creating a class it is much easier to tell that an interface is being implemented this way.

Form Objects and Control Naming Guidelines

When you have a complex form, it is difficult to differentiate between Button1, Button2, Button3, TextBox1, TextBox2, TextBox3 and so on when you are coding. It is much better to give these components proper names to avoid confusion.

The recommendation is to use a three-letter prefix (lowercase) for the type of control (e.g. btn for Button, txt for TextBox, lbl for Label and so on) followed by a meaningful name (e.g. btnSubmitData, txtUsername or lblPassword). You should also rename the form to frmMain or an appropriate name for the function of the form.

Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

We respect your privacy, and will not make your email public. Learn how your comment data is processed.