Ten Good Programming Practices

Most programmers can write code for working applications, but getting those applications to work in the most efficient way possible is no easy task. Following a few best practice guidelines greatly reduces the chances of introducing errors into your application, and reduces the cost of maintaining your code.

Prove you're a Dev Guru! Take the test now!

Below are the 10 best practices we keep in mind while coding:

1. Avoid writing long methods and functions. If a method stretches to more than 30 lines, try to beak it down into small methods.

2. Method names should be self-descriptive, so that the name of each method should clearly explain what it does. This avoids having to write unnecessary comments and documentation to explaining your workings. For example:

Bad:

//save the name to database

void ABC(string name)

{

//save the name to database

}

Good:

void SaveName(string name)

{

//save the name to database

}

3. Each method should carry out only one task. Avoid carrying out multiple tasks in a single method. Instead, create multiple small methods that do one task each. For example:

Bad:

//Save Product details and send email to administrator

void SaveDetails(string product, string email)

{

//Task 1:

//Save the product details

//Task 2:

//Send email to administrator

}

Good:

void SaveProduct(string product)

{

//save the product details

}

void SendEmail(string email)

{

//send email to administrator

}

4. Always look for unexpected values.

For example, suppose we have currently two status values, Waiting and Confirmed

Bad:

if(status = = Status.Confirmed)

{

//code for confirmed status

}

else

{

//code for waiting status

//If new status i.e. Pending is introduced then this code will fail and //will be unnoticed.

}

Good:

if(status = = Status.Confirmed)

{

//code for confirmed status

}

else if(status = = Status.Waiting)

{

//code for waiting status

}

else

{

//Unexpected status. Throw Exception

throw new Exception (“Unexpected status value”);

}

5. Use the StringBuilder class for string manipulations, because strings are immutable in .NET, so each time you append to string, a new string object is created, which is an expensive operation.

For e.g.

Bad:

public string CreateMessage(int[] numbers)

{

string msg = String.Empty;

for(int i = 0; i

{

msg = msg + numbers[i].ToString();

}

return msg;

}

Good:

public string CreateMessage(int[] numbers)

{

StringBuilder msg = new StringBuilder();

for(int i = 0; i

{

msg.Append( numbers[i].ToString() );

}

return msg.ToString();

}

6. Use constants instead of using hardcoded numbers in your code, and try to keep the member variables private. Expose them only when necessary, using the public/protected properties.

7. Validate the input data for all the possible values especially Null, and throw the custom exception in case of any unexpected data value.

8. Error messages should be short and user-friendly, and should tell the user what they need to do to resolve the problem. In addition, make sure a log is maintained of every error that occurs.

9. Implement the IDisposable interface in each class. This interface has a disposal method that is used to free up the memory used by the object, when they go out of scope or are no longer in use.

10. If you using ADO, File stream or Socket objects, always close them in the finally block. This ensures that even if there is an exception, these resources will become free in the finally block.