May 28, 2014
In the business of software creation the code is the building blocks of the product. If the code is a mess, hard to read and difficult to maintain, the product is hard to support, costly to adapt and its life span is shorter which in turns increases the total cost of ownership. It also makes it miserable to work on for developers.
What are coding standards?
Coding standards are a formalised set of rules and practices that developers can adhere to when writing code which ensures that the code is easily readable, maintainable and extensible and reduces the risk of introducing bugs.
As you may imagine, there are many coding standards and some of them conflict with each other. In some ways, if a coding standard adds benefit to the code base it doesn’t really matter what it is, as long as the development team are consistent with its application.
Here are some commonly used coding standards that I have seen across the financial industry and in other industry sectors. I list these here as examples of standards. To learn more you should read the further reading books on this page. This is a must for developers.
Consistent case of methods and properties
This is purely a stylistic thing and it doesn’t matter technically which case you use. Java and .Net both recommend different cases. My approach is to the use the C# recommended case for C# and Java for Java.
Tools like Resharper (see below) can auto correct case sensitivity as the developer is coding.
Descriptive method and class names
Part of making code readable is to use good descriptive names for a class and methods. For example the method which says DoSomething() is totally useless as no one can tell what it does without going into the code. A method which says CreateConnectionToDatabase() and passes back a connection object is obvious in its intent.
Keeping methods to a reasonable length
If methods are too long, they become unreadable and difficult to maintain. If a method is too long, you can use the extract method pattern to encapsulate some of the functionality into another method.
See Martin Fowlers book on improving existing code.
Ensuring code standards are adhered to
It can be difficult to remember and annoying to go back and correct standards in the code you are writing. It is therefore essential to use some sort of automated tool both at design time and checking in time that checks for certain elements of quality in the code.
There are a number of tools in the market that can be used for this and I recommend using the following tools:
For the development environment:
- For C# – Resharper from Jetbrains
- For Java – The IntelliJ environment has the same functionality as Resharper
For the automated build environment
NDepend also have a server component which can analyse the code upon check-in and create reports.
All of the tools that I have used generate a huge amount of data when checking for coding standards. They usually check hundreds of metrics out of the box and this is less than useful when trying to take incremental steps to improve code quality. It is therefore important to configure these tools to only check for the metrics you are interested in.