The Quality of TDD 46

Posted by Uncle Bob Sun, 17 Feb 2008 02:23:44 GMT

Kieth Braithwaite has made an interesting observation here. The basic idea is that code that has been written with TDD has a lower Cyclomatic Complexity per function compared to code that has not been written with TDD. If this is true then it could imply lower defects because of this.

Kieth’s metric takes in the code for an entire project and boils it down to a single number. His hypothesis is that a system written with TDD will always measure above a certain threshold, indicating very low CC; whereas systems written without TDD may or may not measure above that threshold.

Kieth has built a tool that you can get here that will generate this metric for most java projects. He and others have used this tool to measure many different systems. So far the hypothesis seems to hold water.

The metric can’t tell you if TDD was used; but it might just be able to tell you that it wasn’t used.

Comments

Leave a response

  1. Avatar
    Alicia about 12 hours later:

    Braithwaite is a genius. Im sure this will work perfectly.

  2. Avatar
    Keith about 15 hours later:

    Hi Bob, Actually, its not so simple as that TDD code has less complexity per method absolutely (if only), but rather that there is a preference for more simple methods in TDD code.

    What fascinates me about Rich’s data is that the minimum probability of defects comes at what might be considered quite a high complexity.

    By the way, I’m working on a new version of the tool with some visualization features that makes the differences in codebases a little easier to grasp. Should be available sometime in the next couple of weeks.

    Keith

    (PS, the rule is “i before e, except when it’s not” ;)

  3. Avatar
    www.jamesladdcode.com about 19 hours later:

    This is a great tool for measuring complexity in Java code: http://www.martyandrews.net/resources/complexian.html

  4. Avatar
    Paddy Healey 3 days later:

    Another tool that I think could be interesting is Crap4j: http://www.crap4j.org

  5. Avatar
    Mark Dixon 3 days later:

    I just wanted to follow up to the point about Rich’s data showing low probabilities for surprisingly high complexity values.

    The Enerjy data is calculated entirely at the file level, so you can’t compare the Enerjy numbers with the traditional 7-10 range that applies at method level. This comes back to one of the fundamental problems with CC these days – most metrics are measured at a file level but CC only makes sense at the method level. Averaging the CC over the file makes no sense as the whole point of it is to highlight methods with unusual values. For Enerjy we decided to just sum the CC of all methods in the file to get a file-level metric.

    Another useful approach would be to create a static analysis rule that fires whenever a method has complexity over a certain threshold and then use the number of firings as a predictor. We had a hard time coming up with a threshold that worked well for real-world code, though. Values below around 20 seemed to fire very often and on code that, to our mind, didn’t need refactoring. Values above 20 just seemed pointless :-)

    Mark

  6. Avatar
    unclebob 4 days later:
    Mark Dixon said:

    Another useful approach would be to create a static analysis rule that fires whenever a method has complexity over a certain threshold and then use the number of firings as a predictor. We had a hard time coming up with a threshold that worked well for real-world code, though. Values below around 20 seemed to fire very often and on code that, to our mind, didn’t need refactoring. Values above 20 just seemed pointless :-)

    Well, just to be nasty, let me point out that in all 45,000 lines of FitNesse there is only one function with a CC > 20. It happens to be a big switch statement that translates http error codes into strings. e.g. 404->”Not Found”.

  7. Avatar
    Christoph Beck 4 days later:

    If Rich’s data is on a per file basis, it’s not CC (and shouldn’t be named CC). Rather it comes close to another metric called WMC or “Weighted Methods per Class”, which sums up CC values of a class. For a file based CC metric, it might have been better to take the maximum, but that way, the data presented is not that useful.

    @Keith: Now, where does the magic minimum at 11 come from? Let me guess: in a typical code base we have a relevant amount of trivial, bean-like classes, usually without bugs. If we were looking at the distribution of the number of properties of these bean classes, we might find a maximum at around 5 or 6. Now each property has a getter and a setter with CC of 1, which results in a higher number of bean classes whose CC values sum up to something around 11. I’m pretty sure the function would be monotone increasing if these classes would have been excluded.

    @Bob: I just analyzed fitnesse with STAN. You are right, the project has low CC values. However, to be nasty too, there are around 10 classes in the fitnesse tree with WMC > 38. According to Rich, these classes have > 50% probability of being fault-prone.

  8. Avatar
    Leandro Zis 7 days later:

    @Bob: Why not replace the switch statement with a Map?

  9. Avatar
    unclebob 8 days later:

    @Bob: Why not replace the switch statement with a Map?

    Could do. But why? In this case the switch statement is simple, expressive, and terse.

  10. Avatar
    Keith 16 days later:

    @Mark: is there any chance of the enrjy results being written up in a more transparent style? I had actually missed the point that Christoph points out, that what that chart measures isn’t quite Cyclomatic Complexity. I actually didn’t find it easy to tell from the blog posting (and even more so the paper) exactly what it is that was being measured.

    @bob: high CC methods doing problem-free, easy to understand, easy to test dispatching or marshaling tasks occur in more than one of the codebases I’ve looked at—they are a nice example of how any metric can be misleading if not interpreted properly.

  11. Avatar
    Keith 24 days later:

    By the way, Bob’s link has rotted and a new version of the tool is available form http://www.keithbraithwaite.demon.co.uk/professional/software/#measure

  12. Avatar
    MTS File Converter over 2 years later:

    so i am coming

  13. Avatar
    Android code over 2 years later:

    Hey dear your blog is very much rocking and stunning. This blog has main attraction and high quality features. Due to the latest posts, this blog is relatively famous among the readers of allover the world. You have provided enough information on many different topics. well done.

  14. Avatar
    bag manufacturer over 3 years later:

    system written with TDD will always measure above a certain threshold, indi

  15. Avatar
    Designer Bags over 3 years later:

    Thanks for ur nice sharing!!It help me a lot with those information!!!

  16. Avatar
    Pandora over 3 years later:

    You would want this clarity of detail if you ventured out on a lake every day to fish for dinner.

  17. Avatar
    iPhone contacts Backup over 3 years later:

    They are different. When you want to know. When I come to here, I think I am in the right place. the web gives me a lot of infomation, it is very informative. I think lots of people can learn much here. I will come to here again. Thanks.

  18. Avatar
    pandora uk over 3 years later:

    but it might just be able to tell you that it wasn’t used.

  19. Avatar
    pandora over 3 years later:

    Had a good response nearly accomplished, ended up being evidence studying this & obtained wiped out through an ad pertaining to Shipwreck drops. Been age ranges since i have examined throughout here & remaining because the large internet site alterations lsat summer produced contributing by way of dial-up Distressing. I observe situations are quite similar. Sleepless & sprang over from the Etsy community forums which usually didn’t seize me tonight… Spend some time stalking on the forums & checking out goods available for sale & lately distributed. There had been about 197,500 diamond jewelry listings while i opened our shop (NGHDesigns) in late July. There are over Three hundred,500 today. I’ve had a few achievement & get pleasure from contributing generally there, yet feel glad that’s not the way you maintain a new roof over our mind. Some dealers prosper generally there, others never offer one thing. Even probably the most amazing goods are generally buried inside heap in mere min’s

  20. Avatar
    http://www.blacktowhiteiphone4.com over 3 years later:

    Looking for the latest white iphone 4 Conversion Kit? I am just looking for a web to satisfy all my need. Come and take it home!

  21. Avatar
    axial fan over 3 years later:

    Your site is amazing.I am very impressed to see this,i want to come back for visiting your site.Keep doing Good as well as you can.

  22. Avatar
    Criminal Check over 3 years later:

    So far the hypothesis seems to hold water.

  23. Avatar
    Tenant Screening over 3 years later:

    Another useful approach would be to create a static analysis rule that fires whenever a method has complexity over a certain threshold and then use the number of firings as a predictor.

  24. Avatar
    Criminal Records over 3 years later:

    Now each property has a getter and a setter with CC of 1, which results in a higher number of bean classes whose CC values sum up to something around 11. I’m pretty sure the function would be monotone increasing if these classes would have been excluded.

  25. Avatar
    dswehfhh over 3 years later:

    We are the professional t-shirts manufacturer. t-shirts supplier. t-shirts factory, custom t-shirts.

  26. Avatar
    Designer Sunglasses over 3 years later:

    Buy $10 Replica Designer Sunglasses with 3-day FREE SHIPPING At fashion-world4u you find Imitation

  27. Avatar
    SEO Firm India over 3 years later:

    Finally, got what I was looking for!! Thanks.

  28. Avatar
    Aiken Heno over 3 years later:

    I love this one. A very good post. Thank you. roofing venice

  29. Avatar
    okey oyunu oyna over 3 years later:

    i like it..

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

  30. Avatar
    real estate advertising over 3 years later:

    Your blog is awesome! :-)

  31. Avatar
    beats by dr dre headphones over 3 years 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.

  32. Avatar
    Jewellery over 3 years later:

    Online UK costume and fashion jewellery shop with,

  33. Avatar
    Unique Article Wizard over 3 years later:

    very nice article ! quite helpful for a newbie like me !

  34. Avatar
    Diablo3 over 4 years later:

    hmm ,i’m not sure if this is what i’m looking for but anyway this is interresting and could be useful some day,thanks for taking time to write such cool stuff

  35. Avatar
    DR OZ african Mango over 4 years later:

    i think this can be bookmarked for future referenses , i think i could make use of this ,many thanks for the time you spend writing this , best regards for Canada

  36. Avatar
    best sleep aid over 4 years later:

    hmm ,i’m not sure if this is what i’m looking for but anyway this is interresting and could be useful some day,thanks for taking time to write such cool stuff

  37. Avatar
    shanewatson404@gmail.com over 4 years later:

    Wonderful technology is visible in this blog that Director of Operations Job Description to using the nice services in this blog Accounting Job Description This is really superb info is visible in this blog that to using the great technology in this blog and wonderful technology is visible in this blog Graphic Designer Job Description I had really satisfied by Legal Assistant Job Description the nice technology in this blog

  38. Avatar
    Tips For Bowling over 4 years later:

    The actual course is called fishery studies, and you study general aquatics and fishery management. Tom Felton

  39. Avatar
    Brisbane Limo Hire over 4 years later:

    I love this one. A very good post. Thank you. Brisbane Limo Hire

  40. Avatar
    Brisbane Limo Hire over 4 years later:

    I love this one. A very good post. Thank you. Brisbane Limo Hire

  41. Avatar
    iPad to Mac over 4 years later:

    It is true that if we want to make a great improve in programing. we need do understant the exactly mean of each conception. And we need do practice. so, why not think about doing it now.

  42. Avatar
    louboutin sales over 4 years later:

    The Quality of TDD 41 hoo,good article!!I like the post!64

  43. Avatar
    bladeless fans over 4 years later:

    The Quality of TDD 42 good post113

  44. Avatar
    Free Hacks over 4 years later:

    The quality of free hacks is best. Download them easy as a rocket! :D

  45. Avatar
    hermes togo birkin over 4 years later:

    Gone are the days when designing and selling baby clothes used to be for those people who had lots of money?

  46. Avatar
    Silicone Molding over 4 years later:

    With more than 20 years of experience, Intertech provides an extensive integrated operational ability from design to production of molds 100% made in Taiwan. Additional to our own mold making factory, we also cooperate with our team vendors to form a very strong working force in Taiwan.

    For the overseas market, we work very closely with local representatives in order to take care of the technical communication and after-sales service to our customers. We also participate in the EUROMOLD & FAKUMA exhibitions and meet our customers every year in Europe. By concentrating on mold “niche markets”, we play a very useful mold maker role from the Far East whenever customers want to develop their new projects. We provide services from A to Z to our customers on a very economic cost and effect basis.

Comments