ASP.NET, Coding

Naming and Casing standards in .NET

Hungarian Notation:
Hungarian notation is the methodology of using typically a 3-character prefix on a variable name to signify something about the data type. For example:

        Dim strUserName As String
        Dim intCount As Integer
        Dim lngNumOfRecords As Long
        Dim arrItems As Array

This worked well when there weren’t that many data types. In .NET, using custom data structures and having far, far more types available in the .NET runtime – starts to render this method less useful. For example, consider these type names:

        Dim uddUserDetail As _
            New UserDetailDataForm()
        Dim cmnCommon As _
            New Common()
        Dim ccrClaim As _
            New CustomerClaimReference()

As the developer starts dealing with non-standard and custom data types, this notation becomes less useful. But worse, is that developers across a project might use different prefixes, which further de-values the prefix.

In addition to all of this, intellisense has come so far that all a developer need do, is hover the mouse over the keyword and basic information is displayed.

What should be used instead then? Well, there are some more specific rules on this – but generally, full names should be used. Avoid abbreviations and shortening names. For example:

        Dim userDetail As _
            New UserDetailDataForm()
        Private _common As _
            New Common()
        Dim claimReference As _
            New CustomerClaimReference()       

Camel-Case Notation:
Camel-case is the general term for using lowercase letters except for the starting letter of a syllable. For example: ThisIsCamelCase and thisIsAlso.

More commonly, when the first character is lowercase, but the rest follows this rule – that is commonly called camel-case.

Pascal-Case Notation:
Pascal-case is a specific version of camel-case notation. Pascal-case defines that the first character of every syllable is capitalized. For example: GetUserDetail or FirstName.

Using FxCop and naming standard messages:
When you use FxCop as a static code analysis and compliance tool, it is looking for a specific formula for naming standards of class members and arguments. Here is a summary:

Use Pascal-Case for (EveryFirstLetterCapitalized):

  • Class names, NameSpace names, Code File names
  • Public members (properties, methods, events, delegates, structs, constants)
  • Non-public methods, events, delegates, structs, constants

Use Camel-Case for (allButTheFirstSyllable):

  • Non-public field members
  • Arguments in methods, events, delegates
  • Local variables

VB-Specific:
VB.NET is one of the few non-compliant CLS (common language specification) languages. That generally means that it doesn’t respect case-sensitivity. This does throw a kink into this ruleset. Because in C#, you can have two different members as firstName and FirstName without causing any compiler error. In a CLS compliant language, there is a different between firstName and FirstName. In VB.NET, there is no difference. So although the following C# code is valid, you must work around this in VB.NET. This is typically done by keeping the same naming standard, but giving private members an underscore prefix. To follow the same naming notations throughout, we recommend the usage of an underscore for the private variables of VB.NET as well as C# as shown in the example below:

C#

    private string _firstName;
    public string FirstName
    {
                    get { return _firstName;}
                    set { _firstName = value;}
    } 

VB

    Private _firstName As String
    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(ByVal value As String)
            _firstName = value
        End Set
    End Property

Naming Conventions for Types & Members:

  • Class(Pascal-case): Should be named a noun, representing a thing. If it’s a data structure that represents a collection or array, it can be plural. If it inherits from a collection type, the suffix should be Collection. For example: UserCollection – would be a class that inherits from List(of User) / List.
  • Interface(Pascal-case): Should always begin with uppercase “I”. Such as ICustomer, IData, etc – and should follow the same general rules of a class, it should be a singular noun or an “able” suffix such as IConvertible, IFormattable, etc
  • Abstract Class(Pascal-case): Should follow the same conventions as a regular class. It should be designated in the XML Code Comments that it is an abstract. Something like “Abstract: Class that defines a customer.”
  • Method(Pascal-case): Should always be a verb phrase and ideally begin with the verb. InitializeData(), GetCustomer(), InsertRow(), etc.
  • Property(Pascal-case): Follows the same rules as a class, with the exception of the Collection suffix. For example: FirstName, CustomerAddress, or Orders (might point to a collection of orders).
  • Field(Camel-case): Follows the same rules as property except for the first letter which should be lower case. However it’s important to note that you should avoid using public fields, as you can’t apply error handling to the setting or getting of it’s value.
  • Private Field(Camel-case): All the private members should be prefixed with an underscore(” _ “). This is applicable for both C# as well as VB.NET. Do not use underscores for local variables.
  • Local Variable(Camel-case): Follows the same rules as private field except that it should NOT be prefixed with an underscore.
  • Constant Field(Pascal-case): Constants(both private and public) should be named using Pascal Case. An example is ThisIsAConstant. Note that first letter of each syllable is uppercase. Underscores and screaming caps should be avoided for constants. Most of the functionalities developers normally implement using constants can be very well done using Enums. Hence Enums should be used where-ever possible and are recommended over constants.
  • Event(Pascal-case): Should always be a verb phrase, usually ending in the present or past-tense verb. For example: DataChanged() or DataIsChanging() – where the latter example is the event that is fired before it happens. The former is fired after it happens.
  • Struct(Pascal-case): Follows the same rules as a class. However, use of structs should be limited to performance or compatibility reasons.
  • Enum(Pascal-case): Should always represent a noun, and be singular. Enums that will be used as a bitmask (and have the / [Flags] attribute) – should be the plural form. For example: Color for a regular enum or Colors for a bitmask enum.
  • Delegate(Pascal-case): Should represent a singular noun (a thing) and have the word Delegate as a suffix. For example: DataChangedDelegate().
  • EventArgs(Camel-case): Any class that inherits from EventArgs (or any subclass from it) – should have a suffix of EventArgs. Such as RoutedEventArgs or DataChangedEventArgs.
  • Method Parameter(Camel-case): Follows the same rules as local variable.