Saturday, September 5, 2009

Coding convention – C#: Naming convention

Naming convention is the most important part of standard coding convention. By following a common standard to name identifiers, a common medium of communication is established between the developers. Identifiers are easily recognizable along with their context if a developer knows the standard. Naming convention deals with naming almost every programming objects starting from basic variables to large structures as classes and namespaces.

Before listing the standards, it is necessary to understand three basic terms used in this context:

Pascal case

The first letter of each word in the name is in uppercase and the rest of the word in lowercase. If a name consists of more than one word, the first letter of each distinct word should be in uppercase.

For example: BlackColorCode
This name constitutes of three words ‘black’, ‘color’ and ‘code’, hence their first letters ‘B’, ‘C’ and ‘C’ are in uppercase respectively.

Camel case

All letters of the first word in the name is in lowercase. If a name consists of more than one word, the first letter of each distinct word (except the first word) should be in uppercase.

For example: blackColorCode
This name constitutes of two words ‘black’, ‘color’ and ‘code’. All letters of first word ‘black’ is in lowercase. However the following words (in this case ‘color’ and ‘code’) have their first letters, both ‘C’, in uppercase.

Hungarian notation

Hungarian notion is a notation used to name objects by prefixing their type in the name.

For example: strBlackColorCode
It identifies the variable as a string type, where ‘str’ is an abbreviation used for string type. With typed languages like C#, Microsoft has chosen to get rid of Hungarian notation and so shall we. We will use Hungarian notations sparingly (almost try not to use at all).

Naming convention

In the table below, let us have a look at how to name various programming units in C#. The first column has the programming unit, the second has the details about which case to be used (and special conditions, if any) and last column has an example to illustrate.

Identifier
      Convention
Namespace
  • Use Pascal case
  • No abbreviation or underscore
  • Format:
    {CompanyName}.{Technology}[.Format][.Design]

    Example(s):
    MyCompany.Service.Data

Class
  • Use Pascal case
  • No abbreviation or underscore
  • No prefix or suffix (avoid Hungarian notation; however and Exception class should have a suffix “Exception”)

    Example(s):
    FileLogger

Interface
  • Use Pascal case
  • No abbreviation or underscore
  • Name should be same as the implementing default class
  • Prefix character ‘I’ to indicate an interface

    Example(s):
    IFileLogger

Enumerator
(Type and Values)
  • Use Pascal case for both type and values
  • Use singular name for types (exception: use plural for type representing bitfields)
  • No abbreviation or underscore
  • No prefix or suffix (avoid Hungarian notation)

    Example(s):
    LoggingLevel
    High, Medium, Low
Property
  • Use Pascal case
  • No abbreviation or underscore
  • No prefix or suffix (avoid Hungarian notation)
  • Name of a property and underlying type should be same (only differing in case)

    Example(s):
    ListKey
Variable
  • Use Camel case
  • No abbreviation or underscore
  • No prefix or suffix (avoid Hungarian notation)

    Example(s):
    firstName
Constant
  • All letters in uppercase
  • No abbreviation
  • Use underscore to separate words (or to identify groups)
  • No prefix or suffix (avoid Hungarian notation)

    Example(s):
    MATURITY_AGE

Method
  • Use Pascal case
  • No abbreviation or underscore
  • No prefix or suffix (avoid Hungarian notation)

    Example(s):
    LoadProjects

Parameter
  • Use Camel case
  • No abbreviation or underscore
  • No prefix or suffix (avoid Hungarian notation)

    Example(s):
    fileName

Event
  • Use Pascal case
  • No abbreviation
  • Suffix “EventHandler” for event handlers and “EventArgs” for event arguments
  • Method must have two arguments: sender as the object that raised the event and
    e as the appropriate event class
  • Name events with a verb (-ing form for pre-events and –ed form for post-events)

    Example(s):
    SaveButton_Clicked
    MyPanel_Painting
Attribute
  • Use Camel case
  • No abbreviation or underscore
  • Suffix the word “Attribute”

    Example(s):
    ObsoleteAttribute


Besides the standards mentioned in the chart above, the following should be taken care of:
  • Do not declare two identifiers with same name only differing in case.
  • Name an identifier according to its meaning not its type.
  • Always add “EventHandler“ to delegates related to events and “Callback” to delegates related to callback methods.

0 comments: