There are various ways of calculating defect density
The formula itself is simple: Density = Total Defects Found / Size
if we see defect density at granular level say Code size of a particular functionality X in a application Y along with number of files, then we may draw some good observations like -
Taking an example here:- Lets say we have an application ABC, which have three functionality/modules A, B and C.
Code files for A =10 and KLOC=5k
Code files for B =5 and KLOC=1k
Code files for C =1 and KLOC=25k
Bugs found in A=40, B=50, and C=5
Defect density = Total number of defects/LOC (lines of code)
Defect density = Total number of defects/Size of the project
Size of Project can be Function points, feature points, use cases, KLOC etc
Defect Density can be used to:
1) Predict the remaining defects when compared to the expected defect density,
2) Determine if the amount of testing is sufficient.
3) Establish a database of standard defect densities.