Naming Guidelines in .NET


Commenting and following a uniform naming guidelines in your code is one of good programming practices to make code more useful. I've been programming with Microsoft products for almost 5 years. I don't know about you but I have faced many problems during integration and bug fixes due to developers not following a uniform naming and comments.

In the previous versions of Visual Studio, Microsoft used to suggest Hungarian notations to write code and encourage developers to follow same guidelines to write a unique format code. For example -

Variable  Notation
CString szString
char cMyChar
char* pMyChar
long lMyVariable
LPCSTR lpStr
int nMyNumber

In latest release of .NET and it's programming languages, Microsoft has changed it's guidelines to write a unique format of code. If you have even programmed in Delphi, you will see some similarities between the new format and Delphi (Pascal) code.

To avoid code conflicts and make your code more useful, following name guidelines is a good programming practice. Ok, here are some of very common guidelines.

Naming Variables, Methods, and Properties

The name of variables, properties and methods should start with a capital letter and name should speak about the purpose of variable.

Variable .NET Name Hungarian Notation Description
CString EmployeeName szName Name of an employee.
int AttendanceCounter nCounter A counter of type long.
long NumberOfBytes lBytes A long type variable stores bytes.

Sometimes we used to use "_" in our definitions. For example, Add_Data(int a, int b). You should define Add_Data as AddData. According to new guidelines, this is not a good programming practice. Why not? Well .. there is nothing wrong with it. It's just not Microsoft standard and you don't necessarily have to follow these guidelines but there are some places where you will see following these guidelines make sense.

Personally, I like Hungarian notations better than this new pascal type notations. But any way, same rules apply on variables too. If you remember in hungarian notation, a boolean variable was defines as beginning with "b".

BOOL bFlag = TRUE;

New guidelines suggest not to Flag and "b".

bool IsFileFound = true;

You can browse MSDN for more details on new guidelines.

Naming Components and Assemblies

To avoid naming conflicts, following naming guidelines for your library (called assembly in .NET) is a good programming practice. Say you are a company called MindCracker and you are developing an assembly, which provides addition C# database classes. It would be better if you define your assembly name as MindCracker.DatabaseAssembly.ADOSet instead of MyAssembly.Database.ADOSet.

Say your assembly has a method, which reads a table and returns a DataSet. DataSet return_data() should be replaced with DataSet ReturnData().

You should follow a uniform order in names. For example, say you have two tables Employee and Manager and you have two methods. These methods append a record to the table. AppendEmployee and AppendManager is a good programming practice than AppendEmployee and ManagerAppend.

Looping

Align open and close braces vertically where brace pairs align, such as:

for (i = 0; i < 100; i++)
{
..
}

or

for (i = 0; i < 100; i++){
...
}

I prefer second method b/c it's easy to understand a block and nested blocks.
Same rule apply on other loops too.

If ... Then
If ... Then
...
Else
End If
Else
...
End If

can be replaced with

If ... Then
If ... Then
...
Else
...
End If
Else
...
End If

Up Next
    Ebook Download
    View all
    Learn
    View all