This Code is CRAP

Google introduces CRAP, a metric that identifies risky code by combining complexity and test coverage.

12 cards · 2m · tap to begin

From · · 2m

This Code is CRAP

Google introduces CRAP, a metric that identifies risky code by combining complexity and test coverage.

In brief

Google introduces CRAP, a metric that identifies risky code by combining complexity and test coverage. CRAP measures code risk by combining cyclomatic complexity with test coverage, helping teams spot dangerous code that needs attention. Originally reported by Googleblog.

Meet CRAP, the metric

CRAP stands for Change Risk Analysis and Predictions. It's a formula that combines cyclomatic complexity with test coverage to identify risky code.

Why complexity matters

Cyclomatic complexity measures how many decision paths exist in code. Higher complexity means more ways for bugs to hide and more paths to test.

Coverage alone isn't enough

Code coverage tools often produce false positives, forcing developers to write tests for trivial methods like simple getters that don't need testing.

The CRAP formula

CRAP combines complexity and coverage into one score. Low complexity with good coverage scores low. High complexity with poor coverage scores high, flagging dangerous code.

Spotting dangerous code

CRAP identifies code that's both complex and undertested, the combination that creates real risk. This helps teams prioritize what actually needs attention.

CRAP4J tool released

Google released CRAP4J, a Java tool that calculates CRAP scores for your code. It includes an Eclipse plugin for easy integration into development workflows.

Jenkins integration

CRAP4J integrates with Jenkins and Hudson, allowing teams to track code risk metrics across builds and catch dangerous code early in the pipeline.

Real-world adoption

Teams using CRAP4J for years found it the only tool that reliably identifies dangerous code without forcing unnecessary tests on trivial methods.

“Only Crap4J rocks that way, even it has not been modified since a couple of years.”

Avoiding false positives

Unlike traditional coverage tools, CRAP respects common sense. It won't force you to test a simple getter method that just returns a value.

Java 7 compatibility

Early versions of CRAP4J had issues with Java 7, but the community found workarounds to make it compatible with newer Java versions.

Better than CodePro

CRAP4J outperforms tools like CodePro and JaCoCo by focusing on actual risk rather than raw coverage numbers that lead to busywork testing.

A smarter approach

CRAP proves that the best metric isn't just coverage or just complexity. It's the combination that reveals where real testing effort should go.

At a glance

CRAP measures code risk by combining cyclomatic complexity with test coverage, helping teams spot dangerous code that needs attention.

Read the original on Googleblog

React

Sign in to react and comment.

Comments (0)

Life is short. Keep it sweet. Respect others' opinions and be kind!

    Recommended next

    More decks on google and related topics.