Code quality metrics

Hand-drawn three angular hatched terracotta outlines, partly overlapping, over flowing cream, sand, umber and moss contour lines that converge into a tight knot left of centre.

Code quality metrics are numbers computed from source code or its change history to show where code is hard to maintain and where defects or extra work will appear. They are the main way tools estimate technical debt: design or implementation shortcuts that make later change more costly, with effects limited to internal qualities such as maintainability (Perera et al., 2023). The best-supported finding is that a file’s change history predicts defects better than measures of the code itself (Majumder et al., 2022; Moser et al., 2008). Debt tools disagree with one another (Lefever et al., 2021). Researchers dispute whether vendor ratings of maintainability predict development time once file size is accounted for, and whether code smells make changes take longer (Tornhill & Borg, 2022; Chowdhury et al., 2022; Sjøberg et al., 2013; Palomba et al., 2018).

Static-analysis debt tools

Static-analysis tools read the source code without running it and flag rule violations, design problems and complexity. The tools disagree about where the debt is, and their debt items relate only weakly to faults.

Different debt tools point to different files. Across 10 projects, tools such as Structure101, SonarQube, Designite and DV8 reported very different results, even for basic measures such as size, complexity and cycles between modules (Lefever et al., 2021). After adjusting for file size, the sets of files each tool ranked most problematic barely overlapped. A comparison of six static-analysis tools on 47 Java projects likewise found little to no agreement among them, and low precision (a large share of flagged items that were not real problems) (Lenarduzzi et al., 2021).

SonarQube flags items that relate little to faults. In 21 open-source projects, violations SonarQube labels as bugs were generally not fault-prone (Lenarduzzi et al., 2019a). In 33 Apache Java projects, classes with SonarQube debt items changed slightly more often than clean classes but had no more faults. The tool’s severity and type labels were often inconsistent (Lenarduzzi et al., 2019b).

SonarQube expresses debt as the time needed to fix its rule violations, and that estimate is inaccurate. When 81 junior developers fixed debt items in 21 projects, the tool’s remediation time was off and in most cases too high (Baldassarre et al., 2020).

Tools and developers agree mainly on defect debt and miss debt outside the code. In one project, developers’ own lists of debt items matched automated detection best for defect debt (Spínola et al., 2019). Many items developers named sat in documentation, tests and other artifacts that the tools did not analyse, so the tools found no documentation, testing or usability debt. The study had no ground truth for real debt, so it could not say whether the developers or the tools were more correct (Spínola et al., 2019).

Maintainability ratings

Maintainability ratings combine several code measures into one grade. They correlate with how long issues take to resolve, but the correlations come from past data, and none has been shown to hold once file size is taken into account.

SIG star ratings and issue resolution time

The rating by the Software Improvement Group (SIG) correlated with faster issue resolution in an earlier study, but the correlation roughly halved in a replication. The model grades systems against a benchmark of other systems. It gives one star to 5% of them, two, three and four stars to 30% each, and five stars to 5% (Wijnmaalen et al., 2019). The mapping from code properties to the overall rating rests on expert opinion.

An earlier study found that better-rated systems resolved issues faster (Wijnmaalen et al., 2019). Re-measured with the 2018 version of the model, the correlation between maintainability and defect resolution time fell from 0.64 to 0.33. A correlation runs from -1 to 1, with 0 meaning no relation, so the link went from strong to moderate. One new modularity property correlated with resolution time in the wrong direction (-0.30) (Wijnmaalen et al., 2019).

Code Health and the Code Red study Weak evidence: few or small studies support this

One study reports large effects of code quality on defects and development time, using a vendor’s metric on code from the vendor’s users. The Code Red study rated 30,737 files in 39 proprietary codebases with CodeScene’s Code Health metric. In it, low-quality code contained 15 times more defects than high-quality code. Resolving issues in that code took on average 124% more development time, with maximum cycle times up to 9 times longer. Code in the middle “Warning” category needed 78% longer to implement an issue than “Healthy” code (Tornhill & Borg, 2022).

The authors themselves state that the study does not show cause. Low quality might lead to defects, or many defects might lead to low-quality code, and differences in process and organisation could confound the results (Tornhill & Borg, 2022). They tried to limit confounding by including a wide variety of codebases from diverse organisations. All the codebases came from CodeScene’s users and were mainly in C++, a sample they note may not be representative (Tornhill & Borg, 2022). They suggest randomised trials or causal analysis as future work (Tornhill & Borg, 2022).

The authors checked size against Code Health. Lines of code added correlated weakly with issue resolution time (r = 0.13), while Code Health correlated more strongly (r = -0.58: healthier code, shorter resolution time) (Tornhill & Borg, 2022). A later reanalysis by authors including the original two added a second dataset to the public Code Red data. It found the associations vary across the quality range, with strong non-linear effects at the extremes (Borg et al., 2024).

A separate industrial study measured debt differently and found mixed results. In six components of one company’s system, SonarQube debt, adjusted for size, was set against Jira lead time. It had a moderate positive effect in two components, no meaningful effect in two, and a moderate negative effect in two (Paudel et al., 2024). Debt explained between 5% and 41% of the variation in lead time, and the authors point to change size, complexity or a delayed effect as other possible causes (Paudel et al., 2024).

File size as a confounder Contested: studies disagree on this

Researchers disagree on whether code metrics measure anything beyond file size. A metric can appear to predict trouble only because it tracks size. Studies of whether metrics predict maintenance effort have found both good predictors and negative results, and critics argue that, apart from program size, no single metric is reliable (Chowdhury et al., 2022). Earlier studies differed in whether they controlled for size at all.

