This Code Is CRAP (2011)
This Code Is CRAP (2011)
Source: Google Testing Blog
Date: Tuesday, February 22, 2011
Author: Alberto Savoia
[!WARNING] Rating: PG-13
This article contains the repeated use of a mild expletive commonly found on elementary school playgrounds. If you are easily offended, pleasecontinue readingstop here.
What exactly is CRAP?
CRAP is an acronym for Change Risk Anti-Patterns. While the name is intentionally provocative, its purpose is to shield developers from the truly offensive experience of maintaining terrible code.
The concept was pioneered in 2007 by the author, Alberto Savoia, and his collaborator, Bob Evans.
The Motivation
When engineers encounter poorly written code, they rarely use formal academic language. You will almost never hear a developer say:
"I find the efferent coupling values here to be excessive," or "The median cyclomatic complexity is simply unacceptable."
Instead, they use a more visceral, subjective summary:
"This code is crap!"
Savoia and Evans realized that by using a memorable (and slightly edgy) term, they could create a metric that resonates with the actual language developers use, ensuring it grabs their attention immediately.
Measuring "CRAPpiness"
While there is no perfect 100% objective way to quantify how "crappy" code is, empirical evidence and professional intuition suggest that certain measurable patterns correlate with poor quality.
Adoption and Reach
Since its launch, the CRAP metric has expanded far beyond its origins:
- Language Ports: Java, .NET, Ruby, PHP.
- Build Tools: Maven, Ant.
- Analysis Software: Integrated into tools like Atlassian’s Clover and Hudson’s Cobertura.
The Current State of Affairs
After creating the Crap4J Eclipse plug-in and the (now largely defunct) crap4j.org site, both authors joined Google. Despite a hiatus, the widespread adoption of the metric proves its value. Savoia intends to revitalize the project over the coming weeks.
The ultimate goals for the reader are:
- Know your CRAP.
- Cut the CRAP.
- Don't take CRAP from anyone!
A Deep Dive into the CRAP1 Metric
Because there are many patterns that indicate bad code, the authors started with a single formula, which we will call CRAP1. This formula specifically targets Java methods.
The Formula
The score is calculated using the following LaTeX expression:
Variable Definitions
| Variable | Meaning | Description |
|---|---|---|
m | Method | The specific piece of code being analyzed. |
comp(m) | Cyclomatic Complexity | A measure of the number of linearly independent paths through the code. |
cov(m) | Code Coverage | The percentage of basis paths covered by automated tests. |
How the Formula Was Derived
The function wasn't random; it was an empirical best-fit curve. The process looked like this:
- Gathered source code from numerous commercial and open-source Java projects.
- Analyzed associated JUnit tests.
- Ranked the code using a draft formula.
- Validated the results with colleagues to see if the "score" matched their subjective "feeling" of crapiness.
- Iterated until the curve matched the subjective data.
The Logic: Why this works
The core assumption is that complex code is hard to test. Therefore, convoluted code usually lacks automated tests.
If a method is complex but still has high coverage, it suggests the developer was diligent and the code is likely more maintainable.
Limitations and Nuances
No metric is a silver bullet. The authors acknowledge several caveats regarding CRAP1:
- Coverage Quality: It is possible to have 100% code coverage with tests that don't actually validate anything useful.
- Necessary Complexity: Sometimes, a single complex method is more readable than splitting it into three overly simplistic ones.
- Missing Dimensions: The current formula ignores higher-level design metrics such as:
- Cohesion
- Coupling
The long-term plan is to incorporate these into future anti-patterns.
How to Use It
Although Crap4J hasn't been actively maintained by its creators recently, the community has stepped up. To implement this in your own workflow, the authors suggest a simple action:
Google search for "CRAP code metric"
Related Assets
Share this post:
![]()