.Net Coding Standards Part1 : Naming conventions and style


As .Net developers we tend to forget this key ingredient in our lives. Quite frequently, we end up in a coding mess. It would be good to take into account of coding standards before you start writing your first letter. It took me a lot to understand them. In no way you could learn them one shot. It takes time and experience. But never let it go off sight. Let me give you a hand out here.

Try to skim through it 3 – 5 times a day, for a week-like a medicine. It should get imprinted by then.

The following are some coding standards, i used to follow in my coding. Hope it would be useful to you all.

1.    When an identifier consists of multiple words, do not use separators such as underscores (_) between words. Instead use casing to indicate the beginning of each word.

2.    Use Pascal casing for type and method names and constants.

public class DataAccess
{
protected const string LastModifiedOn = “LastModifiedOn”;

public void CreateAdapter()
{
}
}

3.    Use camel casing for local variable names and method arguments.

public void StartTransaction(string environmentName)
{
DBConnection connection;
}

4.    Capitalize both characters of two-character acronyms, except the first word of a camel cased identifier.

5.    Capitalize only the first character of acronyms with three or more characters, except the first word of a camel cased identifier.

6.    Do not use abbreviations in identifiers. The two abbreviations that can be used in identifiers are ID and OK. In Pascal casing identifiers, they must appear as Id and Ok. If used as the first word in a camel cased identifier, they must appear as id and ok respectively.

7.    Prefix interface names with I.

public interface IBusinessFacade
{
void DeleteColumnLayout(ColumnLayoutInfo info);
}

8.    Prefix private member variables with an underscore (_). Use camel casing for the rest of a member variable name following the underscore (_).

public class DataAccessManager
{
private int _number;
}

9.    Suffix custom attribute classes with Attribute.

10.    Suffix custom exception classis with Exception.

11.    Name methods using verb-object pair, such as ShowDialog().

12.    Methods with return values should have a name describing the value returned, such as GetObjectState().

13.    Use descriptive variable names.

  • a.    Avoid single character variable names, such as i or t. Use index or temp instead.
  • b.    Do not use Hungarian notation.
  • c.    Do not abbreviate words (such as num instead of number).

14.    Always use language (C#) predefined types rather than the aliases in the System namespace.

object NOT Object
string NOT String
int    NOT Int32

15.    With generics, use capital letters for types. Reserve suffixing Type when dealing with the .NET type Type.

// Correct.
public class LinkedList<K, T>
{
}

// Not correct.
public class WrongLinkedList<KeyType, DataType>
{
}

16.    Use meaningful namespaces such as the product name or the company name. The general format for a namespace is as follows:

<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>]

17.    Avoid fully qualified type names. Use the using statement instead.

18.    Do not place a using statement inside a namespace.

19.    Group all framework namespaces together and put custom or third-party namespaces underneath.

using System;
using System.Collections.Generic;
using System.Text;

using Getronics.SampleLibrary;

20.    Use delegate inference instead of explicit delegate instantiation.

public delegate void TransactionalMethod();

private TransactionalMethod _method = Delete;

private static void Delete()
{
}

21.    Maintain strict indentation of 4 spaces (the default in Visual Studio 2005 Team Edition for Software Developers). Do not use tabs or nonstandard indentation, such as one space. The indentation should be uniform across all files.

22.    Indent comments at the same level of indentation as the code you are documenting.

23.    All comments use the English (US) language and should pass spell checking. Misspelled comments indicate sloppy development.

24.    All comments start with the single line comment characters //.

25.    All member variables of a class or structure must be declared at the top, with one line separating them from the properties or methods.

internal class DataAccessEnvironment
{
private DatabaseFactory _factory;
private long _baseValue;
private int _lowValue;

public DataAccessEnvironment()
{
}
}

26.    Declare local variables at the top of a block statement.

27.    A file name must reflect the class it contains.

28.    A class should use the following layout.

public class ClassLayout
{
#region Member variables
#endregion

#region Constructors
#endregion

#region Properties
#endregion

#region Methods
#endregion
}

29.    When using partial types and allocating a part per file, name each file after the logical part that part plays. For example:

// In file PartialClass.cs
public partial class PartialClass
{
}

// In file PartialClass.Designer.cs
public partial class PartialClass
{
}

30.    Always place an open curly brace ({) on a new line.

31.    With anonymous methods, mimic the code layout of a regular method, aligned with the anonymous delegate declaration (complies with placing an open curly brace on a new line).

public delegate void SampleMethod(string name);

public void InvokeMethod()
{
SampleMethod sampleMethod = delegate(string name)
{
MessageBox.Show(name);
};

sampleMethod(“Hello”);
}

32.    Use empty parentheses on parameter-less anonymous methods.

Also go through : .Net Coding Standards Part2 : Coding Practices

6 thoughts on “.Net Coding Standards Part1 : Naming conventions and style

  1. Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn’t appear.

    Grrrr… well I’m not writing all that over again. Regardless, just
    wanted to say excellent blog!

Feel free to leave a reply here...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s