Monday, 20 June 2011

.NET coding standard part - 1

Hello friends, in coding practice there are some coding standards that we should follow to make code more readable and maintainable. I have compiled some coding standards that I have mentioned following.

Naming Conventions and Style

1. Use Pascal casing for type and method names and constants:

public class SomeClass
{
const int DefaultSize = 100;
public void SomeMethod()
{}
}


2. Use camel casing for local variable names and method arguments.
void MyMethod(int someNumber)
{
int number;
}

3. Prefix interface names with I
interface IMyInterface
{...}

4. Prefix private member variables with m_. Use Pascal casing for the rest of a member variable name following the m_.

public class SomeClass
{
private int m_Number;
}

5. Suffix custom attribute classes with Attribute.
6. Suffix custom exception classes with Exception.
7. Name methods using verb-object pair, such as ShowDialog().
8. Methods with return values should have a name describing the value returned, such as GetObjectState().
9. Use descriptive variable names.
a) Avoid single character variable names, such as i or t. Use index or temp
instead.
b) Do not abbreviate words (such as num instead of number).
10. Always use C# predefined types rather than the aliases in the System namespace.

For example:
object NOT Object string NOT String int NOT Int32

11. Use meaningful namespaces such as the product name or the company name.
12. Avoid fully qualified type names. Use the using statement instead.
13. Avoid putting a using statement inside a namespace.
14. Group all framework namespaces together and put custom or third-party namespaces
underneath.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; using MyCompany; using MyControls;

14. Maintain strict indentation. Do not use tabs or nonstandard indentation, such as one space. Recommended values are three or four spaces, and the value should be uniform across.
15. Indent comments at the same level of indentation as the code you are documenting.
16. All comments should pass spell checking. Misspelled comments indicate sloppy development.

Example:
Class Level Commenting
///

/// Data Aceess Layer class for SQL server only
///


public class DBManager : IDisposable
{
Method level commenting
///

///

///
///
/// example code here
///

///

///
///
///

public void BeginTransaction()
{
Some code here…
}

17. All member variables should be declared at the top, with one line separating them from the properties or methods.

public class MyClass
{
int m_Number;
string m_Name;

public void SomeMethod1()
{}
public void SomeMethod2()
{}
}

18. Declare a local variable as close as possible to its first use.
19. A file name should reflect the class it contains.
20. Always place an open curly brace ({) in a new line.

No comments:

Post a Comment