Also called Bug density, Defects per KLOC, Fix density
In depth Does code health predict bugs? A validation study: how health scores line up with files that later got bug fixes.
How defect density is calculated
The formula is one division:
defect density = defects / sizeSize is usually thousands of lines of code (KLOC), so the unit is defects per KLOC. Teams that measure size in function points or story points use the same formula with a different denominator.
A worked example. A payments service has 48,000 lines of production code. Over a release cycle, testing and users reported 12 defects that were confirmed and fixed:
| Module | Lines | Defects | KLOC | Defect density |
|---|---|---|---|---|
| Whole service | 48,000 | 12 | 48 | 0.25 per KLOC |
billing/ | 8,000 | 7 | 8 | 0.88 per KLOC |
api/ | 40,000 | 5 | 40 | 0.13 per KLOC |
The service looks fine at 0.25. The split shows that billing/ is about seven times as dense as api/, even though api/ has five times the code. That comparison is what the metric is for. For how lines are counted in the first place, see lines of code.
Why defect density has limits
A size denominator hides as much as it shows. The usual problems:
- Lines are a rough measure of size. The same logic written verbosely or densely changes the KLOC and the ratio without changing the program. Generated code, vendored code and tests inflate it further.
- Small modules swing wildly. One defect in a 200-line file is 5 per KLOC. One defect in a 20,000-line module is 0.05. Neither number says much.
- Defects are counted when they are found. A module nobody tests or uses reports few defects and looks clean. Density measures the defects found, not the defects present.
- Severity is flattened. A typo in a message and a data-loss bug each count as one.
- The window matters. Defects found in the month after a release differ from defects found over three years.
The research on defect prediction handles this by comparing files relative to each other and by pairing size with change data. Nagappan and Ball, "Use of Relative Code Churn Measures to Predict System Defect Density" (ICSE 2005), found that relative code churn predicted defect density in Windows Server 2003 better than absolute churn. See also code hotspots for files that combine change frequency with complexity.
How to measure fix density from git yourself
Most teams do not keep a clean defect database that maps to files. Git history is a usable substitute. The idea is to treat a bug-fix commit as a defect that was found and fixed, then count fixes per file:
# Commits per file whose subject looks like a bug fix, last 180 days
git log --since="180 days ago" --no-merges -i -E \
--grep='^(fix|bugfix|hotfix)|fixes #[0-9]+|closes #[0-9]+' \
--name-only --pretty=format: | sed '/^$/d' | sort | uniq -c | sort -rn | head -20Divide each count by the file's size to get a fix density. This is crude: the subject match catches commits that are not bug fixes and misses fixes with vague messages, and a commit that fixes three files is counted once per file. The research method for finding the commit that introduced each bug is the SZZ algorithm (Sliwerski, Zimmermann and Zeller, "When do changes induce fixes?", MSR 2005): it starts from a bug-fix commit and uses git blame on the lines the fix changed to find the commit that last touched them. Our validation study uses fix history in this spirit.
How repowise measures it
repowise records bug-fix history per file, which answers the question defect density is usually asked for: which files keep breaking. During indexing it reads git history, with no model involved, and stores for each file:
- Fix count over a trailing 180-day window. A commit is a candidate when its subject matches fix keywords such as
fix,bug,patch,closes #Norfixes #N, and none ofdocs,typo,bump,deps,chore,lint,formatorstyle. - The diff decides. A candidate counts only when its diff changes production source lines. Commits that touch only tests, only docs, only config or lockfiles, only comments, or nothing at all do not count, so a docstring rewording is not a bug fix.
- Top symbols, the functions or classes that absorbed the most fixes, as an approximate breakdown.
- A bug magnet flag when the recency-decayed fix mass reaches 3.0. Each fix is weighted by
0.5 ^ (age_days / 90), so recent fixes count most, and the flag always shows the age of the last fix.
You see it in the Bug history section of the health drawer, in repowise risk --target <file>, and as a defect_profile in get_risk over MCP. A file with no counted fix shows nothing, which means no counted fix and not a clean bill of health. See the code health feature for the surrounding scores, and the glossary entries on code health and technical debt for the wider picture.