Website development and design blog, tutorials and inspiration

Variable, Object and Control Naming Guidelines

Good programming practice includes the consistant naming of objects. Here are some recommendations.

By , Written on in Software Engineering

Variable, Object and Control Naming Guidelines

369 words, estimated reading time 2 minutes.

In any programming language, it is very highly recommended, as well as good practice, to name all variables, components and forms properly and meaningful to ensure ease of readability and maintainability.

Variables

Variables should always be given a meaningful name, but not too verbose. 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, with the exception of 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.

  1. for (int i = 0; i < 10; i++)
  2. {
  3. for (int j = 0; j < 10; j++)
  4. {
  5. for (int k = 0; k < 20; k++)
  6. {
  7. }
  8. }
  1. int e;
  2. int m = 200;
  3. int c = 3000000;
  4. int c2 = Math.Pow(c, 2); // c squared
  5. 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. Code on this website uses Pascal Style or CamelCase for public methods and properties, with camelBack used for private and locals.

Microsoft recommends CamelCase for most identifiers, 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.

Objects

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

Classes and Interfaces

Classes and interface 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 Controls

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 much 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.

Last updated on: Wednesday 21st June 2017

Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.