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 TrottSoftware Engineering • March 21, 2010
Software Design - Variable, Object and Control Naming Guidelines

When programming, one of the most crucial aspects is to name all classes, variables, and components in a manner that is not only proper but also meaningful. This practice is not just a recommendation, but a fundamental principle that significantly impacts the readability and maintainability of your code.

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 temporary holding variable. Instead, opt for more descriptive names like tmpCounter or tmpFilename. This simple practice can significantly improve the clarity and readability of your code, making it easier for you and your team to work with.

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;

For capitalisation, it is important to follow an agreed naming convention. One widely used convention is Hungarian notation, which uses a prefix to indicate the variable's data type. Another popular convention is Camel Case, which capitalizes the first letter of each word except the first. The code on this website, for instance, uses Pascal Style or CamelCase for public methods and properties, with camelBack used for private and local.

Microsoft, a leading authority in software development, recommends CamelCase for most identifiers, and camelBack for parameters and variables. This is a shared convention for all the .NET languages. Microsoft further advises against the use of type prefix hints (also known as Hungarian notation), such as strMyString, intMyInt etc. Following these industry standards not only ensures consistency but also makes your code more understandable for other developers.

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.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. 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?

New comments for this post are currently closed.