First of all, this is not a mandatory topic. You can name your variables whatever you like. However, you will soon come to understand that choosing significant variable names is a very good programming practice.
If you ask me, the term “generally accepted programming naming convention” is practically a joke. Even with long established naming conventions, there is always some smart guy or company out there that thinks they can re-invent the Universe, by modifying/creating new naming conventions. There is a general war out there among the programmers regarding which naming conventions are “good” and which are “evil”, so it is quite difficult to establish a standard.
In my opinion, it is best to stick to the practices that appeared a long time ago (possibly, the first practices that ever appeared) and that proved their efficiency by passing the test of time.
Hence, there are a few “rules” that you may want to adopt:
DO
Use PascalCasingPascalCase denotes the practice of writing compound words or phrases such that the first letter of each concatenated word is capitalized. No other characters are used to separate the words, like hyphens or underscores. Example: SomeNameUsingPascalCasing for class names and method names:
|
5
6
7
8
9
10
11
12
13
14
15
16
|
class
FileManipulation
{
public void DeleteFile()
{
// ...
}
public void CreateFile()
{
// ...
}
}
|
Reason: keeping consistency with the Microsoft’s .NET Framework and easiness to read.
Use camelCasingCamel case (also camelCase, CamelCase, camel caps or medial capitals) is the practice of writing compound words or phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, with no spaces or hyphens. Example: someNameUsingCamelCasing"> for method arguments and local variables.
|
5
6
7
8
9
10
11
12
|
class
UserLog
{
public void Add(string[] logEvent)
{
int itemCount = logEvent.Length;
// ...
}
}
|
Reason: keeping consistency with the Microsoft’s .NET Framework and easiness to read.
Use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase):
|
7
8
9
|
HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;
|
Reason: consistent with the Microsoft’s .NET Framework. Two successive capital letters would visually steal too much attention.
Use predefined type names instead of system type names like Int16, Single, UInt64, etc:
|
7
8
9
10
11
12
13
14
15
|
// Correct
string firstName;
int userAge;
bool isCreated;
// Avoid
String firstName;
Int32 userAge;
Boolean isCreated;
|
Reason: Consistent with the Microsoft’s .NET Framework, makes code more natural to read, shortens typing.
Use noun or noun phrases to name a class:
|
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class
Employee
{
// ...
}
public class
UserData
{
// ...
}
public class
DataCollection
{
// ...
}
|
Reason: Consistent with the Microsoft’s .NET Framework, easy to remember, casing differentiates classes by variables
Use letter I as prefix for interfaces. Interface names are noun (phrases) or adjectives:
|
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public interface
IColor
{
// ...
}
public interface
IAttackable
{
// ...
}
public interface
IDisposable
{
// ...
}
|
Reason: Consistent with the Microsoft’s .NET Framework.
Use organized namespaces with a clearly defined structure
|
13
14
|
namespace Company.Employee.Data.Age { }
namespace Company.Product.Price { }
|
Reason: Consistent with the Microsoft’s .NET Framework. Maintains good organization of your code base.
Use capital letters for constants or readonly variables:
|
7
8
9
10
11
|
// Correct
public const int MAXIMUM_CHARACTERS = 256;
// Avoid
public const int maximumCharacters = 256;
|
Reason: consistent with the Microsoft’s .NET Framework. Quickly identifies a read-only value.
DO NOT
Use Hungarian notation Hungarian notation is an identifier naming convention in computer programming, in which the name of a variable or function indicates its type or intended use. or any other type identification in identifiers:
|
7
8
9
10
11
12
13
|
// Correct
int age;
string name;
// Avoid
int iAge;
string strName;
|
Reason: consistent with the Microsoft’s .NET Framework and Visual Studio IDE makes determining types very easy (via tooltips). In general you want to avoid type indicators in any identifier.
Use abbreviations. Exceptions: abbreviations commonly used as names, such as Id, Xml, Ftp, Uri:
|
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// Correct
UserGroup userGroup;
Assignment employeeAssignment;
// Avoid
UserGroup usrGrp;
Assignment empAssignment;
// Exceptions
CustomerId customerId;
XmlDocument xmlDocument;
FtpHelper ftpHelper;
UriPart uriPart;
|
Reason: Consistent with the Microsoft’s .NET Framework and prevents inconsistent abbreviations.
Use underscores in identifiers. Exception: you can prefix private static variables and constants:
|
7
8
9
10
11
12
13
14
15
16
17
|
// Correct
DateTime clientBirthday;
TimeSpan timeLeft;
// Avoid
DateTime client_Birthday;
TimeSpan time_Left;
// Exceptions
DateTime _registrationDate;
const int BOILING_TEMPERATURE = 100;
|
Reason: Consistent with the Microsoft’s .NET Framework. Makes code more natural to read (without ‘slur’). Also avoids underline stress (inability to see underline).
Many of the concepts presented in these examples are still unfamiliar to you. Do not concern yourself with that, for now. However, once in a while, when you will become more advanced and you will develop the usual and inevitable programming “habits”, do come back and refer to this article again, before “the evil takes roots too deep” and you find yourself stuck with a poor choice in choosing significant variable names that will be much harder to correct.
The concepts explained in this lesson are also shown visually as part of the following video: