Glossary · Code health

What is defect density?

Defect density is the number of confirmed defects in a piece of software divided by its size, most often expressed as defects per 1,000 lines of code (KLOC). A module with 12 defects in 48,000 lines has a defect density of 0.25 per KLOC. It compares modules of different sizes, if both are counted the same way.

Last reviewed: November 2026

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 / size

Size 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:

ModuleLinesDefectsKLOCDefect density
Whole service48,00012480.25 per KLOC
billing/8,000780.88 per KLOC
api/40,0005400.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 -20

Divide 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 #N or fixes #N, and none of docs, typo, bump, deps, chore, lint, format or style.
  • 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.

FREQUENTLY ASKED

Defect density: questions

What is a good defect density?

There is no universal target, because the answer depends on what counts as a defect, when you count it and how size is measured. Compare modules inside one codebase, or one codebase against its own past, not against a number from another team.

How do you calculate defect density?

Divide the number of defects by the size of the software in KLOC. For 30 defects in 60,000 lines, that is 30 / 60 = 0.5 defects per KLOC. Use the same window and the same line-counting rules every time.

Is defect density the same as bug density?

Yes. Bug density, defects per KLOC and defect density name the same ratio. Some teams divide by function points or by files instead of lines, which gives a different unit but the same idea.

How do you measure defect density from git?

Count the bug-fix commits that touched each file in a time window, then divide by the file's size. A fix is usually identified from the commit message and the diff. The result is a fix density, which approximates defects found and fixed, not defects that exist.

See it on your own repository.

Index a repository free and read the result for your own code and history. Or explore a live repo first.