Coding Standards


Naming conventions

1.     Private fields (_camelCase)
private int _empSalary;

2.     Public/Protected fields (camelCase)
public int empSalary;
protected int empSalary;

3.     Property (PascalCase)
public int EmpSalary
            {
                        get { return _empSalary; }
                        set { _empSalary = value; }
            }

4.     Method (PascalCase)
public int SomeMethod ()
{
            return 1;
}

5.     Method arguments and local variables (camelCase)
public int SomeMethod (int empSalary)
{
            int empSalaryTemp = empSalary;
            return empSalaryTemp;
}

6.     NameSpace and Class (PascalCase)
namespace Employee
{
            public class EmpPay
            {

}
}

7.     Page name (lowercase with hyphen (‘-’) as seperator)
employee-details.aspx

8.     Prefix the id of control accordingly
btn for button ex: btnSubmit
rbn for radiobutton ex: rbnSelect
chk for checkbox ex: chkSelect
grv for gridview ex: grvData
txt for textbox ex: txtName
ddl for dropdown ex: ddlCountry

Coding best pratices

1.     Should write comments where every required.
2.     Do not have more than one class in a file.
3.     Do not exceed more than 50 lines in a method, if needed write it as a separate method and call it in the main method.
4.     Always use try catch finally blocks to handle exceptions.
5.     Use StringBuilder instead of string where you have to concatenate.
string str = “A”;
str += “B”;
str += “C”;
str += “D”;

Instead of the above we can write as below

StringBuilder sb = new StringBuilder();
sb.Append(“A”);
sb.Append(“B”);
sb.Append(“C”);
sb.Append(“D”);
6.     We can reduce the number of lines but writing if else as follows i.e.
instead  of writing it as
bool result;
if (x > 1)
{
            result = true;
}
else
{
            result = false;
}
we can write as
bool result = ((x > 1 ) ? true : false);

7.     Use transaction when you are executing more than one related database operations.
8.     Use public access modifier only when required.
9.     Avoid declaring methods with more parameters i.e.
instead of declaring as
private bool SomeMethod (int empId, string empName, decimal empName, int empAge, DateTime dob)
{
            return true;
}
we can write as
private bool SomeMethod (Employee emp)
{
}
Where Employee class contains the fields empId, empName, empName, empAge, dob.
10.  Use method overloading to reuse the code.