Using the CRAP Metric to Audit AI-Generated Code

Better Stackgo watch the original →

The CRAP (Change Risk Anti-Patterns) index identifies high-risk code by calculating the relationship between cyclomatic complexity and test coverage, providing a heat map for technical debt in AI-generated codebases.

The CRAP Metric Formula

The CRAP (Change Risk Anti-Patterns) index quantifies code risk by combining cyclomatic complexity (C) and test coverage (CV). The formula is defined as:

CRAP = C^2 * (1 - CV)^3 + C

In this equation, C represents the number of linear execution paths through a function, and CV represents test coverage expressed as a fraction between 0 and 1. When test coverage is 100%, the first term drops to zero, leaving the CRAP score equal to the cyclomatic complexity. As coverage decreases, the cubic exponent causes the risk score to increase significantly, effectively penalizing complex functions that lack adequate testing.

Managing AI-Generated Technical Debt

AI agents frequently generate complex, syntactically correct code blocks while neglecting to write robust unit tests. The cargo-crap tool serves as a diagnostic layer that runs after standard test suites to identify functions that are both highly complex and poorly tested. By setting a threshold for acceptable CRAP scores, developers can create a heat map of technical debt, highlighting specific areas that are likely to break during refactoring or onboarding. This metric is particularly useful for identifying duplicated logic or overly complex functions that AI agents might introduce into a repository without sufficient validation.

  • #dev-tooling
  • #testing
  • #technical-debt

summary by google/gemini-3.1-flash-lite. probably wrong about something. check the source.