Crash Test Dummies 31

Posted by Uncle Bob Mon, 11 Feb 2008 20:25:41 GMT

Even the most dedicated TDDer would not write a unit test for “Hello, world.” Even the most fervent FitNesse wonk would not write an acceptance test for a program that prints the first 10 squares. This implies that there are programs too simple for TDD. So where do we draw the line?

Accellerating towards a brick wall.

The issue, of course, is speed. For projects that are too simple to require unit tests, or acceptance tests, you can go a lot faster by not writing them. I can write HelloWorld or SquaresOfIntegers in much less than half the time that it would take me to write unit and acceptance tests for those programs. And I know they work because I can run them and manually validate them almost instantly.

For large projects the opposite is true; unit tests and acceptance tests make us go faster. The increase in speed comes from reduced debugging time and reduced ambiguity. When programmers follow the three laws of TDD, writing tests and code in a very short cycle, they reduce their debugging time by perhaps as much as 90%. Code written this way is much easier to understand since the tests act as little code examples. It is also much easier to change since the suite of tests act as instant fault detection.

Acceptance tests act as executable requirements. When Business Analysts and QA staff write these tests at the beginning of an iteration, they are producing unambiguous requirements. Programmers cannot help but implement the features as specified, since the tests will fail otherwise. So there is much less requirements thrashing and rework.

The cutoff for unit tests is probably around 10 lines of code. That is, it is probably faster to write 10 lines of code by writing unit tests first, than to just write the 10 lines and hope for the best. As an example of this, I recently wrote a dumb stack class. I found that I had to write the simple unit tests first in order to be sure that I had properly implemented the algorithm. Writing the stack without tests just left too much room for something to go wrong.

Acceptance tests are another matter. You don’t really feel the benefit of acceptance tests for projects below a few thousand lines of code. The problem is that in such projects manual testing is cheap. The programmers and customers can just run the program for 15 minutes and prove that it works. Any team caught in a project of this size will immediately feel the drag imposed by writing automated acceptance tests. They’ll look at how much they could get done without them. They’ll envy the time lost to those tests. And they may make an obvious, but possibly catastrophic choice.

The problem is that as the project grows, the manual testing time gets longer. It gets harder and harder to remember everything that needs to be tested. So manual test plans have to be written. Following those plans becomes ever more tedious and time consuming, so customers hand this off to a QA team.

The QA team requires staffing; and as the project grows that staffing must increase. Test time also increases, from minutes to days to weeks. Eventually the cost of running manual QA tests becomes significant enough to be a large number on the project cost spreadsheet. Managers resist the addition of more QA staff and suggest working “smarter”.

With staffing throttled, QA gets ever more stressed. Development seldom delivers builds on time, yet the release date cannot move; so QA must make up the slack. They do this by choosing which tests to run and which tests to assume still work. They must quickly execute thousands of horribly tedious manual tests and interpret the results. Under pressure to deliver, the choices they make inevitably, and ironically, reduce the quality of the result.

  • * *

The name of this blog is a metaphor for a team on a smallish project who decides that they can go faster if they don’t write automated acceptance tests. They’re right—for now.

Comments

Leave a response

  1. Avatar
    Anthony Bailey about 2 hours later:

    Well said. I’ll venture to add a note about retrofitting…

    We know it is harder to add unit tests to a class that was designed independent of testing concerns than it is to test-drive its design from the get go. In my experience the pattern repeats at a system level: adding acceptance tests to a creaking existing system is usually made more tricky by the fact that it doesn’t expose the right seams between its architectural layers.

    So if you’re pretty sure you’re going to need acceptance tests eventually, it makes a lot of sense to grow them alongside the system from the start.

    My inner core cries YAGNI at the assertion above. But it is much more plausible to believe – and easier to predict – that a piece of code will never need to be longer than ten lines than it is to know that a useful system will never want or need to exceed the few thousand lines where acceptance tests will reduce pain. So if we have any ambition for our projects, we should expect them to grow up big – and strong – and so ensure they are acceptably testable from day one.

  2. Avatar
    Jeff Langr about 8 hours later:

    Do it now, do it now. Unless you’re absolutely certain that you’ll never touch the code again, or that you’ll never need to maintain it in a production environment.

    Once the ball is rolling, you won’t have the time to come back and clean things up, do them the right way. The automated tests will already be behind, and you’ll have a tough time just keeping up with new requirements. No one will let you take “time off” just to get caught up. You’ll always have “more holes” if you wait.

    From a developer’s perspective, the only things I don’t unit-test-drive are bits of throwaway code or spikes. If it’s going into production, I test-drive it. Also, if we view TDD as a form of documentation, there are almost no excuses to not do it. Anything less is cheating your fellow developer.

  3. Avatar
    Viktor about 16 hours later:

    I suggest you test it as soon as possible. It’s best that everything is fixed before there would be errors that others would find before you do. Best that you know first.

  4. Avatar
    Ravi Venkataraman 1 day later:

    I believe in testing, and did so long before it became popular and almost faddish. I spend at least 1.5 hours testing for every hour spent writing code.

    That said, I feel that TDD is the wrong approach to testing. It is too fine grained a level. In some sense, the fact that one has to write so much code to test even simple things is discouraging.

    The interesting fact for me is the statement by the original poster that some projects are too simple to require unit tests, and that the author is comfortable with small functions/methods that obviously work.

    If we design things such that these functions/methods are “atomic”, that is, perform one task and one task only, then testing becomes much less onerous. Then, following the Unix idea, one can combine these small pieces of code in creative ways to meet the business requirements.

    Unfortunately, the mainstream languages, Java, C#, etc., do not really permit you to build the proper abstractions in an easy manner. Therefore, one ends up writing lots of code and spending lots of time testing. This also occurs in part because the current fad of Object-Oriented development leads to rigid hierarchy of classes. Such hierarchies often turn out to be very difficult to change, leading to “hard”ware rather than “soft”ware.

    While Object-Orientation is one way to solve problems, it is not the only way, and is not the correct way for many classes of problems. Some of the most reusable code are mathematical libraries (sine, cosine, exp, etc.) and none of it is object-oriented. Unfortunately, Java and C# proponents and self-styled thought leaders keep implying that OO is the one true way, and the vast majority of developers, for whom programming is just a way to earn a living, are happy to accept this idea uncritically.

    All this leads to all sorts of fads every few years, such as “pair programming”, “TDD” as an alternative to design, etc. Can TDD lead to the Quick Sort algorithm? Of course not! Yet, its proponents keep implying that TDD is the one true way to test, with unsubstantiated claims that it reduces debugging time by “90%”, is much easier to understand, and is easier to change.

    While TDD (and other approaches) may work for many situations, the implication that it is the only way to do work, and the continuous mockery of other approaches stifles curiosity and creativity, and ossifies an approach that may not be the best.

  5. Avatar
    drop 2 days later:

    we talk alot about junit- but we do not know realy how to design such tests. I recently read about some nice technique to design junit tests. The idea is to document the required method as properties such as property_1 , propert_2, ... property_n and for each propery_i to write propert_i_assertion. Than, for each run all over the propert_asserions and call all the property_1 , propert_2, ... property_n This technique make sure that your method realy do what it documented to do

  6. Avatar
    unclebob 3 days later:

    Ravi Venkataraman said:

    I believe in testing, and did so long before it became popular and almost faddish. I spend at least 1.5 hours testing for every hour spent writing code.

    I think that’s laudable. However, by following TDD I spend significantly less time testing, since my tests are automated. The time I spend writing tests is also smaller than 1.5X since the size of my tests is about 50% the size of my production code.

    That said, I feel that TDD is the wrong approach to testing. It is too fine grained a level. In some sense, the fact that one has to write so much code to test even simple things is discouraging.

    I would say, to the contrary, that spending 1.5X time in repeatedly running manual tests is more discouraging than investing an extra 50% in automated tests.

    The interesting fact for me is the statement by the original poster that some projects are too simple to require unit tests, and that the author is comfortable with small functions/methods that obviously work.

    If we design things such that these functions/methods are “atomic”, that is, perform one task and one task only, then testing becomes much less onerous. Then, following the Unix idea, one can combine these small pieces of code in creative ways to meet the business requirements.

    I completely agree. We want to organize our code into very small classes and functions which do one thing.

    Unfortunately, the mainstream languages, Java, C#, etc., do not really permit you to build the proper abstractions in an easy manner. Therefore, one ends up writing lots of code and spending lots of time testing. This also occurs in part because the current fad of Object-Oriented development leads to rigid hierarchy of classes. Such hierarchies often turn out to be very difficult to change, leading to “hard”ware rather than “soft”ware.

    I completely disagree. One can write very nice software using OO techniques. Indeed, the enhanced ability to manage source code dependencies greatly increass the ability to create small atomic classes and functions.

    While Object-Orientation is one way to solve problems, it is not the only way, and is not the correct way for many classes of problems. Some of the most reusable code are mathematical libraries (sine, cosine, exp, etc.) and none of it is object-oriented. Unfortunately, Java and C# proponents and self-styled thought leaders keep implying that OO is the one true way, and the vast majority of developers, for whom programming is just a way to earn a living, are happy to accept this idea uncritically.

    I agree that OO is not the “one true way”. I also agree that OO is not universally appropriate. Good programmers should avail themselves of many different programming styles and tools. Having said that, I would not want to do without my OO tool.

    All this leads to all sorts of fads every few years, such as “pair programming”, “TDD” as an alternative to design, etc. Can TDD lead to the Quick Sort algorithm? Of course not! Yet, its proponents keep implying that TDD is the one true way to test, with unsubstantiated claims that it reduces debugging time by “90%”, is much easier to understand, and is easier to change.

    New techniques are often thought of as “fads” by some of both their adherents and opponents. I think the practices of TDD and Pairing have survived long enough, and enjoyed enough success, that they are no longer “fads’. Nowadays they are well-established practices that are available for teams to use.

    I will plead guilty to using the 10:1 claim. It comes from a study preformed by Symantec some years ago. By following the practices of TDD they measurably reduced their defects to the field by 10:1. Does that really translate into a 10:1 reduction in debugging time?

    While TDD (and other approaches) may work for many situations, the implication that it is the only way to do work, and the continuous mockery of other approaches stifles curiosity and creativity, and ossifies an approach that may not be the best.

    There will always be mockery. Humans like to affiliate into groups and then mock the groups they don’t belong to. Reasonable people look past the mockery to the substance of the issues. What are Obama’s policies? Does McCain’s recent voting record really establish him as a conservative?

    Yes, there is value in TDD. Is TDD the one true way? Of course not. Better ways will be found and then the new group will berate the TDDers for being old-fashioned and stodgy. And the story will continue.

    We didn’t light the fire.

  7. Avatar
    Ravi Venkataraman 3 days later:

    In UB’s response, no mention is made of my claim that TDD can not be used to generate the QuickSort algorithm (or other neat agorithms). Thus claims to the effect that TDD improves the quality of the code lead us to ask, “Quality improvement compared to what?”. That is not clear at all. If one wants to say that it is better than some code that would have been developed otherwise, then how do you realistically compare with what “might” have been developed?

    Also, UB seems to conflate the criticism of TDD with the use of automated tests. One can use automated tests without using TDD.

    While mentioning the 1.5 factor, I should clarify that this includes more than unit testing, it includes integration with other parts of the application, too, often including GUI testing, and is spread over the development lifetime of the application. If you look at TDD and the time spent running the tests frequently, you’ll probably get a similar time ratio during the development phase. Moreover, while TDD confines itself almost exclusively to unit tests, testing in general is more general and thorough.

    In the one place where UB completely disagrees, there seems to be some misunderstanding of my statement. I specifically mentioned mainstream OO languages. Building the proper abstractions, though possible, is not easy in Java or C# or C++. And once again a claim is thrown in

    One can write very nice software using OO techniques. Indeed, the enhanced ability to manage source code dependencies greatly increass the ability to create small atomic classes and functions.
    that OO provides enhanced abilities to maintain source code. Compared to what? And, in
    Having said that, I would not want to do without my OO tool.
    the implication seems to be that I am advocating that we absolutely refuse to use OO, which is nowhere stated in my response, and which is not what I am advocating.

    Like UB, I feel that we should use the right tool for the job, not be wedded to one approach. The hosannas sung to OO by many of its adherents seems to suggest to the community that it is the best approach to any problem. And that implication is my main target of criticism, as far as OO is concerned.

    I personally choose to focus on the problem, and a practical solution, without worrying about the specific technology or methodology to use, whether it be OO, data-driven programming, or something else. I want to deliver “soft“ware applications, that are “soft” and easy to change and maintain, that is all. OO does not seem to be the answer to all problems.

    I can understand that as a consultant one has to come up with something “new” every so often, as can be seen in Martin Fowler’s blog articles glorifying the ability of ThoughtWorks’ super-developers and the “new” techniques they use. But the average programmer does not have to blindly follow all these ideas without first examining them critically.

  8. Avatar
    Dean Wampler 3 days later:

    The “quick sort conundrum” has come up before in other venues.

    TDD is not a silver bullet that solves all problems. Nor should it be used slavishly. There is a place for exploratory programming where you try out ideas and test-driving the exercise may not be helpful. Algorithm discovery is a case in point.

    However, once you have figured out what to do, then you should test drive the final implementation. That process will most often clarify and simplify the design and implementation. It will also result in an implementation that you know is working and it will continue to work for as long as the test suite is executed on a regular basis. (Even if no further changes are made to the code, the rest of “world” is probably changing around it.)

    The essential goal of TDD is to build optimal production software. When you’re ready to implement that algorithm for production use, that’s when you need to use TDD.

    Neither is OO a silver bullet. The resurgent interest in functional programming is a good thing. Relational databases didn’t get replaced by OO databases because the value proposition wasn’t good enough to justify the change.

    Finally, you offer no evidence to support your suggestion that “building proper abstractions, though possible, is not easy in Java or C# or C++”. These languages would be historical relics if this were true. Sure, some particular abstractions are easier in some languages than others, but the general statement is evidently false. The issues with rigid class hierarchies, etc. were really rookie mistakes. We know how to use inheritance and composition effectively now.

  9. Avatar
    unclebob 4 days later:
    Ravi said:

    Thus claims to the effect that TDD improves the quality of the code lead us to ask, “Quality improvement compared to what?”. That is not clear at all.

    TDD improves the quality of code in terms of its testability and test coverage.

    Clearly you can achieve high testability and coverage without using TDD. On the other hand you cannot achieve low testability and coverage if you use TDD.

  10. Avatar
    Jeff Langr 5 days later:

    > In UB’s response, no mention is made of my claim that
    > TDD can not be used to generate the QuickSort
    > algorithm (or other neat agorithms). Thus claims to
    > the effect that TDD improves the quality of the code
    > lead us to ask, “Quality improvement compared to
    > what?”.

    Bob also posts recently on a Keith Braithwaite blog entry that demonstrates lower cyclomatic complexity for TDD-built functions in a (limited?) study. Test-driven systems I’ve worked on have demonstrated lower coupling metrics than the predominance of the (hundreds of) other systems I’ve worked on. There are other research studies that demonstrate positive aspects of TDD. I’m sure there are some studies that show negative aspects of TDD. There are case studies and many anecdotal claims.

    What exactly do you want or are you looking for? It seems that you (and some other very vocal posters) are looking for something to convince you that TDD is a complete waste of time. If that would make you happy, I suggest you go and do the appropriate research.

    Most of the developers I know that are into TDD and OO are also exploring other things such as functional programming languages. I still work in procedural languages. We discuss OO a lot because that’s what most shops are using. And, it is a fine tool for a very large number of applications (no, of course not all of them).

    I hear none us saying that TDD or OO solves all problems. What we generally try to do is take tools and figure out the best ways to use them. Just because we don’t spend all our time decrying their limitations doesn’t mean we don’t recognize them.

    Yes, TDD won’t guide me to build some complex algorithms. So what? I’ve heard this often as a reason that TDD sucks. Compared to what? It’s a straw man. There is no known technique that will derive all complex algorithms. But TDD does help derive the predominance of algorithms in systems I work on—most algorithms are simple enough.

    For the rare situation where I have to derive a complex algorithm and TDD isn’t helping, I follow Mr Wampler’s solid recommendation.

  11. Avatar
    Ravi Venkataraman 7 days later:

    I am not saying that TDD or OO is useless. I am merely saying that the OO proponents and TDD adherents claim that (or seem to be claiming that) their method is the best, if you do not follow their method, you are a troglodyte, or a Luddite with your head buried in the sand.

    Some of the responses to my posts do suggest strongly that

    However, once you have figured out what to do, then you should test drive the final implementation.
    Note the emphasis in the original. The funny thing is, UB said in his original post that for some types of code, you do not need TDD.

    As for cyclomatic complexity, I haven’t found it to be a problem in my code. As a consultant, I’ve ceased to be amazed at the low quality of the designs that I’ve seen. No amount of good coding practices will salvage a bad design.

  12. Avatar
    unclebob 12 days later:
    Ravi said:

    I am not saying that TDD or OO is useless. I am merely saying that the OO proponents and TDD adherents claim that (or seem to be claiming that) their method is the best, if you do not follow their method, you are a troglodyte, or a Luddite with your head buried in the sand.

    I suppose there are some TDD proponents who say things like that. It is common for both proponents and detractors to misunderstand their own enthusiasm, and the enthusiasm of others. What either side says is not really germane to the argument of whether TDD is a beneficial practice.

    The observations so far suggest that where TDD is practiced, CC is lower and defects to the field are reduced by as much as 10X.

    TDD is almost certainly not the only way to achieve these kinds of results. On the other hand the correlation is worthy of attention.

  13. Avatar
    MTS File Converter over 2 years later:

    so what….......

  14. Avatar
    bag factory over 3 years later:

    not write an acceptance test for a program that prints the first 10 squares. This i

  15. Avatar
    Pandora over 3 years later:

    and he showed me a function he was writing. In the midst of the function I saw this

  16. Avatar
    hublot replicas over 3 years later:

    a trial conducted under omega seamaster gmt replica under the auspices of the National Institutes breitling crosswind watches Institutes of Health’s National Center for Complementary tag heuer replica watches Complementary and Alternative

  17. Avatar
    Silicone Molding over 3 years later:

    Intertech Machinery Inc. provides the most precise Plastic Injection Mold and Rubber Molds from Taiwan. With applying excellent unscrewing device in molds, Intertech is also very professional for making flip top Cap Molds in the world.

  18. Avatar
    okey oyunu oyna over 3 years later:

    i love it

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

  19. Avatar
    wastewater management over 3 years later:

    Programmers should follow the following rules. I want to do that in a new manner in optimize position. Optimization is the best process in Google Optimization process. It will read your code at W3 complaints and make it successive for the rules and regulations of programming language.

  20. Avatar
    Air Jordan 3 over 3 years later:

    Lakers + Bob Weiss? Pettit (Bob Pettit) 1958 Bob St. speaking difficult eagle? Pettit (Bob Pettit) 1957 St. Martin speaking difficult eagle Bo? Marcoussis (Bob Cousy) 1956 Bob Kay were special people? Pettit (Bob Pettit) 1955 [url=http://www.guccihandbagss.com]Gucci outlet[/url] St. eagle [url=http://www.guccihandbagss.com/gucci-men-s-C35.html]Gucci mens wallets[/url] difficult than people say? Sand People Man (Bill Sharman) 195…

  21. Avatar
    beats by dr dre headphones over 3 years later:

    Beats by dr dre studio with look after talk in white. extra attributes on Monster Beats By Dr. Dre Pro Headphones Black a specific tri-fold design and design and carrying circumstance which make for compact and uncomplicated safe-keeping when not in use. Beats by dr dre solo .

  22. Avatar
    beats by dre store over 4 years later:

    Optimization process. It will read your code at W3 complaints and make it successive for the rules and regulations of programming language.high quality headphones new design headphones

  23. Avatar
    Diablo3 over 4 years later:

    Blog posts about wedding and bridal are always rare to find , at least with great quality,you qualify for a great blog post writer title,kep the great job happening

  24. Avatar
    best sleep aid over 4 years later:

    Blog posts about wedding and bridal are always rare to find , at least with great quality,you qualify for a great blog post writer title,kep the great job happening

  25. Avatar
    christian louboutin over 4 years later:

    Christian Louboutin Rolando Hidden-Platform Pumps Golden is a fashion statement – that’s sexy, it makes you look longer highlight, and it highlights the curves in the woman body and makes the body look more elegant and thinner without any diet.

    ?Brand: Christian Louboutin ?Material: Golden leather ?Specialty: Signature red sole ?Color: Golden ?Heel height: Approximately 130mm/ 5.2 inches high and a concealed 20mm/ 1 inch platform ?Condition: Brand New in box with dust bags & Original Box

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

  26. Avatar
    beats by dr dre over 4 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

  27. Avatar
    Awesone over 4 years later:

    I love you.

  28. Avatar
    louboutin sales over 4 years later:

    Crash Test Dummies 27 hoo,good article!!I like the post!63

  29. Avatar
    bladeless fans over 4 years later:

    Crash Test Dummies 28 good post144

  30. Avatar
    Injection mold over 4 years later:

    Intertech Machinery Inc. provides the most precise Plastic Injection Mold and Rubber Molds from Taiwan. With applying excellent unscrewing device in molds,

    Intertech is also very professional for making flip top Cap Molds in the world. Mold making is the core business of Intertech (Taiwan). With world level technology, Intertech enjoys a very good reputation for making Injection Mold and Plastic Molds for their worldwide customers.

  31. Avatar
    bluecotton over 4 years later:

    An increasingly more common debate in the last few years among parents and couples has been to question what type of diaper to actually use for their babies?

Comments