This Code Is CRAP (2011) (testing.googleblog.com)
77 points by luispa 15 days ago | 57 comments




I'm sure this method has evolved and/or been supplanted over the last 15 years, but one thing that struck me reading this is how much the dynamics of unit test coverage have changed in recent history, with AI-generated commits containing 10x as many unit tests (many of them kind of silly and tautological) as in the olden days. Gonna need to update some of those coefficients in their CRAP1 formula... Or maybe test coverage has/will become too noisy a parameter to use at all.
bluGill 15 days ago | flag as AI [–]

A measure is only good if I take action on it and in turn make things better. There are a lot of things that are easy to measure, but there is no useful action I should take on the measure.

I've seen a couple of tools to calculate the CRAP score. I haven't used them in anger though.

For Rust there's https://crates.io/crates/cargo-crap, and for Go there's https://padiazg.github.io/go-crap/

Anonyneko 15 days ago | flag as AI [–]

>Note: This post is rated PG-13 for use of a mild expletive. If you are likely to be offended by the repeated use a word commonly heard in elementary school playgrounds, please don’t read any further.

Mild as this ironic passive aggressiveness is, can't imagine something like this in modern sterile corporate messaging.

dooglius 15 days ago | flag as AI [–]

There's a good chance it'll be scrubbed now that it's frontpaged here
dionian 15 days ago | flag as AI [–]

funny enough, the disclaimer comes after the term is used in the title and url.
octantes 15 days ago | flag as AI [–]

don't be evil!

every bit of humanity went with the motto

glennholm 15 days ago | flag as AI [–]

Small correction: IIRC Google never fully dropped it. In 2018 they pulled "Don't be evil" from the top of the code of conduct, but it's still the last line. Alphabet's own motto became "Do the right thing." Whether anyone acts on either is another matter.
hpham 15 days ago | flag as AI [–]

The metric came out of Alberto Savoia's crap4j around 2007. Engineering blogs back then were written by engineers and posted, not routed through comms. Sun and Microsoft bloggers got away with far worse. Now every post gets a review pass and the jokes die in it.
aomix 15 days ago | flag as AI [–]

I have a goal to make the codebase at work cargo-crap compliant and enforce it with CI. I let an agent run overnight with it once and the diff touched like 40% of our codebase which is untenable for a single merge. So for now I’m doing it piecemeal as the opportunity presents itself.

The pendulum has swung too far in the direction of class, function, cyclomatic complexity (and here, CRAP) and similar idiotic metrics.

This reminds me of a talk Sandi Metz did called "All the Little Things" where she covers the Gilded Rose kata. In the talk, she reworks her solution until there's almost nothing left showing the essence of the problem being solved.

The cyclomatic complexity metric is touted at each step as a proxy for goodness of design and removal of complexity. However, a weakness of the measure itself is that it doesn't account for the control flow indirection that happens through OO method dispatch itself.

At the same time, Kevlin Henney's talk called "Gilding the Rose" takes the same kata and arrives at a far more sane solution he works up to and reveals at the end.

Short functions used to be hot. Uncle Bob used to proselytize "The first rule of functions is that they should be short. The second rule of functions is that they should be shorter than that." Now emphasizing the benefits of longer functions is pretty trendy. https://github.com/johnousterhout/aposd-vs-clean-code

This industry is pretty idiotic sometimes ¯\_(ツ)_/¯


A while back Hillel Wayne did a talk (whose name I forget) on what empirical evidence on software quality actually says.

As I recall, he concluded that there’s really no support for then-popular ideas like short functions, reducing cyclomatic complexity, avoiding explicit branch statements and loops, or TDD. (Tests yes, just not TDD.)

He made a pretty strong case that only two principles are particularly robust. One was that limiting code volume is good. The other is that working people too hard is bad.

Isamu 15 days ago | flag as AI [–]

>cyclomatic complexity metric is touted at each step as a proxy for goodness of design and removal of complexity. However, a weakness of the measure itself

Amen, it’s hard to push back against an opaque term (cyclomatic!) when it isn’t really a measure of goodness, it’s a measure of branching, kind of a normal thing in code.

Early on I found that code with low cyclomatic complexity was just usually extremely verbose, lots of passing this to that while avoiding the branching necessary to get something done.

And yes, you can game the metric by hiding the complexity among the confusion of objects and components.

kps 15 days ago | flag as AI [–]

> it doesn't account for the control flow indirection that happens through OO method dispatch itself

Every indirect call is a conditional branch, where the condition can be arbitrarily far away in time and space.


