Saturday 4 August 2012

1. Coding standards & practices


In this post, I am going to explain about Coding standards and practices.

Coding standards:
Anyone can write the “working code” but very tricky to write “efficient code”.  Writing “efficient code” will take more time than writing “working code”. But “efficient code” is easily readable and maintainable. We always try to write efficient code.

However, writing “efficient code” is very hard as we usually focus on writing the “working code” due to time availability and other project factors.

Can we all sit with our project team members for an hour to discuss about these coding standards and come with agreed plan to follow up?

Naming Conventions and Standards:

Please use the following 3 naming conventions and standards.

Pascal case:  The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized.
For example: HowToBecomeAnArchitect,
GetAllEmployers

Camel case: The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.
For example: firstName,
lastName

Upper case: All letters in the identifier are capitalized. Use this convention only for identifiers that consist of two or fewer letters.
For example: Architect.UML.Usecase
Architect.Designpatterns.Creational.Singleton

Next question is about WWW (When, Where, Why) the above naming conventions?
  1. Please use Pascal notations for declaring class name, methods name…etc. except declaring variables and parameters.
  2. Please use Camel notations for declaring all parameters
  3. Please use Uppercase for declaring package and namespaces…etc.
Here is the example, shows useability for the above notations.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Architect.Ready.Set.Go
{
class StartUp
{
static void Main(string[] args)
{
Console.WriteLine(GetHowToBecomeAnArchitectBlogURL());
Console.WriteLine(IsAnArchitect("Adam"));
Console.ReadKey();
}
private static string GetHowToBecomeAnArchitectBlogURL()
{
return "http://tobecomeanarchitect.wordpress.com";
}
private static bool IsAnArchitect(string firstName)
{
// Implement the code here
return true;
}
}
}
Other coding standards to follow up
  1. Use meaningful name for variables. For example : address, firstName, lastName, amount
  2. Use prefix ‘I’ for interface. For example, IPostPaid, IBusinessAccount, IPersonalInsurance
  3. Use namespace names for the below standard pattern <Company Name>.<Product Name>.<Top Level Module>.<Bottom Level Module>. For example, if you working for the company Amazon and product name is cloud services then your namespace should be like Amazon.CloudServices.Hosting.ClientServices
  4. A method should contain 1-20 lines only. If its exists more than 20 lines then split into two methods.
  5. Use constant variables instead of declaring hardcoded value. For example: public const decimal INTEREST_RATE = 6.245;
  6. Use enum for declaring group of constants. For example:
enum AccountType
{
Prepaid,
PostPaid,
AsYouGo,
Flexi
}
7. Use comments in the code wherever it’s necessary.
8. #region for specifying the block of code. For example,
#region Calculation definition
public class Calculation
{
// method implementation
}
#endregion
9. Use a positive connotation for Boolean types. For example: IsItRegisteredForInternetBanking, IsItAccountIsActive.
10. Please use meaningful name for class name, file name & these length should be less than 60 characters.

Coding practices:

The some of the coding practices are
  1. Please try to write the code which can be easily maintainable. We can describe many ways for this. Let’s say, you are try to implement method inside your class or file. If you see the same method going to use for other classes or files then you can move into common methods. So that everyone can use it, reduce the time and can update in one place if you need to change the method.
  2. Use exceptions (try-catch) where ever it’s necessary and use closing connection string, file handlers…etc. in the finally block.
  3. Write the code in the numerous methods and each method does the one job at a time. For example, Let’s say, you are writing registration page then you can split your methods like
Private void SaveRegistrationdetails(Customer customer)
{
// implement method here
}
Private void SendActivationEmail(string emailaddress)
{
// implement method here
}
I am not going to write all the details here. For more information please visit the following URLs’

http://msdn.microsoft.com/en-us/library/ms229042(Design Guidelines for Developing Class Libraries on Microsoft site).
http://www.amazedsaint.com/2010/11/top-6-coding-standards-guideline.html

Please try to use the coding standards & good practices for your programming as much as you can.

Conclusions:
  1. Prepared the coding standards documentation for your company and use it for your IT development. Please ask everyone to follow these standards including new starters.
  2. Prepared the deployment documentation & scripts for your IT development & it for deploying the packages on live.
  3. Please use any source version control software (which your company adopt) to maintain the code.  For more information regarding source version, please visit the following URLs’ and

1 comment: