<p>I have mixed feelings about TDD. While I believe in testing I have a issues with the idea of the test driving my development effort.</p>

<p>When you code to satisfy some tests written for an interface for requirements you have right now, you might shift your focus from building maintainable code, from clean design and from sound architecture.</p>

<p>I have a problem with driven not with testing. Any thoughts?</p>

[{'answer_id': 80274, 'author': 'pjz', 'author_id': 8002, 'author_profile': 'https://Stackoverflow.com/users/8002', 'pm_score': 1, 'selected': False, 'text': '<p>It\'s always a balance:<br>\n - too much TDD and you end up with code that works, but is a pain to work on.<br>\n - too much \'maintable code, clean design, and sound architecture\' and you end up with <a href="http://www.joelonsoftware.com/articles/fog0000000018.html" rel="nofollow noreferrer">Architecture Astronauts</a> that have talked themselves into coding paralysis</p>\n\n<p>Moderation in all things.</p>\n'}, {'answer_id': 80311, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 5, 'selected': False, 'text': '<p>No.</p>\n\n<p>If done right, Test Driven Development IS your design tool.</p>\n\n<p>I hope you forgive me for linking to <a href="http://jonlimjap.net/2016/09/01/when-tdd-goes-red/" rel="nofollow noreferrer">my own blog entry, wherein I discuss the pitfalls of Test Driven Development that went wrong</a> simply because developers treated their tests as, merely, tests.</p>\n\n<p>In a previous project, devs used a highly damaging singleton pattern that enforced dependencies throughout the project, which just broke the whole thing when requirements were changed:</p>\n\n<blockquote>\n  <p>TDD was treated as a task, when it\n  should have been treated as an an\n  approach. [...]</p>\n  \n  <p>There was a failure to recognize\n  that TDD is not about tests, it’s\n  about design. The rampant case of\n  singleton abuse in the unit tests made\n  this obvious: instead of the test\n  writers thinking “WTF are these\n  singleton = value; statements doing in\n  my tests?”, the test writers just\n  propagated the singleton into the\n  tests. 330 times.</p>\n  \n  <p>The unfortunate consequence is that\n  the build server-enforced testing was\n  made to pass, whatever it took. </p>\n</blockquote>\n\n<p>Test Driven Development, done right, should make developers highly aware of design pitfalls like tight coupling, violations of DRY (don\'t repeat yourself), violations of SRP (Single Responsibility Principle), etc.</p>\n\n<p>If you write passing code for your tests for the sake of passing your tests, you have already failed: you should treat <em>hard to write</em> tests as signposts that make you ask: why is this done this way? Why can\'t I test this code without depending on some other code? Why can\'t I reuse this code? Why is this code breaking when used by itself? </p>\n\n<p>Besides if your design is <em>truly</em> clean, and your code <em>truly</em> maintainable why is it not trivial to write a test for it?</p>\n'}, {'answer_id': 80391, 'author': 'Tom Carr', 'author_id': 14954, 'author_profile': 'https://Stackoverflow.com/users/14954', 'pm_score': 2, 'selected': False, 'text': "<p>There are three steps to complete software:</p>\n\n<ol>\n<li>Make it work </li>\n<li>Make it right </li>\n<li>Make it fast</li>\n</ol>\n\n<p>Tests get you #1.  Your code is not done just because the tests have passed.  Preferably you have some concept of project structure (Utilities, commonly accessed objects, layers, framework) before you start writing your tests/code.  After you've written your code to make the tests pass, you need to re-evaluate it to see which parts can be refactored out to the different aspects of your application.  Yuo can do this confidently, because you know that as long as your tests are still passing, you code is still functional (or at least meeting the requirements).</p>\n\n<p>At the start of a project, give thought to the structure.  As the project goes on continue to evaluate and re-evaluate your code to keep the design in place or change the design if it stops making sense.  All of these items must be taken into account when you estimate, or you will end up with spagetti code, TDD or not.</p>\n"}, {'answer_id': 80406, 'author': 'user15074', 'author_id': 15074, 'author_profile': 'https://Stackoverflow.com/users/15074', 'pm_score': 2, 'selected': False, 'text': '<p>I completely agree with pjz. There is no one right way to design software. If you take TDD to an extreme, without any forethought except the next unit test, you may make things harder on yourself. Ditto for the person who sets out on a grand software project by spending months on diagrams and documentation, but no code.</p>\n\n<p><strong>Moderate.</strong> If feel the urge to draw up a quick diagram that helps you visualize the structure of your code, go for it. If you need two pages, it might be time to start writing some code. And if you want to do that before you write your tests, so what. The goal is working, quality software, not absolute conformity to any particular software development doctrine. Do what works for you and your team. Find areas where improvements can be made. Iterate.</p>\n'}, {'answer_id': 80495, 'author': 'Aaron', 'author_id': 14153, 'author_profile': 'https://Stackoverflow.com/users/14153', 'pm_score': 1, 'selected': False, 'text': "<p>I'm relatively new to TDD and unit testing, but in the two side projects I've used it on, I've found it to be a design <em>aide</em> rather than alternative to design.  The abilty to test and verify components / sub-components independently has made it easier for me to make rapid changes and try out new design ideas.</p>\n\n<p>The difference I've experienced with TDD is reliability.  The process of working out component interfacing on smaller levels of component at the begining of the design process, rather than later, is that I've got components I can trust will work <em>earlier</em>, so I can stop worrying about the little pieces and instead get to work on the tough problems.  </p>\n\n<p>And when I inevitably need to come back and maintain the little pieces, I can spend <em>less</em> time doing so, so I can get back to the work I want to be doing.</p>\n"}, {'answer_id': 80529, 'author': 'user15122', 'author_id': 15122, 'author_profile': 'https://Stackoverflow.com/users/15122', 'pm_score': 1, 'selected': False, 'text': "<p>For the most part I agree that TDD does provide a sort of design tool.  The most important part of that to me is the way that it builds in the ability to make more changes (you know, when you have that flash of insight moment where you can add functionality by deleting code) with greatly reduced risk.</p>\n\n<p>That said, some of the more algorithmic work I've contracted on lately has suffered a bit under TDD without a careful balance of design thought.  The statement above about safer refactoring was still a great benefit, but for some algorithms TDD is (although still useful) not sufficient to get you to an ideal solution.  Take sorting as a simple example.  TDD could easily lead you to a suboptimal (N^2) algorithm (and scads of passing tests that allow you to refactor to a quick sort) like a bubble sort.  TDD is a tool, a very good tool, but like many things needs to be used appropriately for the context of the problem being solved.</p>\n"}, {'answer_id': 80666, 'author': 'Bjorn Reppen', 'author_id': 1324220, 'author_profile': 'https://Stackoverflow.com/users/1324220', 'pm_score': 2, 'selected': False, 'text': '<p>I completely agree with you on that subject.  In <em>practice</em> I think TDD often has some very negative effects on the code base  (crappy design, procedural code, no encapsulation, production code littered with test code, interfaces everywhere, hard to refactor production code because everything is tightly coupled to many tests etc.).</p>\n\n<p><a href="http://en.wikipedia.org/wiki/Jim_Coplien" rel="nofollow noreferrer">Jim Coplien</a> has given talks on exactly this topic for a while now:</p>\n\n<blockquote>\n  <p>Recent studies (Siniaalto and\n  Abrahamsson) of TDD show that it may\n  have no benefits over traditional\n  test-last development and that in some\n  cases has deteriorated the code and\n  that it has other alarming (their\n  word) effects. The one that worries me\n  the most is that it deteriorates the\n  architecture. \n  --<a href="http://www.artima.com/weblogs/viewpost.jsp?thread=6771" rel="nofollow noreferrer">Jim\'s blog</a></p>\n</blockquote>\n\n<p>There is also a <a href="http://www.infoq.com/interviews/coplien-martin-tdd" rel="nofollow noreferrer">discussion over on InfoQ</a> between Robert C. Martin and James Coplien where they touch on this subject.</p>\n'}, {'answer_id': 80758, 'author': 'Peter Evjan', 'author_id': 3397, 'author_profile': 'https://Stackoverflow.com/users/3397', 'pm_score': 4, 'selected': False, 'text': '<p>There\'s always a risk of overdoing either the TDD design or the upfront design. So the answer is that it depends. I prefer starting with a user story/acceptance test which is the base of the requirement that my tests will aid in producing. Only after I\'ve established that, I start writing detailed unit tests TDD-style. If the only design and thinking you do is through TDD, then you risk too much of a bottom up approach, which might give you units and classes that are excellent in isolation, but when you try to integrate them into the user story fulfilling task you might be surprised by having done it all wrong. For more inspiration on this, look att <a href="http://blog.daveastels.com/files/BDD_Intro.pdf" rel="noreferrer">BDD</a>.</p>\n\n<p><a href="http://www.infoq.com/interviews/coplien-martin-tdd" rel="noreferrer">A great "debate" about this has been recorded</a> between Robert C. Martin and James Coplien, where the former is a TDD advocate and the latter has stated that it ruins the design of a system. This is what Robert said about TDD and design:</p>\n\n<blockquote>\n  <p>"There has been a feeling in the Agile\n  community since about \'99 that\n  architecture is irrelevant, we don\'t\n  need to do architecture, all we need\n  to do is write a lots of tests and do\n  lots of stories and do quick\n  iterations and the code will assemble\n  itself magically, and this has always\n  been horse shit. I even think most of\n  the original Agile proponents would\n  agree that was a silliness."</p>\n</blockquote>\n\n<p>James Coplien states that merely driving your design from TDD has a great risk:</p>\n\n<blockquote>\n  <p>"One of the things we see a lot, in a\n  lot of projects, is that projects go\n  south on about their 3rd sprint and\n  they crash and burn because they\n  cannot go any further, because they\n  have cornered themselves\n  architecturally. And you can\'t\n  refactor your way out of this because\n  the refactoring has to be across class\n  categories, across class hierarchies,\n  and you no longer can have any\n  assurances about having the same\n  functionality."</p>\n</blockquote>\n\n<p>Also he gives a great example of how a bank account probably would look if you test drove it as compared to using your upfront knowledge to drive the architecture:</p>\n\n<blockquote>\n  <p>"I remember when I was talking with\n  Kent once, about in the early days\n  when he was proposing TDD, and this\n  was in the sense of YAGNI and doing\n  the simplest thing that could possibly\n  work, and he says: \'Ok. Let\'s make a\n  bank account, a savings account.\'\n  What\'s a savings account? It\'s a\n  number and you can add to the number\n  and you can subtract from the number.\n  So what a saving account is, is a\n  calculator. Let\'s make a calculator,\n  and we can show that you can add to\n  the balance and subtract from the\n  balance. That\'s the simplest thing\n  that could possibly work, everything\n  else is an evolution of that.</p>\n  \n  <p>If you do a real banking system, a\n  savings account is not even an object\n  and you are not going to refactor your\n  way to the right architecture from\n  that one. What a savings account is,\n  is a process that does an iteration\n  over an audit trail of database\n  transactions, of deposits and interest\n  gatherings and other shifts of the\n  money. It\'s not like the savings\n  account is some money sitting on the\n  shelf on a bank somewhere, even though\n  that is the user perspective, and\n  you\'ve just got to know that there are\n  these relatively intricate structures\n  in the foundations of a banking system\n  to support the tax people and the\n  actuaries and all these other folks,\n  that you can\'t get to in an\n  incremental way. Well, you can,\n  because of course the banking industry\n  has come to this after 40 years. You\n  want to give yourself 40 years? It\'s\n  not agile."</p>\n</blockquote>\n\n<p>The interesting thing here is that both the TDD proponent and the TDD antagonist are saying that you need design up front. </p>\n\n<p>If you have the time, watch the video. It\'s a great discussion between two highly influential experts, and it\'s only 22 minutes long.</p>\n'}, {'answer_id': 80829, 'author': 'Robert Gould', 'author_id': 15124, 'author_profile': 'https://Stackoverflow.com/users/15124', 'pm_score': 2, 'selected': False, 'text': "<p>My way to think about it is, write what you want your code to look like first.\nOnce you have a sample of your target code (that right now does nothing) see if you can place a test scaffolding onto it. \nIf you can't do that, figure out why you can't. \nMost of the time it's because you made a poor design decision (99%), however if that's not the case (1%) try the following:</p>\n\n<ul>\n<li>determine what the crazy requirements are that you need to abide to that wont let you test your code. One you understand the issues redesign your API.</li>\n<li>if someone else decided this requirements discuss about it him/her. They probably had a good reason for the requirement and once you know their reason you'll be able to perfect your design and make it testable. If not now you can both rework the requirement and you'll both be the better for it.</li>\n</ul>\n\n<p>After you have your target code and the test scaffolding. Implement the code. Now you even have the advantage of knowing how well your progressing as you pass your own test (Its a great motivator!)</p>\n\n<p>The only case where testing may be superfluous, from personal experience, is when you are making an early prototype because at that point you still don't understand the problem well enough to design or test your code accurately.</p>\n"}, {'answer_id': 35225587, 'author': 'Cope', 'author_id': 2464041, 'author_profile': 'https://Stackoverflow.com/users/2464041', 'pm_score': 2, 'selected': False, 'text': '<p>There are many informal opinions here, including the popular opinion (from Jon Limjap) that bad results come from doing it wrong, and claims that seem unsupported by little more than personal experience. The preponderance empirical evidence and published results point in an opposite direction from that experience.</p>\n\n<p>The <em>theory</em> is that a method that requires you to write tests before the code will lead to thinking about design at the level of individual code fragments — i.e., programming-in-the-small. Since procedures are all you can test (you still test an object one method at a time, and you simply can\'t test classes in most languages), your design focus goes to the individual methods and how they compose. That leads, in theory, to a bottom-up procedural design and, in turn, to bad coupling and cohesion among objects.</p>\n\n<p>The broad empirical data substantiate the theory. Siniaalto and Abrahamsson, (<em>Comparative Case Study on the Effect of Test-Driven Development on Program Design and Test Coverage</em>), ESEM 2007, found that "Our\xa0results indicate that the cohesion may be worse (even though Beck claims that TDD produces highly cohesive systems).\xa0In our second study we noticed that the complexity measures were better with TDD, but the dependency management metrics were clearly worse." Janzen and Saledian (<em>Does Test-Driven Development Really Improve Software Design Quality?</em> IEEE Software 25(2), March/April 2008, pp. 77 - 84) found that “[T]he results didn\'t support claims for lower coupling and increased cohesion with TDD”.</p>\n\n<p>A literature review will uncover other publications furthering these cases.</p>\n\n<p>Even my dear friend Uncle Bob writes: "One of the more insidious and persistent myths of agile development is that up-front architecture and design are bad; that you should never spend time up front making architectural decisions. That instead you should evolve your architecture and design from nothing, one test-case at a time. Pardon me, but that’s Horse Shit." ("The Scatology of Agile Architecture," \n<a href="http://blog.objectmentor.com/articles/2009/04/25/the-scatology-of-agile-architecture" rel="nofollow">http://blog.objectmentor.com/articles/2009/04/25/the-scatology-of-agile-architecture</a>)</p>\n\n<p><strong>However</strong>, it\'s worth noting that the broader failure is that people think it\'s a testing technique rather than a design technique. Osherov points out a host of approaches that are often casually equated with TDD. I can\'t be sure what\'s meant by the posters here. See: <a href="http://weblogs.asp.net/rosherove/archive/2007/10/08/the-various-meanings-of-tdd.aspx" rel="nofollow">http://weblogs.asp.net/rosherove/archive/2007/10/08/the-various-meanings-of-tdd.aspx</a>.</p>\n'}]
['https://Stackoverflow.com/questions/80243', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13615/']