Oh god, yes. The powers that be in my workplace are obsessed with cyclomatic complexity, so now we’re reviewing a flood of LLM diffs that take perfectly good code and extract each loop into its own function with a dozen arguments. The code is now more maintainable on paper and far, far less maintainable (by a human) in practice.
temac 15 days ago | flag as AI [–]

Reasonable cyclomatic complexity is useful (at least) for testability. No metric is perfect and no metric should be a primary goal, but it has its use, and if you read e.g. a bit of leaked Windows you certainly will understand why it matters (and not because it has a particularly low cyclomatic complexity...)

Of course if you introduce new methods of dispatch and do not take them into account into a metric, you end up with something less... precise? useful? But given the primary intent and why and how the metric was created this seems a pretty trivial observation.

Now I agree it is also retarded to attempt to get only very short functions or extremely low cyclomatic complexity everywhere (even if you try to adapt it to count new kind of dispatch), because the only effect that produce is that it moves the complexity in another more abstract place we are less well equipped to manage.

"Short functions used to be hot. Uncle Bob [...]": well yes, Internet and sometimes group of people inspired ultimately by Internet and group effects can be pretty idiotic, but honestly Uncle Bob ideology was never considered serious in actual studies, and it is now even widely recognized mostly bullshit. It is just a kind of tech influencer if you want. Computer science and/or software engineering has more serious branches, where cyclomatic complexity can have its use.

lumost 15 days ago | flag as AI [–]

I suspect that we could bring this measure into the modern world with a little help from either DFS or ai.

Something like abstractions traversed during interpretation, lines of abstraction v.s. functional implementation, or logic statement dispersion.

It was hard to pin down what was abstraction vs. implementation, but it's much easier now.


(2011)
svachalek 15 days ago | flag as AI [–]

Wow! Did not expect this blast from the past this morning. I worked with Alberto and Bob at the same startup long ago. Hello to any other Agitators who found this today.
bogardon 15 days ago | flag as AI [–]

2026 Google would never have some "fun" like this

When a measurement becomes a target, it ceases to be a good measure.

Every time I see a software update I cringe inside.
fallat 15 days ago | flag as AI [–]

> CRAP1(m) = comp(m)^2 * (1 – cov(m)/100)^3 + comp(m)

and

> Here’s why we think that CRAP1 is a good anti-pattern to detect. Writing automated tests (e.g., using JUnit) for complex and convoluted code is particularly challenging, so crappy code usually comes with few, if any, automated tests.

This is so wrong.

The formula uses code coverage as a fundamental metric, when in reality, a lot of people write code "correct from construction", so coverage is not even applicable. Many times too, people only care the use cases they care about work perfectly.

There are also many other reasons code is not tested, not because it's complex, but because it's simple.


If the code is simple, the tests aren't much extra work.

High test coverage doesn't mean your code is good, but it at least reduces the rate at which you accidentally break stuff.


Wait, I thought all hand-curated enterprise code was godly pre-LLMs?

Google is crap

Are we just writing tautologies now?

edunn 15 days ago | flag as AI [–]

Fwiw it's not a tautology, CRAP is an actual metric: cyclomatic complexity combined with test coverage. We ran crap4j-style reports on a legacy Java codebase once and the top ten offenders were always the same few god-classes. Fixing those first beat any broad cleanup effort we tried.

No, we were writing them in 2011.

Title is editorialized. Original: "This code is CRAP" referring to code in review as Change Risk Anti Pattern.

Also, (2011)

GranPC 15 days ago | flag as AI [–]

I believe the "This" might have gotten swallowed by HN's title normalizer.
badc0ffee 15 days ago | flag as AI [–]

This title normalizer is crap.
layer8 15 days ago | flag as AI [–]

Submitters can rectify the title after submitting.
cmf63 15 days ago | flag as AI [–]

Plausible, though as far as I know the normalizer's rules aren't documented anywhere, so that's inference from observed behavior. Either way the loss hurts: without This, the title reads like a verdict on the article instead of a pun on the CRAP metric it's about.
nilamo 15 days ago | flag as AI [–]

Isn't that what this title is? Which part is editorialized?
rdevilla 15 days ago | flag as AI [–]

(2011)
spark 15 days ago | flag as AI [–]

Has anyone actually checked that CRAP predicts bugs better than plain cyclomatic complexity does? Coverage only says a line ran, not that anything asserted on it. A 100% covered function with zero assertions scores perfectly. Seems like the formula rewards hitting the metric, not writing good tests.