Wednesday, April 22, 2009

C# Coding Style (1)

From lu yong_chao's blog:

Naming Conventions and Styles

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

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

2.Use camel casing for local variable names and method arguments:

int number;
void MyMethod(int someNumber){}

3.Prefix interface names with I:

interface IMyInterface{..}

4.Prefix private member variables with m_.

5.Suffix custom attribute classes with Attribute.

6.Suffix custom exception classes with Exception.

7.Name methods using verb/object pairs, such as ShowDialog( ).

8.Methods with return values should have names describing the values returned, such as GetObjectState( ).

9.Use descriptive variable names.

9.1.Avoid single-character variable names, such as i or t. Use index or temp instead.

9.2.Avoid using Hungarian notation for public or protected members.

9.3.Avoid abbreviating 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.With generics, use capital letters for types. Reserve suffixing Type for when dealing with the .NET type Type:

//Correct:public class LinkedList{...}//Avoid:public class LinkedList{...}

12.Use meaningful namespace names, such as the product name or the company name.

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

14.Avoid putting a using statement inside a namespace.

15.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;

16.Use delegate inference instead of explicit delegate instantiation:

delegate void SomeDelegate( );
public void SomeMethod( ){...}
SomeDelegate someDelegate = SomeMethod;

17.Maintain strict indentation. Do not use tabs or nonstandard indentation, such as one space. Recommended values are three or four spaces.

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

19.All comments should pass spellchecking. Misspelled comments indicate sloppy development.

20.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( ) {}
}

21.Declare a local variable as close as possible to its first use.

22. A filename should reflect the class it contains.

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

//In MyClass.cs
public partial class MyClass{...}
//In MyClass.Designer.cs
public partial class MyClass{...}

24.Always place an open curly brace ({) in a new line.

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

delegate void SomeDelegate(string someString);
//Correct:
public void InvokeMethod( )
{
SomeDelegate someDelegate = delegate(string name)
{
MessageBox.Show(name);
};
someDelegate("Juval");
}

//Avoid
public void InvokeMethod( )
{
SomeDelegate someDelegate = delegate(string name){MessageBox.Show(name);};
someDelegate("Juval");
}

26. Use empty parentheses on parameter-less anonymous methods. Omit the parentheses only if the anonymous method could have been used on any delegate:

delegate void SomeDelegate( );//Correct
SomeDelegate someDelegate1 = delegate( )
{
MessageBox.Show("Hello");
};
//Avoid
SomeDelegate someDelegate1 = delegate { MessageBox.Show("Hello"); };

No comments: