How to Measure the Quality of your Code
Introduction
Measuring the quality of your code can be a daunting task for a beginner programmer; Measuring quality of code can be subjective and hard for a team to agree on. It can be difficult to produce qualitative metrics for your code, and much harder to be disciplined and enforce those metrics. Due to differences in opinions, and the myriad of advice you receive as a beginner, I wanted to write an article around software quality characteristics.
Software quality characteristics sum up all the different areas of thinking you need to go through in order to evaluate the quality of your software. Within this article, I will describe six different qualitative characteristics that govern the overall quality of your codebase, and what to think about when trying to improve each one. For more information on measuring code quality, see this overview by SeaLights.
Maintainability
Maintainability of a codebase is all to do with the ease of which it is to program in. Whether it is how easy it is to read, or how easy it is to locate a bug and fix it. It is also to do with the complexity of your system, how fragile, or rigid it might be. A fragile codebase is one that is very easy to break; A rigid codebase is one where it is very hard to change.
In order to measure the maintainability of your codebase, a good metric is the cyclomatic complexity of methods. The cyclomatic complexity of a method is the number of paths you can take through that method in order to achieve a result.
Reliability
Reliability is the notion of your system remaining available under extreme circumstances. For example, a website that can deal with an order of magnitude of traffic over a certain period of time. A system that is fault tolerant, and easily manages to cope with such numbers is known as a reliable system.
Measuring the reliability of your system comes down to performance testing. There are performance testing frameworks that can help you test your software under heavy load, and is able to measure the general availability when under such stress.
Functionality
Functionality of a codebase is reason the code exists in the first place. If your codebase did not provide any functionality, then there is little point in it. The existence of functionality is the direct objective of a codebase. The quality of functionality depends on your specific goals. However, the main way to understand the quality of your functionality is within the flexibility, and completeness of it.
When designing your software, you will be providing a core set of features. For example, given an eCommerce system, you would expect there to be some representation of an invoice. The minimum viable product would be to have a list of items, and the totals of those. However, there may be a lot more pieces of functionality that you could offer to help a developer with manipulating an invoice in code.
Usability
Usability of a system is how the apis to your software, or the objects contained within your code-base, have been designed to give a sense of ease and intuition. This characteristic of a code-base is always hard to measure. The easiest way to measure such a thing is by using your code-base yourself, and understanding the counter-intuitive areas, and whether your apis make logical sense, and give enough functionality to be useful.
Efficiency
Efficiency is simple. How long or how much computing power is needed to perform a specific action. Efficient software is all about algorithmic complexity, and the trade-offs between space and computation. It is relatively straightforward to measure efficiency, as you can apply a similar method as you would for reliability. The difference is to measure the change in space/time of your system before and after the changes have been made. This can then give you evidence that your system has become more efficient.
Portability
Portability is all about how easy it is to move, copy, or deploy your software. It is all about setting up an ecosystem that allows you to go from development to production with very little ease.
Technologies such as docker and orchestration frameworks like kubernetes, can help you set up a portable application that can be easily scaled.
Conclusion
This article has given you six ways in which to measure the quality of your code. If you measure the quality of your code and overall solution with these six characteristics in mind, then you will have a system that can be maintained and utilised for many years with zero downtime.
Code quality is mostly measured in passed unit / integration tests, happy linters and satisfied definitions of done.