Introduction
When building software, it's hard to know how many problems or bugs are hiding in the code. We need ways to measure how many defects exist and how well we find them during testing to improve quality and fix issues faster.
Imagine cleaning a messy room. Defect density is like counting how many trash items are in the room compared to its size. Detection rate is how quickly you find and pick up the trash while cleaning.
┌─────────────────────────────┐ │ Software Project │ ├─────────────┬───────────────┤ │ Defect Density │ Detection Rate │ │ (Defects per │ (Defects found │ │ thousand │ per hour) │ │ lines code) │ │ └─────────────┴───────────────┘
def calculate_defect_density(defects_found, lines_of_code): return defects_found / (lines_of_code / 1000) def calculate_detection_rate(defects_found, testing_hours): return defects_found / testing_hours # Example values defects = 50 loc = 10000 hours = 25 density = calculate_defect_density(defects, loc) rating = calculate_detection_rate(defects, hours) print(f"Defect Density: {density:.2f} defects per KLOC") print(f"Detection Rate: {rating:.2f} defects per hour")