Thursday, March 26, 2015

Coding standards

Coding standards are the set of rules which makes the development of the code consistent and helps the developers to understand the code easily.They also helps us in enforcing the best programming practices.

Following are the some of the coding standards every programmer should follow.

1)Use exception handling and Logging.

2)Use naming conventions for classes,Interfaces,methods and variables.

3)Use object oriented programming principles.

4)Remove redundant code.

5)Add meaningful comments to Types and methods.

6)Avoid hard coded strings,instead use string constants.

7)If there is any lengthy method,more chances are that it is going to have errors.So re-factor them to smaller meaningful methods.

8)Use IsNullOrEmpty property of string to check if the value of the string is null or Empty.

9)Give meaningful names to classes and methods.

10)Use the Using statement to Dispose the objects as soon as they go out of scope.

11)Remove unused using's and assembly references.

12)Use #region to outline the block of code so that you can expand and collapse.

13)Every object(class) should have a single responsibility.

Ex:In below example,I have a DateManager class which is handling too many responsibilities.They are

a)Date manipulations and conversions.:These actions class supposed to be doing.I dont have any problem with this.

b)Writing exceptions to text file:This DateManager shouldn't be doing.Here the class is overloaded with multiple responsibilities.For proper maintenance and design(separation of concerns) this code should be moved to another class called Logger.cs

public class DateManager
{

public string ApendDates(string dateInput, string toDate)
{
try
{
var fromDateTime = DateTime.ParseExact(dateInput, "MM/dd/yyyy", new CultureInfo("en-US"), DateTimeStyles.None);
var toDateTime = DateTime.ParseExact(toDate, "MM/dd/yyyy", new CultureInfo("en-US"), DateTimeStyles.None);

return fromDateTime.ToString(CultureInfo.InvariantCulture) + "---" + toDateTime.ToString(CultureInfo.InvariantCulture);
}
catch (Exception ex)
{
const string filePath = @"C:\Exceptions.txt";

using (var writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
"" + Environment.NewLine + "Date :" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
}
return null;
}

public DateTime? ConvertToDateFormat(string dateInput)
{
try
{
var dateOutput = DateTime.ParseExact(dateInput, "MM/dd/yyyy", new CultureInfo("en-US"), DateTimeStyles.None);

return dateOutput;
}
catch (Exception ex)
{
const string filePath = @"C:\Exceptions.txt";

using (var writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
"" + Environment.NewLine + "Date :" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
}
return null;
}
}


14)Follow the KISS(Keep It Simple Stupid) and DRY(Don't Repeat Yourself) principles.

a)We should always try to keep the code simple and avoid complexity.

b)We should make sure that same code is not repeated in more than one place in the project.
ex:Error logging code is repeated in  ApendDates and ConvertToDateFormat methods.

No comments:

Post a Comment