Tuesday, October 27, 2020

Coding standards and Best Practices

As a developer, we would have used 75% of office time for coding. Any functionality can be coded in multiple ways. Some being the best and other may be complicated way of doing it.

According to me, the best code is something that is clean, with high quality, high performance and less rework in case of future modifications.

Write comments and documentation


This may be one of the important thing every developer should start practicing early in your career. Write proper comments to your code and add documentation header to every method. This will help other developers who works on your code to understand the algorithm and logic without going through each line of code. 

This may look like a waste of time initially but it is a great help for other developers.

Follow coding standards


Very important step in coding. Each and every language we use have defined  their set of standards to use in coding. This will help developers to follow same coding pattern.

For e.g. Using Camel case for any private variable and Pascal case or methods and classes.

I would recommend to use some tools that can help developers to check coding standards. One we become familiar, then it comes automatically. StyleCop or Resharper can be used for this.

DRY vs WET


This is one important software principle i urge everyone to follow. Don't  Repeat Yourself(DRY) principle aimed at reducing the repetition of code. Write Everything Twice(WET)  is just opposite of DRY principle.

We should try to reduce the repetition of code by moving the common piece code into reusable methods and parametrize as required. Another option can be creating a helper or a utility class and place the reusable methods and it can be used across the project.

This helps in avoiding redundant code and easy to maintain in case of future modifications.

Separation of Concern


This principle talks about segregating your code into different components or parts which performs an independent functionality. A common example would be moving all your business logics to a seperate layer or multiple classes.

Exception Handling


Proper exception handling is one of the important key for a quality application. Implement centralized error handling and try catch blocks on each method. 

Please try to log as details as possible to identify the error. Show user friendly error messages to user about the error.

Dispose Objects


When working with unmanaged resources, please make sure to dispose all the unmanaged objects after use. We can either use dispose pattern or use using statements.

Examples of such unmanaged resources are anything that executes in the control of operating system such as network connection, file objects, database connections etc..