In this blog post for beginner programmers I have tried to collect a few very important pieces of advice on naming variables, data types, methods and so on in C#. Each piece of advice comes with a few examples of correct and incorrect naming.
Because C# distinguishes between upper and lower case in identifiers (which is not true of every language), we can use capital letters as separators in names that contain more than one word. Unlike other languages, where the most common way of separating words is the "_" character, the accepted practice in C# is to separate the words in a name using capital letters. There are several naming styles, depending on how the capitals are used. The more important ones are:
- PascalCasing - the first letter of the identifier is capital, and every following word in the name also starts with a capital letter. Examples: BookCoverColor, NextElement, etc.
- camelCasing - the same as PascalCasing, except that the first word (or abbreviation) is all lower case. Examples: firstParameter, htmlText, ioStream, etc.
- UPPERCASE - every letter in the identifier is capital. In some languages this style is used together with "_" for constant names. In C#, PascalCasing is more commonly recommended for constants, which is why UPPERCASE is almost never used in C#.
- lowercase - every letter in the name is lower case. Not used in C#.
Naming advice
- Of course, the most important advice is to name everything in the most meaningful way possible, no matter how few lines of code use it.
Correct: bytesCount, GetStudentsWithExcellentGrade
Wrong: count, varr, n1, p, get, …
Loop variables (i, j, k, …) are an exception to this rule, and only when there really is no more meaningful name for them (for example: arrayIndex). - Do not use a language other than English for naming. Everyone knows English, but not everyone understands Bulgarian.
Correct: SongName, CarModel
Wrong: ИмеНаПесен, МоделКола (Bulgarian for "SongName" and "CarModel") - Use PascalCasing for the names of namespaces, classes, interfaces, methods, properties, events, enumerations, enumeration values, public fields, constants, delegates and so on.
Correct: FontSize
Wrong: fontSize, fontsize, font_size, FONTSIZE - Use camelCasing for the names of variables and parameters.
Correct: string name; DoSomething(string firstParameter) { }
Wrong: string Name; DoSomething(string FirstParameter, string _secondParameter) - Two-letter abbreviations may be written with two capital letters. If the abbreviation starts a camelCasing identifier, both letters must be lower case.
Correct: using System.IO; public void UseIO(Stream ioStream)
Wrong: public void UseIo(Stream iOStream)
This rule is not universally accepted, and two-character abbreviations are sometimes written with the first letter capital and the second lower case. Example: DbContext. - For abbreviations of three or more letters, only the first letter is capital. In names that require camelCasing, if the identifier begins with an abbreviation, that first word must be written entirely in lower case.
Correct: public string ParseHtml(string html)
Wrong: public string ParseHTML(string hTML) - Do not use capital letters as separators inside compound words.
Correct: Callback, Endpoint, Hashtable, Metadata, Placeholder, Namespace, FileName
Wrong: CallBack, EndPoint, HashTable, MetaData, PlaceHolder, NameSpace, Filename - Use names that are easy to understand, with the words in the right order.
Correct: HorizontalAlignment, SongName
Wrong: AlignmentHorizontal, NameSong - A longer, understandable name is better than a short, unclear one.
Correct: CanScrollHorizontally
Wrong: ScrollableX - Use only the Latin letters a-z, A-Z and the digits 0 to 9. Although the C# compiler allows almost every character in the Unicode table to be used in a name, using anything other than Latin letters and digits is not recommended.
Correct: ConstantName, FirstName
Wrong: CONSTANT_NAME, s_FirstName - Avoid using C# reserved words as identifiers.
Correct: ClassName, variableName, IsPublic
Wrong: class, var, public
Sometimes, however, using a reserved word as a name cannot be avoided. One example is passing the HTML class attribute as a helper parameter in ASP.NET MVC. In that case you put the @ character in front of the identifier, and the compiler no longer treats it as a keyword. Example: string @class = "class-name"; - Do not shorten words. Do not use abbreviations unless they are widely accepted and can be understood by most programmers.
Correct: GetWindow, FacultyNumber, HtmlContent
Wrong: GetWin, FN, HyperTextMarkupLanguageContent
It is hard to say which acronym is widely accepted and which is not. If you do not know the acronym yourself, it is probably not a good idea to abbreviate it. - Name classes and structures with singular nouns.
Correct: struct Student, class FileReader, class Console
Wrong: struct Students, class FileRead, class ConsoleReadWrite - Name interfaces with adjectives or nouns, using the prefix "I" and the suffix "-able".
Correct: interface IReadable, interface IEnumerable, interface IObjectContextAdapter
Wrong: interface Readable, interface Enumerable - Do not put Get and Set prefixes on methods and properties in classes. Use properties instead of getters and setters (the way Java programmers do).
Correct: public string Name { get; set; }
Wrong: public string getName() { … }; public void setString() { … };
There is a great deal more advice on choosing the best names in C#. I recommend going carefully through the resources below, because a real .NET ninja writes high-quality code, and there is no high-quality code without good naming.
Resources
Most of the naming advice here comes from the book Framework Design Guidelines, which I recommend to anyone who has decided to work seriously with the .NET Framework.
Another useful resource on the subject is MSDN, and more specifically:
