Code Quality Metrics: Is Your Code Any Good?
Code quality can have a major impact on software quality, on the productivity of software teams, and their ability to collaborate. But how can you measure code quality, to determine if your code is worth its salt?
This post covers code quality metrics of two types: qualitative metrics, including maintainability, code readability and level of documentation; and useful automated, quantitative measurements of code quality, such as cyclomatic complexity.
You can use any of these metrics to evaluate your current code quality, and understand if it’s getting any better.
What is Code Quality and How Can You Improve it?
Code quality is an attempt to approximate the value of code as either good code or bad code. Since almost every developer has his/her own definition of what constitutes good code, no single definition of code quality exists.
Rather, a number of factors contribute to code quality in much the same way that several factors affect writing quality. Continuing the analogy, writers often define good writing in their own unique way, but most writers agree on several factors that make good writing—coding is similar to writing and other art forms in this regard.
There are several ways to achieve high code quality, such as:
- Regular code reviews. According to a recent survey, software professionals rank code reviews as the number one way to improve code quality. Code reviews enable developers to collaborate and share knowledge with each other, which improves the code they write. Furthermore, code reviews ensure that code adheres to established coding standards.
- Functional Testing. Functional testing improves code quality because it encourages developers to focus on software functionality from the outset, reducing extraneous code. The aim of software coding is to develop an application that delivers exactly what users need from it.
- Clear requirements. Most software development projects begin with a requirements document, or story cards in agile development. A project with clear, feasible requirements is much more likely to achieve high code quality than ambiguous, poorly specified requirements.
Qualitative Code Quality Metrics
Qualitative code quality metrics are subjective measurements that aim to better define what good code quality means.
Code maintainability is a qualitative measurement of how easy it is to make changes to code, and the risks associated with such changes.
Developers can make judgments about maintainability when they make a change to some code—if the change should take an hour but it ends up taking three days, the code probably isn’t that maintainable.
Another way to gauge code maintainability is to check the number of lines of code in a given software feature or in an entire application. Software with more lines of code is harder to maintain.
Extensibility is the degree to which software is coded to incorporate future growth. The central theme of extensibility is that developers should be able to add new features to code or change existing functionality without it affecting the entire system.
Readability and Code Formatting
Readable code should use indentation and formatting standards particular to the language it’s written in—this makes the code’s structure consistent and visible.
Comments should be used where required, with concise explanations given for each method. Names for methods should be meaningful in the sense that the names indicate what the method does.
Clarity is an indicator of code quality that says good code should be unambiguous. If you look at a piece of code and wonder what on earth the code does, then that code is ambiguous.
Code clarity means any other developer can easily use code written by someone else, without taking a long time to understand how it works.
If code is not documented, it will be difficult to other developers to use it, or even for the same developer to understand the code years from now. One common definition of quality code is “code that is used long term, can be carried across to future releases and products” (without being considered “legacy code”). Documentation is essential to make this happen.
Documentation also provides a way to improve code quality by formalizing the decisions you make when writing a piece of code. When you document code and do it well, you need to think differently about each component and why it exists in the software. Writing out the reasons for certain parts of the code can improve the code’s design.
Code that is well tested is likely to be of higher quality, because much more attention is paid to the inner workings of the code and its impact on users. Testing means that code is constantly under scrutiny.
Efficient code only uses the computing resources it needs to. Also, efficient code runs in as little time as possible.
Many developers believe that inefficient code is not quality code (even if it satisfies some of the criteria above). Some negative impacts of inefficient code is long build times, difficulty to detect and fix bugs, and performance issues for the user.
Quantitative Code Quality Metrics
There are a few quantitative measurements that are indicative of code quality.
Weighted Micro Function Points
This code quality metrics is a modern software sizing algorithm that parses source code and breaks it down into micro functions. The algorithm then produces several code complexity metrics from these micro functions, before interpolating the results into a single score.
WMFP automatically measures the complexity of existing source code. The metrics used to determine the WMFP value include comments, code structure, arithmetic calculations, and flow control path.
Halstead Complexity Measures
The Halstead complexity measures were introduced in 1977. These measures include program vocabulary, program length, volume, difficulty, effort, and the estimated number of bugs in a module. The aim of the measures is to assess the computational complexity of a program. The more complex any code is, the harder it is to maintain and the lower its quality.
Cyclomatic complexity is a code quality metric that measures the structural complexity of a program. It does so by counting the number of linearly independent paths through a program’s source code. Methods with high cyclomatic complexity (greater than 10) are more likely to contain defects.
With cyclomatic complexity, developers get an indicator of how difficult it will be to test, maintain, and troubleshoot code. This metric can be combined with a size metric such as lines of code, to predict how easy the code will be to modify and maintain.
Code Quality Metrics: The Business Impact
From a business perspective, the most important aspects of code quality are those that most significantly impact on software ROI. Software maintenance consumes 40 to 80 percent of the average software development budget. A major challenge in software maintenance is understanding the existing code, and this is where code quality metrics can have a big impact.
Consequently, quality code should always be:
- Easy to understand (readability, formatting, clarity, well-documented)
- Easy to change (maintainability, extensibility)
Beyond Code Quality: Monitoring Software Quality
Code quality has a known impact on higher quality software. It’s not enough to measure code quality—it’s essential to understand and actually measure your software quality, and see if it is improving as a result of higher code quality.
Existing software testing tools do not provide a holistic picture of software quality. Most teams know their unit test coverage, but have much less visibility over the coverage and quality of their integration and acceptance tests.
SeaLights is a continuous testing platform that takes data from all your testing tools and gives you a holistic measurement of test coverage across the entire application. You can easily identify “Quality Holes”, functionality that is being worked on by developers but is not actively tested, which represents business risk. Learn more about the SeaLights platform.