TDD is how I do it, not what I do 51

Posted by Brett Schuchert Mon, 21 Jul 2008 19:32:00 GMT

“Do not seek to follow in the footsteps of the men of old; seek what they sought.” ~Basho

That quote resonates with me. I happend across that a few days after co-teaching an “advanced TDD” course with Uncle Bob. One of the recurring themes during the week was that TDD is a “how” not a “what”. It’s important to remember that TDD is not the goal, the results of successfully applying TDD are.

What are those results?

  • You could end up writing less code to accomplish the same thing
  • You might write better code that is less-coupled and more maleable
  • The code tends to be testable because, well, it IS tested
  • The coverage of your tests will be such that making significant changes will not be too risky
  • The number of defects should be quite low
  • The tests serve as excellent exampls of how to use the various classes in your solution
  • Less “just in case” code written, which typically doesn’t work in those cases that they targeted

Right now I do not know of a better way to accomplish all of these results more effectively than practicing TDD. Even so, this does not elevate TDD from a “how” to a “what.” TDD remains a technique to accomplish thigns I value. It is not a self-justifying practice. If someone asks me “why do we do it this way”, saying something like “we practice TDD” or “well you don’t understand TDD” is not a good answer.

We had an interesting result during that class. One group was practicing Bob’s three rules of TDD (paraphrased);
  • Write no production code without failing tests
  • Write only enough test code so that it fails (not compiling is failing)
  • Write only enough production code to get your tests to pass.
But they ended up with a bit of a mess. Following the three rules wasn’t enough. These rules are guiding principles, but those three rules mean nothing if you forget about clean code, refactoring and basic design principles (here are a few):
  • S.O.L.I.D.
  • F.I.R.S.T.
  • Separation of Concerns (Square Law of Computation)
  • Coupling/Cohesion
  • Protected Variation
  • ...

TDD is a means to an end but it is the end we care about. What is that end? Software that has few defects and is easy to change. Tests give us that. Not testing generally does not give us that. And testing in a common “QA over the wall” configuration typically does not cut it.

Since I do not know how to so easily produce those results in any other way. TDD becomes the defacto means of implementation for me. That doesn’t mean I should turn a blind eye to new ways of doing things. In lieu of any such information, however, I’ll pick TDD as a starting point. This is still a “how” and not a “what”.

It turns out that for me there are several tangible benefits I’ve personally experienced from practicing TDD:
  • Increased confidence in the code I produce (even more than when I was simply test infected)
  • Less worrying about one-off conditions and edge cases. I’ll get to them and as I think about then, they become tests
  • Fun

Fun?

Yes I wrote fun. There are several aspects of this:
  • I seem to produce demonstrable benefits sooner
  • I actually do more analysis throughout
  • I get to do more OO programming

Demonstrable Benefits Sooner

Since I focus on one test at a time, I frequently get back to running tests. I’m able to see results sooner. Sure, those results are sometimes disjoint and piecemeal, but over time they organically grow into something useful. I really enjoy teaching a class and moving from a trivial test to a suite a tests that together have caused what students can see is a tangible implementation of something complex.

More Analysis

Analysis means to break into constituent parts. When I practice TDD, I think about some end (say a user story or a scenario) then I think about a small part of that overall work and tackle it. In the act of getting to a test, I’m doing enough analysis to figure out at least some of the parts of what I’m trying to do. I’m breaking my goal into a parts, that’s a pretty good demonstration of analysis.

More OO

I like polymorphism. I like lots of shallow, but broad hierarchies. I prefer delegation to inheritance. But often, the things I’m writing don’t need a lot of this – or so it might seem.

When I try to create a good unit test, much of what I’m doing is trying to figure out how the effect I’m shooting for can be isolated to make the test fast, independent, reliable … To do so, I make heavy use of test doubles. Sometimes I hand-roll them, sometimes I use mocking libraries. I’ve event used AOP frameworks, but not nearly as extensively.

Doing all of this allows me to use polymorphism more often. And that’s fun.

Conclusion

Am I wasting time writing all of these tests? Is my enjoyment of my work an indication that I might be wasting the time of my product owner?

Those are good questions. And these are things you might want to ask yourself.

Personally, I’m pretty sure I’m not wasting anyone’s time for several reasons:
  • The product owner is keeping me focused on things that add value
  • Short iterations keep me reigned in
  • I’m only doing as much as necessary to get the stories for an iteration implemented
  • The tests I’m writing stay passing, run quickly and over time remain (become) maintainable

Even so, since TDD is a how and not a what, I still need to keep asking myself if the work I’m doing is moving us towards a working solution that will be maintainable during its lifetime.

I think it is. What about you?

Comments

Leave a response

  1. Avatar
    Bheeshmar about 13 hours later:

    Excellent observation. I think this is what I’ve had trouble articulating to people who are reluctant to give TDD the time it needs to realize the benefits.

  2. Avatar
    John 3 days later:

    What’s FIRST stand for?

  3. Avatar
    Brett L. Schuchert 4 days later:

    F.I.R.S.T. – A Mnemonic to remember when writing unit tests.

    • Fast
    • Independent
    • Repeatable
    • Self-Validating
    • Timely
  4. Avatar
    Keith Ray 8 days later:

    One lesson learned: don’t forget the refactoring!

    I’ve repeatedly had to learn not to leave out steps in Test/Code/Refactor.

  5. Avatar
    Dagfinn Reiersøl 11 days later:

    One reason TDD is fun, or at least more pleasant, is that it’s less stressful. Fewer hard-to-find bugs, fewer nasty surprises.

  6. Avatar
    Dan Bergh Johnsson 14 days later:

    Very important to keep the reasons in mind, so we do not revert into just doing the moves “of the cult”.

    Also, I like the “fun” part – we tend to be better at things we enjoy. For me TDD is also compelling because it gives a good, warm, fuzzy feeling inside. Have thought a lot about why it is so …

    [http://dearjunior.blogspot.com/2008/02/psychology-of-tests-testing-and-tdd.html]

  7. Avatar
    D Cate 2 months later:

    “The coverage of your tests will be such that making significant changes will not be too risky”

    Seems like it not only does it allow you to understand what can be changed, but it can also be used to definitely identify the risks that are currently acceptable. That means risk assessment based on well written tests are better than any BRDs. A lot of projects are started to determine code risk, exposure, etc and this just provides a tighter bond.

  8. Avatar
    Cohen 2 months later:

    What does SOLID stand for?

  9. Avatar
    Tim Ottinger 2 months later:
  10. Avatar
    Alexander Poluektov 4 months later:
    > What does SOLID stand for?
    • Single Responsibility Principle
    • Open-Closed Principle
    • Liskov Substitution Principle
    • Interface Segregation Principle
    • Dependency Inversion Principle
  11. Avatar
    Wmv to ipad about 1 year later:

    so waht

  12. Avatar
    Kenton Trocinski over 2 years later:

    great topic. Thanks for your article, its been http://www.china-ouya.com very helpful. Thanks for sharing your information.

  13. Avatar
    fdg over 2 years later:

    ????“??”?? ???????????

    ??20??80??????“??”??21???????

    ??????“?”???????VI??

    ???????????????????????????

    ??????????

  14. Avatar
    Baby Name Meanings over 2 years later:

    That means risk assessment based on well written tests are better than any BRDs. A lot of projects are started to determine code risk, exposure, etc and this just provides a tighter bond. Baby Name Meanings

  15. Avatar
    Pandora over 2 years later:

    This looks a lot like the way “good” inheritance should behave.

  16. Avatar
    pandora uk over 2 years later:

    Doing all of this allows me to use polymorphism more often. And that’s fun.

  17. Avatar
    gas card over 2 years later:

    Thanks for posting such an informative article. I gain more knowledge and information in this article. I will visit your blog to keep updated. gas cards

  18. Avatar
    viagra over 2 years later:

    Very good explanation of what you expect Anyway, I enjoy your blog as I learn a lot viagra

  19. Avatar
    Steel Building over 2 years later:

    Very interesting and informative article indeed. thanks for posting. http://www.AlliedBuildings.com

  20. Avatar
    Steel Framed Structure over 2 years later:

    Its very awesome article,all the content is so beneficial and valuable for us.Thanks for sharing nice and pretty post. Steel Framed Structure

  21. Avatar
    http://www.blacktowhiteiphone4.com over 2 years later:

    As the Apple iphone 4 bumper is taken off the marke, will white iphone 4 available soon?

  22. Avatar
    Criminal Records over 3 years later:

    When I try to create a good unit test, much of what I’m doing is trying to figure out how the effect I’m shooting for can be isolated to make the test fast, independent, reliable …

  23. Avatar
    mosic over 3 years later:

    I am really appreacite!

  24. Avatar
    Jones over 3 years later:

    I am really appreacite!

  25. Avatar
    Backup iPhone SMS over 3 years later:

    Keep your Contacts and SMS safe! Actually, the contacts and SMS have more values than a cell phone’s own value. You can pay money to buy a new iPhone, but cannot buy your lost contacts and SMS back. So it’s important for you to backup your contacts and SMS in iPhone. And we recommend you backup contacts and SMS regularly. Our backup software can help you take a snapshot for your contacts and SMS. Your important personal information will be never lost.

  26. Avatar
    Sunglass over 3 years later:

    Sunglasses

  27. Avatar
    cheap true religion los angeles over 3 years later:

    hni

    Method of washing and maintenance of True Religion Jeans for cheapjeans 1,
  28. Avatar
    Newman over 3 years later:

    That’s a remarkably bold assertion from a Tory PPC from Scotland! Now I know you are an endangered species, but a little perspective is required.

  29. Avatar
    Maria Kramer over 3 years later:

    I like this post. A really good one. Thanks guys. roofing venice

  30. Avatar
    tiffany outlet over 3 years later:

    thank you for nice information.. I am going to bookmark this page. and i will also suggest my friends about this post.

  31. Avatar
    okey oyunu oyna over 3 years later:

    thanks a lot for it

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

  32. Avatar
    cheap brand watches over 3 years later:

    It is really a nice post, it is always great reading such posts, this post is good in regards of both knowledge as well as information. Very fascinating read, thanks for sharing this post here.

  33. Avatar
    real estate advertising over 3 years later:

    I read your article and I really love it Thank you :-)

  34. Avatar
    mac cosmetics over 3 years later:

    haha, I love them so much too, they are my treasure xP

  35. Avatar
    coach purses over 3 years later:

    Mr Coates coach purses is the longest U.S. market popular with one of the most successful leather brand. Mr Coates coach purses store represents the most admirable American fashion innovative style and traditional skills . Mr Coates coach bags have durable quality and exquisite technology, Conspicuous Coach Heels in the female consumers have good reputation. Welcome to our shop Elegant Coach Purses

  36. Avatar
    Jewellery over 3 years later:

    Online UK costume and fashion jewellery shop with, Online UK costume and fashion jewellery shop with, Online UK costume and fashion jewellery shop with, Online UK costume and fashion jewellery shop with, Online UK costume and fashion jewellery shop with,

  37. Avatar
    Bounce House Rental over 3 years later:

    I got these guides very helpful. Here I got much stuff to know..Bounce House Rental

  38. Avatar
    cartier bangle over 3 years later:

    http://www.alijewelry.com/burberry-earring-c-10.html"> Burberry Earring ,
    http://www.alijewelry.com/burberry-bangle-c-11.html"> Burberry Bangle ,
    http://www.alijewelry.com/bvlgari-earring-c-12.html"> Bvlgari Earring ,

  39. Avatar
    beats by dre store over 3 years later:

    shop with, Online UK costume and fashion jewellery shop with, Online UK costume and fashion jewellery shop with,cheap beats by dre beats by dre store

  40. Avatar
    Faviana over 3 years later:

    Thank you for the wonderful article! I want to buy the Jovani dress. Many brides-to-be around me find it difficult to buy a La Femme dress. I think they will be very happy when they read your article.

  41. Avatar
    beats by dr dre over 3 years later:

    good in regards of both knowledge as well as information. Very fascinating read, thanks for sharing this post here.beats by dr dre beats by dre sale

  42. Avatar
    Diablo3 over 3 years later:

    it needs a bokmark so i can come back to it later ,nice stuff

  43. Avatar
    Tips For Bowling over 3 years later:

    There are a couple of carp fishing books I’ve been reading. I’m very interested in that line of books, because I think they write very well, carp anglers, about the general environment. Tom Felton

  44. Avatar
    christian louboutin over 3 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.

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

  46. Avatar
    iPhone contacts backup over 3 years later:

    Get to know about C# and C++. In fact, I find there is no much difference between the two. However, If we want to do much better. I need work hard.

  47. Avatar
    bladeless fans over 4 years later:

    TDD is how I do it, not what I do 46 good post11

  48. Avatar
    louboutin sales over 4 years later:

    TDD is how I do it, not what I do 47 hoo,good article!!I like the post!79

  49. Avatar
    longchamp outlet online over 4 years later:

    sadsdsdasdasdasdasad

  50. Avatar
    hermes plume over 4 years later:

    It is a commonly accepted fact in the fashion world that there are three things that make any woman stand out in the crowd?

  51. Avatar
    cosplay stores over 4 years later:

    http://www.outfitscosplay.com/cosplay-catalog/cowboy-bebop-cosplay Deluxe Cowboy Bebop Cosplay Costumes for Sale.Find your favorite characters and cosplay outfits from all the popular anime and games.

Comments