Metrics of Moment 75

Posted by Uncle Bob Mon, 08 Jun 2009 16:26:51 GMT

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.

Comments

Leave a response

  1. Avatar
    tieTYT about 2 hours later:

    I guess my google skills are weak. I can’t find the java tools for the Braithwaite Correlation. Do you have any links?

  2. Avatar
    Keith Braithwaite about 2 hours later:

    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

  3. Avatar
    Ben about 3 hours later:

    Can you suggest a tool for C++?

  4. Avatar
    Markus Gärtner about 3 hours later:

    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.

  5. Avatar
    zvolkov 1 day later:

    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.

  6. Avatar
    zvolkov 1 day later:

    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.

  7. Avatar
    Ben 8 days later:

    Related to my question about a C++ tool, a beta of CppDepend is now available: http://www.cppdepend.com/

  8. Avatar
    Clint Shank 14 days later:

    I recently blogged about taking CRAP and adding in afferent coupling to really home in on where to focus your energies.

  9. Avatar
    Lior Friedman 4 months later:

    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.

  10. Avatar
    psn card 9 months later:

    Thanks for sharing this great article! That is very interesting Smile I love reading and I am always searching for informative information like this.

  11. Avatar
    hanlyone 10 months later:

    In short I’ve trying to create a utility for measuring CRAP in the .Net world.

  12. Avatar
    FLV extractor 10 months later:

    it is a goood idea

  13. Avatar
    flv converter about 1 year later:

    nice idea.

  14. Avatar
    bags factory about 1 year later:

    want to keep this metric low. (FitNesse is currently at 0.07). Crap will s

  15. Avatar
    wholeslae shoes about 1 year later:

    think you ! http://www.yourvoguemall.com

  16. Avatar
    moncler about 1 year later:

    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.

  17. Avatar
    canada goose about 1 year later:

    we offer you canada goose to enjoy the winter!! Canada goose jakkesnug Goose jakkeYou’re Worth It Canada goose jakkerremain stylish Canada goose parkais a trustworthy brand Canada goose tilbudis the best one

    canada goosewarm canada goose parka protection canada goose expeditionfashionable canada goose jakkerlet you have Happy winter canada goose jakkeGood quality

    canada goose jacka vogue goose jacka pretty canada goose jackordifferent designs outlet canada goose amazing canada goose Expedition smooth welcome to buy!thank you!

  18. Avatar
    immortal vampires about 1 year later:

    I wish I could find such informative sites more often. I regularly spend much time on just looking for some worthy sites when I can find something to read. Thanks for sharing.

  19. Avatar
    video to ipad converter about 1 year later:

    best Video Converter for iPad for Converting Video for iPad with high quality. Guide on How to Convert Videos to iPad and Convert Movies to iPad.

  20. Avatar
    Pandora about 1 year later:

    reason to target crappy modules is that CRAP is a measure of care—or rather it’s lack

  21. Avatar
    moncler about 1 year later:

    Very few has the knack of writing such beautiful post like you do.. !!moncler mens I have been following this blog and i feel different energy while reading your post. Keep it dear buddy.. !!moncler women

  22. Avatar
    Rolex about 1 year later:

    Hey I think its just an awesome idea..Please do posting this like in future..

  23. Avatar
    SEO Lancaster about 1 year later:

    Helpful information ..I am very happy to read this short article..many thanks for giving us this useful information. Fantastic walk-through. I appreciate this post.

  24. Avatar
    best electric fence about 1 year later:

    You did make some intriguing points over there. Thanks.

  25. Avatar
    kimono bjj about 1 year later:

    Great work. I wanna appreciate the creator of this site. By the way nice comments too.

  26. Avatar
    vampire history about 1 year later:

    I was very happy that I found this website. I needed to thank you for this excellent information!! I certainly enjoyed every bit of it and I have bookmarked your website to check out the new stuff you post in the future.

  27. Avatar
    lv backpack about 1 year later:

    lv backpack have fairly very rated for rather a few of elements just like pattern durability and ease of use

  28. Avatar
    clothing manufacturer about 1 year later:

    many thanks for giving us this useful information. Fantastic walk-through.

  29. Avatar
    Karen about 1 year later:

    I spent hours playing this gambling game. It is a job well done.

  30. Avatar
    traprenovatie about 1 year later:

    reasons to sppof modules is that thngs get a measure of aprecience

  31. Avatar
    okey oyunu oyna about 1 year later:

    nice article.

    internette görüntülü olarak okey oyunu oyna, gerçek kisilerle tanis, turnuva heyecanini yasa.

  32. Avatar
    insomnia cures about 1 year later:

    Don’t you find it funny that in programming, they always use funny names to name themselves? CRAP of all names!

  33. Avatar
    cheap brand watches about 1 year later:

    Good writing, this article bring me a lot. Your blog is great, thanks for sharing.

  34. Avatar
    mbt shoes sales about 1 year later:

    I’m definitely both!!

  35. Avatar
    Futons for Cheap about 1 year later:

    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.

  36. Avatar
    replicas-bags-store about 1 year later:

    you best choose fashion Louis Vuitton Damier Azur Stresa MM onsale.The Best Site to buy CHEAP Louis Vuitton Damier Azur Stresa PM .Also has Louis Vuitton Damier Azur Canvas Neverfull MM, Louis Vuitton Pochette Milla MM outlet that can provide to you.

  37. Avatar
    christian louboutin shoes on sale about 1 year later:

    These christian louboutin patent leather pumps are just the same as selling in the franchise store.Or even better. Quite cheap christian louboutin leather platform pumps.I could say this is the best one I have ever ordered online.

  38. Avatar
    beats by dr dre headphones about 1 year later:

    I found that his foot odors never bring us to death.I never regret buying these beats by dr dre studio for him. These beats by dr dre solo are just the same as selling in the franchise store.Or even better.

  39. Avatar
    Hancy about 1 year later:

    Hello Friend,Whichever style of Fashion Shoes you’re looking for, classical, fashionable, lovely or the latest design, you can find your favorite designer shoes in www.dunkpage.com ,several days ago I bought one pair of shoes from there,It’s beautiful and very comfortable!

  40. Avatar
    Do you have a spam problem on this site; I also am a blogger, and I was wanting to know your situation; many of us have created some nice practices and we are looking to swap solutions with others, be sure to shoot me an email if interested. over 2 years later:

    Do you have a spam problem on this site; I also am a blogger, and I was wanting to know your situation; many of us have created some nice practices and we are looking to swap solutions with others, be sure to shoot me an email if interested.

  41. Avatar
    http://www.coogi-store.com/# over 2 years later:

    I am extremely delight to be on this website reading this great blog. The author has done a fantastic job. Thanks for sharing.

  42. Avatar
    rip dvd to ipad/ipad 2 over 2 years later:

    Rip DVD to iPad free with DVD on iPad Converter which can easily rip/convert DVD movies for iPad supported formats like MPEG-4, H.264/MPEG-4 AVC, MOV, and M4V, also supports extract most audio formats, such as AAC, MP3, WAV, AIFF, M4A, from DVD file.

  43. Avatar
    Belstaff Jackets over 2 years later:

    Recently Star Derek Ross wears Belstaff Jackets in the heat of Belstaff Outlet basketball game war. However, his new war boots Belstaff Factory will still be the his Belstaff Leather war boots in the rest of the game this season. Particularly the exclusive equipment including Belstaff Jackets, Belstaff Jackets Men, Belstaff Jackets Women, Belstaff Jackets Kid shoes is notable. In st. Patrick’s day, Ross will wear special matching Belstaff Bags to join the game.

  44. Avatar
    beats by dre store over 2 years later:

    many of us have created some nice practices and we are looking to swap solutions with others, be sure to shoot me an email if interested.cheap beats by dre beats by dre store

  45. Avatar
    Dez Bryant Jerseys over 2 years later:

    Cowboys defensive coordinator Rob Ryan did not deal with reporters Monday, when he spoke of his brother’s foot around the issue of love. In December, foot fans video allegedly Rex Ryan’s wife Michelle, appeared on the Internet controversy. But what this means to Cowboys? Nothing. I still support Marion Barber Jerseys, Dez Bryant Jerseys and Tony Romo Jerseys. I look forward to their playing in the NFL season instead of the unrelated news.

  46. Avatar
    Drew Brees Jerseys over 2 years later:

    10 years, NFL has started and heavyweight showdown on Thursday night season. Officially named for the NFL kickoff game on Thursday night’s game in the ESPN, ABC and now NBC broadcast. This game has always been the two best teams in the NFL. My favorite team, the Saints Jersey, has been scheduled for Thursday night’s game at times. NFL kickoff game on Thursday in the appearance of NFL teams. I like Drew Brees Jerseys, Pierre Thomas Jersey in the team.

  47. Avatar
    Mark Ingram Jersey over 2 years later:

    As the most wins since 2006 NFC team, it should be New Orleans Saints, NFL kickoff game on Thursday appeared three times no surprise. Saints is one of the best teams in the NFL, always draw ratings. After their loss in 2007, the Super Bowl champion in 2010 rally the Saints beat Brett Favre and the Minnesota Vikings 14-9. In 2011, the Saints will travel north, facing the Green Bay Packers NFL kickoff game on Thursday. I hope Mark Ingram Jersey, Darren Sharper Jersey and Jeremy Shockey Jersey will give us a good play performance.

  48. Avatar
    Miles Austin Jerseys over 2 years later:

    Experienced left means stick with David Buehler and former Oklahoma outstanding Dan Bailey in Cowboys. Buehler kick-off will almost certainly deal with Bailey handling field goals. But I still like Terrell Owens Jerseys and Miles Austin Jerseys, I think they still the good players in Cowboys.

  49. Avatar
    http://www.watchtang.co over 2 years later:

    So, should you haven’t enough money at the pocket and wish to fulfil your own desire in an economical cost, fake Rolex watch provides you this chance to seize it inexpensively affordable line

  50. Avatar
    beats studio by dr dre over 2 years later:

    i think you will love here

  51. Avatar
    Guess Horloges over 2 years later:

    reasons to sppof modules is that things get a measure of apprecience. Crap indeed is very clever metric.

  52. Avatar
    mts Convertidor over 2 years later:

    afternoon. MTS Convertidor Just after 4 p.m. a plane wrote the words “Last Chance” in the air. The message was preceded by”Lost Our Lease” and followed by “Now Open.”MTS Mac

  53. Avatar
    Tips For Bowling over 2 years later:

    With my luck, if I ever invested in General Motors, they’d bust it to Corporal!

  54. Avatar
    modular kitchen in India over 2 years later:

    It’s really great stuff and I look forward to reading your post. What you have to say is important and should be read.

  55. Avatar
    cheap gucci over 2 years later:

    Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog.Any way Ill be subscribing to your feed and I hope you post again soon.

  56. Avatar
    christian louboutin over 2 years later:

    The professional design make you foot more comfortable. Even more tantalizing,this pattern make your legs look as long as you can,it will make you looked more attractive.Moveover,it has reasonable price.If you are a popular woman,do not miss it.

    Technical details of Christian Louboutin Velours Scrunch Suede Boots Coffee:

    Color: Coffee
    Material: Suede
    4(100mm) heel
    Signature red sole x

    Fashion, delicate, luxurious Christian louboutins shoes on sale, one of its series is Christian Louboutin Tall Boots, is urbanism collocation. This Christian louboutins shoes design makes people new and refreshing. Red soles shoes is personality, your charm will be wonderful performance.

  57. Avatar
    beats by dr dre over 2 years later:

    A university studentbeats by dr dre caught by the enemy, the enemy tied him at the poles,just beats solo headphones purple and then asked him: say, where are you? You do not say it electrocuted! Scheap dr.dre beats studio headphones balck/yellowtudents back to the enemy a word, the result was electrocuted, he said: I am TVU.Hot sale beats by dr dre pro headphones

  58. Avatar
    send gifts to dominican republic over 2 years later:

    great blog…Thanks For sharing this

  59. Avatar
    Arcteryx Jackets over 2 years later:

    http://www.arcteryxjackets-sale.com

  60. Avatar
    gucci handbag sale over 2 years later:

    I have many question about your post. I think u should write lot of more details. But Thanks anymore.

  61. Avatar
    hotels singapore over 2 years later:

    Very good to read your most informative article. Its very informative and well written also.

  62. Avatar
    part time cleaning over 2 years later:

    Very impressive post. Thank you for sharing.

  63. Avatar
    graduate school personal statement over 3 years later:

    I found very valuable information from your post.

  64. Avatar
    hair suppliers over 3 years later:

    findingdream provide hair extensions

  65. Avatar
    mbtshoe over 3 years later:

    Australia Beats By Dre Studio dr dre beats headphones beats studio beats pro beats solo hd pro headphones music Official store Monster Beats By Dre Pro

  66. Avatar
    http://www.mydubstep.co.uk/ over 3 years later:

    you shared very informative article. i learnt so much out of it. i appreciate you for such a useful work.

  67. Avatar
    ladbrokes casino over 3 years later:

    This is very much enjoyed for the great technology is visible in this blog that to sharing the amazing services is display in this blog. Thanks a lot for using the nice services in this blog.

  68. Avatar
    bladeless fans over 3 years later:

    Metrics of Moment 67 good post19

  69. Avatar
    louboutin sales over 3 years later:

    Metrics of Moment 68 hoo,good article!!I like the post!141

  70. Avatar
    Tory bruch outlet for sales over 3 years later:

    Our tory burch sandals, tory burch flip flops, tory burch reva flats, tory burch handbags are perfect design and with high discount and free shipping?

  71. Avatar
    NBA jerseys for sales over 3 years later:

    We supply discount authentic NBA Jerseys, NHL Jerseys, NFL Football Jerseys, NBA Jerseys online sale.Here have the best hot team jersey,eg Los Angeles Clippers Jerseys Outlet,New York Knicks Jersey,Miami Heat Jersey Outlet,Boston Celtics Jersey,Chicago Bulls Jersey,Los Angeles Lakers Jersey,Hope you like.

  72. Avatar
    Polo outlet for sales over 3 years later:

    Top polo ralph lauren outlet offer the most popular mens & womens polo ralph lauren and with fashion styles and good materials. Fast delivery!

  73. Avatar
    lelom over 3 years later:

    If you are wondering to buy a snapback hat and don’t know which color to choose, all black hat is a pretty choice for you for red is the main trend color in the summer of 2012.

  74. Avatar
    hermes ciel over 3 years later:

    In the article we are going to look at the range of handbags and the purposes they fulfil?

  75. Avatar
    best cosplay over 3 years later:

    http://www.outfitscosplay.com/cosplay-catalog/d-gray-man-cosplay Deluxe D.Gray-Man Cosplay Costumes for Sale.Find your favorite characters and cosplay outfits from all the popular anime and games.

Comments