You are on page 1of 12

Coding Standards

&
Best Practices

Balaji Uppala
buppala@nisum.com
Coding Standards
Why do we need?
Most of the developers are inclined towards
writing code for higher performance,
compromising reliability and maintainability. But
considering the long term ROI (Return On
Investment), efficiency and performance comes
below reliability and maintainability. If your code
is not reliable and maintainable, you (and your
company) will be spending lot of time to identify
issues, trying to understand code etc throughout
the life of your application.
Working Code VS Good Code
Sample code
Naming Conventions
Pascal Casing
First character of all words are Upper Case and other characters
are lower case
Example: BackgroundColor
Camel Casing
First character of all words, except the first word are Upper Case
and other characters are lower case.
Example: backgroundColor
Hungarian Notation
In earlier days most of the programmers liked it - having the
data type as a prefix for the variable name and using m_ as
prefix for member variables
Example: m_sName or sName
Naming Conventions
Class Name Standards
Use a noun or noun phrase to name a class.
Do not use a type prefix, such as C for class, on a class name.
Use Pascal casing.
Do not start any class name with letter I.
public class HelloWorld{..}
Method Name Standards
Use the verb-noun method for naming.
Use Pascal Casing
Avoid using Underscore in method names to separate words
void SayHello(string name)
Naming Conventions
Use Came casing for Property declaration.
Use Camel casing for variables and method
parameters
int totalCount = 0;
Use the prefix I with Camel Casing for interfaces.
Ex: IEntity
Do not use Hungarian notation to name variables.
Use Meaningful, descriptive words to name
variables. Do not use abbreviations.
Naming Conventions
Do not use single character variable names like i, n, s
etc. Use names like index, temp
Do not use underscores (_) for local variable names
Do not use variable names that resemble keywords.
Use Pascal Case for file names.
Use Upper case notation to declare constants.
const string SAMPLESTRING=hello;
Declare control with its prefix type.
Ex: btnClick
Best Programming Practices
Avoid writing very long methods. A method should
typically have 1~25 lines of code.
Method name should tell what it does. Do not use
mis-leading names
void SavePhoneNumber ( string phoneNumber )
Always watch for unexpected values
Do not hardcode numbers. Use constants instead
Do not hardcode strings. Use resource files.
Use #region to group related pieces of code together.
Best Programming Practices
Avoid using member variables. Declare local variables
wherever necessary and pass it to other methods.
Use String.Empty to represent empty string. String.Empty
do not create any empty object in memory.
Do not make the member variables public or protected.
Keep them private and expose public/protected Properties.
Error messages should help the user to solve the problem.
Avoid having very large files. If a single file has more than
1000 lines of code, it is a good candidate for refactoring.
Avoid public methods and properties, unless they really
need to be accessed from outside the class.
Best Programming Practices
Exception Handling
Commenting code
Code Reviews
Minimize usage of Session in Web
applications.
Best Programming Practices
Data Access
Use where clause to restrict number of rows
returned by the query.
Specify the columns to fetch from the table
instead of using * in select query.
Use Stored procedure instead of direct queries
from the code.
Avoid using cursors unless it is mandatory.
Use indexes to increase the performance of query.
More Information
http://msdn.microsoft.com/en-us/library/xzf533w0(v=VS.71).aspx
http://ramcrishna.blogspot.com/2008/09/dotnet-coding-standards.html

You might also like