Which code quality metrics should devs track?
Developers and testers strive to create quality, reusable code to deploy apps faster and keeps users happy. But what metrics should they track to evaluate code quality?
Code quality refers to code's long-term usefulness and maintainability. If you can reuse a unit of code in multiple modules and easily maintain the code, it lessens developers' and testers' work for ongoing and future projects.
Code quality metrics help teams quantify the value of code. It's not always straightforward to measure code quality and determine a value, but the right metrics can help teams evaluate test coverage, code complexity and business value. Consider the concerns and needs of the application's intended audience before you choose which metrics to track.
Let's look at important code quality metrics across several categories to understand why they matter and how they serve different users.
Functional quality metrics
Use functional quality metrics to ensure high-quality code throughout the development lifecycle. These metrics include average percentage of faults detected (APFD), defect severity and number and rate of occurrence of production incidents.
Product owners and the project team, especially QA, rely on these code quality metrics. Track the APFD to gauge test case prioritization, as it examines the number of bugs found relative to the number of tests executed. If this percentage is high, that means that you found many bugs with relatively few test cases -- a sign you've optimized the test suite. Defect severity enables testers to prioritize test cases based on the most disruptive bugs.
Coverage metrics
Code coverage metrics help teams evaluate the degree to which each test iteration executes the source code. Coverage metrics include functional coverage, statement coverage, branch coverage and condition coverage.
Developers and QA analysts value these code quality metrics, as they show how extensive tests are. Some organizations require a certain percentage. For example, Salesforce requires that unit tests cover 75% of Apex code, all of which must pass. If your organization practices test-driven development, 100% coverage is not an unreasonable goal.
Complexity metrics
To recognize code complexity and plan how to simplify it, use metrics like cyclomatic complexity, essential complexity, integration complexity and cyclomatic density.
Developers find these metrics valuable when they improve the maintainability of code, as do QA analysts when they assess the scope of test efforts. Use the metric that works best for the project. Cyclomatic complexity tabulates the number of independent paths through the code. Cyclomatic density measures the number of decision points. For both, the goal should be to keep them as low as possible.
Remember to monitor production incidents when code complexity and density are high. You might consider a complex enterprise application with a low incidence of production faults to be high-quality. Not all good code can be simple. However, in more complex code, production bugs are more difficult to fix, and, when you can't easily address such incidents, revenue suffers.