At the level of single methods, dividing by size did not remove its influence. In about 730,000 Java methods from 47 open-source projects, once size was removed there was no evidence that metrics other than size were valid (Chowdhury et al., 2022). The same study still concludes that metrics help estimate change-proneness after removing size, but that a metric’s effect varies with the context and the maintenance measure used (Chowdhury et al., 2022).

For defects, size confounding holds for some metrics and not others. Earlier studies found that class size confounded the link between most object-oriented metrics and defects (Tahir et al., 2021). A 2021 reassessment found the effect significant for only some metrics; size consistently mediated only the coupling-between-objects metric, a count of the other classes a class depends on (Tahir et al., 2021).

Change history versus code measures Strong evidence: many studies agree on this

Measures of how a file has changed over time, called process metrics, predict defects better than product metrics computed from the code itself. One process metric is churn, the amount of code added, deleted or modified.

History-based models found far more of the defective code. Across 722,471 commits from 700 GitHub projects, the best history-based learners reached a median recall of 98%, against 44% for the best code-based ones (Majumder et al., 2022). Recall is the share of actually defective code that a model flags. Their median AUC was 95% against 54%. AUC measures how well a model ranks defective code above clean code, where 50% is chance and 100% is perfect. The same result held on Eclipse data and in an industrial Java system (Moser et al., 2008; Arisholm et al., 2010).

Relative churn predicts defects better than raw churn. In Windows Server 2003, absolute churn predicted defect density poorly, while relative churn measures, which express churn as a ratio rather than a raw count, predicted it well (Nagappan & Ball, 2005). The relative measures separated fault-prone from other binaries with 89.0% accuracy. Similarly, files that change together predicted change- and error-proneness better than code measures other than size and complexity (Lefever et al., 2021).

History-based prediction has practical limits. History data are among the most expensive measures to collect, and which model counts as best depends heavily on the criterion used to judge it (Arisholm et al., 2010). Prediction performance also varies with the time period evaluated, so a substantially different next release can change it drastically (Bangash et al., 2020). Which metrics matter most can change dramatically between small studies and large-scale data (Majumder et al., 2022).

Code smells as signals

Code smells are recurring patterns in code, such as an overlong method or a class that does too much, that suggest a design problem. Smells add information to defect prediction, but whether they make changes take longer is disputed.

Smell signals improve defect models built on history and code metrics. Classes that depend on anti-patterns, a broader family of poor design patterns, were more fault-prone than others in almost all releases of three open-source systems (Jaafar et al., 2013). Adding smell intensity to models built on product and process metrics raised their accuracy. The combined model’s F-measure, a score that balances missed defects against false alarms, was at least 13% higher than the previous best models (Palomba et al., 2019). See also code duplication, a smell with its own literature.

Do smells raise change effort? Contested: studies disagree on this

Studies disagree on whether smells make code more costly to change. Across 395 releases of 30 open-source projects, with 17,350 manually validated smells, smelly classes changed more often and had more faults than smell-free classes (Palomba et al., 2018).

In a controlled study that measured effort directly, no smell raised it once file size and number of changes were adjusted for. Six developers maintained four Java systems, and none of 12 code smells was significantly associated with more effort; size and number of changes explained almost all the variation (Sjøberg et al., 2013). The authors conclude that reducing code size and limiting changes may help more than refactoring smells. A re-analysis of the same data split effort by activity and found smells mattered more than file size for editing and navigating, while size mattered more for reading and searching (Soh et al., 2016). The size of the change mattered more than either for every activity.

Self-admitted debt comments Weak evidence: few or small studies support this

Self-admitted technical debt is debt that developers flag themselves in code comments; in four large open-source projects it appeared in 2.4% to 31% of files (Potdar & Shihab, 2014). Whether such comments mark code with more defects depends on the level studied. Earlier studies at the level of files and classes found no clear evidence of harm (Chowdhury et al., 2024). One reported mixed results: in some projects files with such comments had more bug-fixing changes, in others files without them had more defects (Chowdhury et al., 2024).

At the level of single methods, the link appears. In a preprint covering 774,051 methods from 49 open-source projects, methods with self-admitted debt were larger, more complex, less readable, and more prone to bugs and changes (Chowdhury et al., 2024).

Further reading

  • Lefever et al., “On the lack of consensus among technical debt detection tools” (Lefever et al., 2021): compares several debt tools on the same projects and shows how little their findings overlap.
  • Majumder, Mody and Menzies, “Revisiting process versus product metrics” (Majumder et al., 2022): the 700-project comparison of history-based and code-based defect prediction.
  • Tornhill and Borg, “Code Red” (Tornhill & Borg, 2022): the Code Health study behind the 15-times-more-defects figure, with its own discussion of causality and sampling.
  • Sjøberg et al., “Quantifying the effect of code smells on maintenance effort” (Sjøberg et al., 2013): the controlled study that found size and number of changes, not smells, drove effort.
  • Chowdhury et al., “Revisiting the debate: are code metrics useful for measuring maintenance effort?” (Chowdhury et al., 2022): a large method-level study of size as a confounder.

Known gaps

What this article cannot tell you yet.

  1. No maintainability rating has been tested against issue resolution time or change effort with controls for file size and change frequency, and none has been tested prospectively.
  2. Studies relating churn or files that change together to change effort, rather than to defects, were not found; the history-versus-code comparison covers defects only.
  3. SQALE and SonarQube debt ratios were not found validated against development effort over time.
  4. No independent replication of the Code Red findings was found; its only follow-up is a reanalysis by the same authors.
  5. Hotspot analysis (combining churn with complexity) as a way to locate debt was not evaluated separately.

References

Solid source
Rated highly trustworthy by the research, and every passage taken from it was found in it.
Weak source
Rated low, or a passage taken from it was not found in it.
Medium trust
Rated medium by the research.
Not checked
Rated high, but its passages could not all be checked against it.
Show all 25 references