Architecture & DesignTop 10 Best Practices for Code QA

Top 10 Best Practices for Code QA content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.


As a developer, you likely write code. In this article, I will present the best practices to use while writing code that will improve code quality and developer productivity. It also improves the readability, performance, and maintenance of the code.

Best Practices for Better Code Quality

Code Quality Best Practices 1: Basics First

When declaring variable, you should use a standard format.

    • For example, in C# the variables always start with a small letter, as shown below:
string firstName = string.Empty;
    • A property is declared as follows:
public string FirstName {get; set;}
    • You can also declare private variables to start with an underscore ‘_’. For example:
string _firstName;
    • Place constants in capital letters:
const string CONNECTION_STRING = "xxxx";

Similarly, when declaring methods, give a meaningful name and follow the standards for that language. For example, in C# methods are declared as follows:

void GetData()

Whereas in JavaScript, methods are declared as

function getElementById()

These small patterns improve the readability and maintainability of the code. Other points to keep in mind while coding are:

  • If you are using loops, ensure there is an exit condition and the code will not get into an infinite loop.
  • If you are using a ‘switch case’ structure, ensure that a ‘default’ or ‘else’ condition is implemented. This will take care of the unhandled scenarios.
  • Ensure the code is written within a try/catch block. In case of an error, the user gets a friendly message and unhandled errors are logged properly for the team to troubleshoot. Also, ensure the stack trace is logged internally for troubleshooting.
  • Explicitly handle error conditions as shown below:
    if (obj != null)
    if (denominator > 0)
       X = nominator/denominator;

Code Quality Best Practices 2: Modularity

When writing code, methods should not be too long because they become difficult to read and comprehend. Divide the logic of the method into multiple methods if possible. If the methods are re-usable, create a generic class to hold these methods; this will increase reusability. For example:

  • To log errors, you can create a different class and methods can be used in multiple places in the same project and also in different projects.
  • Another example could be keeping ‘constants’ in a different class so that they can be reused by multiple developers working on that project.

Code Quality Best Practices 3: Unit Test Scripts

Modular code also helps in writing better unit test scripts. Unit tests scripts should be written in such a way that each script verifies only one functionality. Use mock objects when writing unit test scripts, because scripts should not be dependent on external factors. For example, when reading values from ‘appsettings’ in the web.config file, use mock objects.

Code Quality Best Practices 4: Specific Languages/Platforms

You should use the best practices specified for the platforms/languages you are using. Not all languages and platforms have the same best practices.

In .NET, a ‘string’ object is immutable. When there are manipulations on the string object, it would be better to use a ‘StringBuilder’ object that provides an ‘append’ method. Another example is, when using File IO operations, ensure the file object connections are explicitly closed; otherwise, they will not be immediately collected by the GC (garbage collector).

Code Quality Best Practices 5: Code Versions

Save your code in a centralized location so that the latest code is accessible to all the developers and all are working on the current version. You can version your code using TFS, SVN (Apache sub version), CVS, or PVCS (Polytron version control system). This will help track changes and the code can be reverted to any of the previous versions if need be.

Code Quality Best Practices 6: Gated Check-ins

You also can use something similar to Gated check-in in TFS. This ensures that the code that gets checked in compiles perfectly; otherwise, it’s not checked in. Common mistakes by the developer, like if a new file is added to the project that is missed out while check-in, and then, when other developers get the latest the code there are compilation errors, can be avoided.

Code Quality Best Practices 7: Deployment Packages

Preferably, a deployment package to be used on multiple environments should not be prepared from a developer’s system. This is because there may be partially written code (un-checked in code), so that the versions of the reference DLLs might be different in developer systems. It would be good to maintain a separate system only used to prepare the deployment packages.

If you are using TFS, create a build server and let TFS prepare the deployment package. You also can integrate the unit test scripts so that they are executed on the latest build. This helps ensure that all the checked-in code compiles perfectly and the unit test scripts verify the new code has not affected the existing functionality.

Code Quality Best Practices 8: JS Code

While writing in JavaScript, keep the code in a separate JS file and include the file as a reference in HTML. In other words, avoid inline JS code wherever possible. When developing, use JS files (as coded); however, for production use the minified JS files because they reduce the size of the files, therefore increasing performance. There are tools available that create the minified version of the JS files.

Code Quality Best Practices 9: Markup Code

Similarly for CSS (Cascading Style Sheets), also use the minified version of the CSS. There are tools such as ‘winless’ that help create the minified version of the CSS.

When coding using HTML and JS, try using JS frameworks such as ‘AngularJS’, ‘Knockout’, and so forth. Frameworks reduce the number of lines of code, inline code (embedding HTML code in JavaScript), and code can be managed better.

Code Quality Best Practices 10: Commenting

Most importantly, ensure there are detailed comments for every method/functionality. Also, add comments within the code to ensure code is explained in detail so that when another developer maintains the code, they don’t have any difficulty understanding the code.


Following best practices helps ease the maintenance of code as well as improve readability and performance. It also helps when the application is in maintenance mode. If there is an enhancement request in the future, it will help the person implementing the request understand the code better, avoid errors, and, most importantly, increase the developer’s productivity even during the development phase.

Editor’s Note

Do you have your own best practice for code quality? If so, post it into the comments on this article!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories