Variable, Object and Control Naming Guidelines
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 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.
- for (int i = 0; i < 10; i++)
- for (int j = 0; j < 10; j++)
- for (int k = 0; k < 20; k++)
- 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. 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 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