Friday, October 2, 2009

General Coding Guidline for .Net

General Coding Guidline for .Net

Importance of coding standards is increasing day by day because of the fact the people work more in teams than as a single coder. The code written by one person may be read by many, including to client for whom the code is written for. This demands the code to be more readable and consistent. By strictly following a coding standard, we can increase the readability of the code by many bounds.

Every group or individual follow their own coding standards. Microsoft provides its own coding standards.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconeventusageguidelines.asp

My style of coding is much like the standards set by Microsoft, however I beg to differ with the software giant in some areas. In this article I am discussing only the some basic concepts, but people who are interested I would suggest them to read the guidelines suggested by MS in link above.

Always remember that coding standards aren’t rules. They are mere suggestions.

1. Naming Conventions

Following a consistent naming pattern is one of the fundamental elements to increase the readability of code. One important aspect in this area is that NEVER use acronym (unless of course that acronym is widely known) or any other short form for variable name.

Note: Use meaningful, descriptive words to name variables etc.

For Example:
String stringAddr; // This is Wrong
String stringAddress; // This is Correct

Microsoft advocates on not using Hungarian notation and prepending datatype to the variable name. Incidentally this is one of areas where I would differ with MS. By attaching the datatype to variable name would allow much more understandability as you write long codes.

For Example:
String stringName;
int intAge;

Note: Never use special characters like “_” in names.

Just like Microsoft I would suggest you to use pascalCase for all local variables and function parameters. As for all global variables, Function name, class name etc, I would suggest you to use CamelCase. However, for constant variables, especially global constants, I would advice you to use full caps. Ex: Const int INTVALUE;



Note: Always make sure to select a name that won’t confuse you with a keyword

General rule for naming namespaces is to use the company name followed by the project name and optional feature or module name.
[CompanyName].[Project].[Module].[subsection]

For Example:
CalpineTechnologies.ProjectCalpine.Accounting
While using exception, we need mostly use ‘e’ as the exception object. However event handlers in Visual Studio .NET tend to use an "e" parameter for the event parameter to the call. To avoid a conflict, its better will use "ex" as a standard variable name for an Exception object.
The naming conventions can be summarized as follows

• Always append datatype to variable name.
• Use pascalCase for local variables.
• Use pascalCase for parameters.
• Use CamelCase for Global Variables;
• Use CamelCase for Class Name, Function Name, Property Names etc.
• Always use meaningful, descriptive names.
• Never use special characters like “_”.

2. Comments

• All Methods Should use XML document comments.
• All other comments in the code should use // style (two slash) instead of “/*”.(of course, VB.Net coders use “’”)

3. Exception Handling

• Never do try catch and do nothing.

For Example , below code is wrong coding practice.
Try
{
// your code
}
catch
{
return;
}

• Always give a friendly message to the user. For your use, you can log the actually error along with all possible details.
• Always use specific exception and not generic
• No need to use exception in all your methods. Use exception where there is possibility for an exception to raise. For example, writing to a file.

4. Some General Practices

• Always place an open curly bracket “{“on a new line. However, in case a block with single line code, you can use it same line.

Example: Both below examples are good styles

public String StringName
{
get{return stringFirstName}
set{string FirstName=value;}
}

public String StringName
{
get
{
return stringFirstName;
}
set
{
stringFirstName=value;
}
}



• Avoid methods with over 25 lines. But it doesn’t mean you should split a 26 lines code into 2. The idea behind this is to keep each function simple.

• Avoid using methods with over 5 arguments. If you need to pass more than 5 arguments, use structures for passing multiple arguments. This increase readability.

• Avoid Files with over 500 lines. Always try keep each file less than 500 lines of code. Of course , this is excluding machine generated code.

• Do not provide public or protected variables. It’s better to use properties. It would restrict the values, range of values passed onto a variable.

• Avoid using multiple namespaces in same file. A single file should contain a single namespace.

• Use String.Empty instead of “”

For Example

string stringAddress=””; // This is wrong
string stringAddress=String.Empty; // This is correct

• When building a long string, always use StringBuilder. This is because string is immutable, and when you use “+” to built string , its infact destroying and creating new string.

No comments:

Post a Comment