Calculate software defect density, total defects, or lines of code from any two inputs, with results in defects per line, KLOC, or MLOC.
- All Statistics Calculators
- Defect Rate Calculator
- DPMO Calculator (Defects Per Million Opportunities)
Defect Density Formula
The basic defect density formula is:
DD = D / LOC
- DD = defect density, usually expressed as defects per line, defects per KLOC, or defects per MLOC
- D = number of defects
- LOC = number of lines of code
If you know defect density and lines of code, the number of defects is calculated as:
D = DD * LOC
If you know defects and defect density, the amount of code is calculated as:
LOC = D / DD
The calculator lets you enter any two values and solves for the missing one:
- Find defect density: divides the number of defects by the number of lines of code.
- Find number of defects: multiplies defect density by the amount of code.
- Find lines of code: divides defects by defect density.
Unit selections are converted internally so that lines, KLOC, and MLOC can be used consistently.
Defect Density Unit Conversions
| Unit | Meaning | Equivalent in Lines |
|---|---|---|
| Lines | Individual lines of code | 1 line |
| KLOC | Thousand lines of code | 1,000 lines |
| MLOC | Million lines of code | 1,000,000 lines |
How to Read Defect Density Results
| Result Type | Example | Interpretation |
|---|---|---|
| Defects per line | 0.002 defects/line | There are 0.002 defects for each line, or 2 defects per 1,000 lines. |
| Defects per KLOC | 2 defects/KLOC | There are 2 defects for every 1,000 lines of code. |
| Defects per MLOC | 2,000 defects/MLOC | There are 2,000 defects for every 1,000,000 lines of code. |
Example Problems
Example 1: Calculate defect density
You found 45 defects in 15 KLOC of code.
DD = 45 / 15 = 3
The defect density is 3 defects/KLOC.
Example 2: Calculate number of defects
A project has 80,000 lines of code and a defect density of 1.5 defects/KLOC.
First convert 80,000 lines to KLOC:
KLOC = 80000 / 1000 = 80
Then multiply by the defect density:
D = 1.5 * 80 = 120
The estimated number of defects is 120 defects.
FAQ
What is defect density?
Defect density is the number of defects compared with the size of the codebase. In software metrics, it is commonly shown as defects per KLOC, which means defects per 1,000 lines of code.
Is a lower defect density always better?
A lower defect density usually suggests fewer known defects for the same amount of code. However, the result depends on how defects are counted, how much testing has been done, and whether the codebase includes generated code, comments, or only executable lines.
Should I use lines, KLOC, or MLOC?
Use the unit that best matches the size of the project. Lines work for small code samples, KLOC is common for normal software projects, and MLOC is useful for very large systems. The value is the same relationship expressed at a different scale.
