Metrics of Moment 9
There are two metrics that I think are quite useful in the pursuit of clean code. One is Crap, and the other is The Braithwaite Correlation. The first is a pragmatic call to action, the second is a measure of overall care.
CRAP
I’ve written about Crap before. It’s a very clever metric that combines test coverage and cyclomatic complexity. You want to keep this metric low. (FitNesse is currently at 0.07). Crap will skyrocket if you have a method with high CC and no coverage. You can bring it down if you 1) split the method up into a group of low CC methods or 2) cover the method with tests or 3) both.
One of the more useful elements of CRAP is that it measures the “crappiness” of individual methods, and provides you with a list of the worst. (Fitnesse has very few crappy methods at the moment.) This list of crappy methods gives you targets to attack. You see a method whose CRAP metric is 30, or 50, you target it for decomposition and coverage. In this way you can engage in a program to bring the “Crappyness Trend” (i.e. the ratio of crappy methods to total methods.) down.
What is the benefit of brining crappiness down? Apparently there is a strong correlation between CC and defects. So an effort to either reduce CC or increase coverage in high CC modules seems an appropriate way of reducing defects. Of course the real reason to target crappy modules is that CRAP is a measure of care—or rather it’s lack.
If you have high CRAP numbers it simply means that you have been careless with your code. Big CRAP means that your methods are long, complicated, and untested. And that’s just slovenly behavior. Sorry, that’s just the way it is.
The Braithwaite Correlation
Despite the fact that this metric sounds like a movie title, this metric excites me. The creator, Keith Braithwaite is duly humble about it; but I think it’s a significant discovery. The math behind the metric may be a bit hi-falutin for some folks, but the logic is very compelling. If you don’t know what the terms “scale-free”, “power-law”, and/or “log-log” mean, don’t worry. It wouldn’t hurt you to learn these concepts, but the bottom line is very simple.
The Braithwaite Correlation is simply a measure of how many methods have high Cyclomatic-complexity. Well, OK, that’s not precisely true. It’s actually a measure of how CC is distributed amongst all the methods. Suffice it to say that if this number is small, then many methods have a high CC. (notice the inversion.)
Now here’s the interesting bit. BC is not dependent upon the size of the program being measured. Two different programs that have the same BC, have the same distribution of CC amongst their methods. One program may have a thousand methods, and the other may have only 20, but the distribution is the same. Therefore BC can be viewed in an absolute sense. For example, we can say that we want our programs to have a BC > 2.
Actually the number 2 seems to have a special significance. Keith’s research so far shows that programs developed with TDD almost always have a BC greater than 2. Whereas programs developed without TDD almost always have a BC less than 2. (FitNesse currently has a BC ~= 2.88)
What is the value of measuring BC in your code? If BC is high it means that your methods are generally small and simple, and that very few are large and complex. If BC is low, it means that more of your methods are larger and more complicated. And since we know that CC is strongly correlated with defects, we can surmise that high BC correlates with low defects.
But again, the real reason to keep BC high is that it simply reflects a greater amount of care.
Conclusion
The conclusion is simple. These metrics are easy to measure1 and so you should be measuring them now. What’s more, these metrics (especially CRAP) tell you where to focus your energies. You can take action that will have an immediate and direct effect on the quality of your code that correlates to defect rate.
So what are you waiting for?
1 For java programmers the tools already exist to measure these metrics, and they are very simple to use. For everyone else, the tools that calculate CC and coverage are readily available. Combining them to calculate these two metrics just isn’t that hard.

I guess my google skills are weak. I can’t find the java tools for the Braithwaite Correlation. Do you have any links?
Blimey.
Thanks for your enthusiasm, Bob. Let me inject a little humility right here and point out that the metric which I have been investigating in my spare time the last few years remains very much a work in progress.
There are all sorts of open questions about it, and I’d strongly advise that no-one worries terribly much about the exact value of the metric on their code. That said, I do think it can be a useful guide (only) to the global state of your codebase.
Keith
Can you suggest a tool for C++?
The Braithwaite Correlation can be measured with the download provided on Keith’s blog: http://www.keithbraithwaite.demon.co.uk/professional/software/index.html
I’m glad that I coult contribute to lowering the CRAP value for FitNesse a bit. What I missed was the full name: Change-Risk Anti-Patterns. CRAP identifies problematic methods in your code base. Currently I feel confident at work in a combination of FindBugs, PMD, CPD, JDepend, Cobertura and Crap4J – though those builds are nothing to be dealt with all the time, since compilation takes a lot longer. The static code reviewing tools taught me a big deal about the usage of the language as well. On the other hand I’m aware of using too much and think on cutting these down a bit more.
As Patrick Smacchia (lead developer on NDepend) pointed out in Hanselminute’s podcast on NDepend (http://www.hanselman.com/blog/HanselminutesPodcast51StaticCodeAnalysisWithNDepend.aspx), the aggregate metrics tend to be less intuitive than organic metrics and their absolute values are usually meaningless so they’re only useful for comparison.
Certainly the Crap effect can be achieved by searching for methods with (CC > X and CoverageRatio < Y)—much simpler and more useful for the practical purpose of finding methods to refactor.
Now, if what you’re looking for is a single number to show-off on the blog or put on a marketing brochure, the Crap metric may be more useful.
This Braithwaite Correlation thing sounds a bit different, but still smells like megapixels if you know what I mean; let’s wait and see if it flies.
As Patrick Smacchia (lead developer on NDepend) pointed out in Hanselminute’s podcast on NDepend (http://www.hanselman.com/blog/HanselminutesPodcast51StaticCodeAnalysisWithNDepend.aspx), the aggregate metrics tend to be less intuitive than organic metrics and their absolute values are usually meaningless so they’re only useful for comparison.
Certainly the Crap effect can be achieved by searching for methods with (CC > X and CoverageRatio < Y)—much simpler and more useful for the practical purpose of finding methods to refactor.
Now, if what you’re looking for is a single number to show-off on the blog or put on a marketing brochure, the Crap metric may be more useful.
This Braithwaite Correlation thing sounds a bit different, but still smells like megapixels if you know what I mean; let’s wait and see if it flies.
Related to my question about a C++ tool, a beta of CppDepend is now available: http://www.cppdepend.com/
I recently blogged about taking CRAP and adding in afferent coupling to really home in on where to focus your energies.
I hope you might find this effort interesting:
http://imistaken.blogspot.com/2009/10/crap4net.html
In short I’ve trying to create a utility for measuring CRAP in the .Net world.