Ask HN: Help me see why everyone seems to love TDD?
You don&#x27;t sell unit tests to management. You don&#x27;t even <i>tell</i> management. The same way you don&#x27;t sell using reasonable variable names. You do it because (if!) it makes you and your team more productive. Coverage metrics are internal to the development team - they are not management metrics. Shipped features, open support tickets, velocity, story points, etc. are management metrics; cyclomatic complexity, test coverage, and length of variable names are developer metrics.<p>That said, there is a fairly large difference in experience and skill required to learn how to write variable names that make you more productive and how to write unit tests that make you more productive.<p>It all comes down to TCO of the system to users. In many cases, high test coverage does reduce TCO, but this is difficult to see during the first year of the project. And note too that the risk of <i>increasing</i> TCO due to poorly written unit test code is real. You can do more harm than good by creating flaky ad-hoc unit tests that are slow to run, difficult to change, and make refactoring of production code PITA.<p>The reason we do TDD, is that has some unique benefits. Especially, if you aim to use &quot;full TDD&quot; in the sense that <i>no line production code is written before there is a failing test for that line</i>.<p>What your life would be like if every time you interrupt a programmer in your team, everything she was working on compiled and passed all its tests less than two minutes ago? And if you&#x27;d have an always up-to-date spec that is so precisely written, it executes? And deployment to production would be a business decision instead of technical one as you&#x27;d be ready to deploy practically whenever you feel like it? And if you decided to swap MSSQL for PostgreSQL in your production system, you could do it without breaking a sweat?<p>What would your life be like if you did not have to fear breaking anything when cleaning up code? If you&#x27;d be able to keep your system maintainable and you&#x27;d know it?<p>If TDD is so great, why then, it is not used more often? I can only speculate, but I think this comes back to the question of developer skills. I have mentioned some of this before here at HN, but I&#x27;ll reiterate.<p>I have noticed that I have to make large refactorings to move things around to arrange the whole system so that each part can be tested without too much effort. To do this I have to view most things in terms of the interfaces they provide. On the test side, I have to write the test code so that the <i>what the test does</i> is strictly separated from <i>how the test does it</i>. This way changing the system causes only minor changes to ripple to the majority of the test code. This is by far the most common paint point of a novice TDD&#x27;er.<p>Based on this, it seems that programming with TDD is a distinct skill-set that requires significant effort to get reasonably good at, i.e., to be more productive than without TDD. I have given it a try on medium-size projects and it does pay off in terms of simplicity of the design (I have to manage dependencies and decouple external systems and components quite heavily), low defect rates in production&#x2F;QA, way less time spent in debugger, and relatively high velocity (this is notoriously hard to measure cross projects&#x2F;teams, but at least this was true based on customer and product owner feedback).<p>However, the problem with TDD is that all of the above (tests decoupled from interface, interface decoupled from implementation, system decoupled from external systems, components decoupled from each other, design skills to recognize this, and refactoring skills to do this fast enough to remain productive) need to be done well enough at the same time. Otherwise the approach falls into pieces at some point.<p>To paraphrase Uncle Bob from some years ago: &quot;I have only been doing TDD for five years, so I am fairly new to it...&quot; Half of the programmers have that much experience in programming in general, so the amount of time required to hone TDD and refactoring skills may not be there yet.<p>TDD&#x27;ing requires months or years of practice to get really productive with, and has a fairly large set of prerequisites that one has to know in order to remain sane. It took me several years of experimenting (especially with different techniques of writing unit tests) before I found a way to be productive with TDD. I also drew the connection between testability and program architecture (aggressive decoupling) fairly recently (some four years ago), and that was one of the last pieces of the puzzle that made everything work. The system structure is especially crucial for writing fast unit tests. You really want the dependencies to external systems (DB, UI, Network, MQ, OS, Library, Framework, or the like) injected and abstracted behind and an interface.<p>If your system&#x27;s design results in your unit tests depend on volatile components, your system becomes unnecessarily complex. This is because volatile components change often and these changes ripple to your units tests effectively rendering them volatile too. Avoiding this problem has been captured, among others, by the Stable Dependencies Principle (<a href="http:&#x2F;&#x2F;c2.com&#x2F;cgi&#x2F;wiki?StableDependenciesPrinciple" rel="nofollow">http:&#x2F;&#x2F;c2.com&#x2F;cgi&#x2F;wiki?StableDependenciesPrinciple</a>), which states that the dependencies should be in the direction of the stability. A related one is the Stable Abstractions Principle (<a href="http:&#x2F;&#x2F;c2.com&#x2F;cgi&#x2F;wiki?StableAbstractionsPrinciple" rel="nofollow">http:&#x2F;&#x2F;c2.com&#x2F;cgi&#x2F;wiki?StableAbstractionsPrinciple</a>), which states that components should be as abstract as they are stable.<p>When I started with TDD, my productivity plummeted initially, but the benefits were too good, and I slowly found the techniques needed to keep up with my old self in terms of produced features. I dread to think the pieces of code that send me deep down into debugging sessions due to non-existent test coverage.<p>Part of the problem is that there are not that many TDD codebases or TDD&#x27;ers around. Also, this is probably not something you can pick up while doing toy projects or school assignments. The benefits start to show in the 100 kloc and above magnitudes, and as there are so many ways to paint yourself into a corner with bad overall design, coupling, unmaintainable (or, my pet peeve, slow) tests, chances are, you don&#x27;t figure out all the necessary things yourself. On top of that, there is no time to learn this much in most dev jobs, so you are left to learn with hobby projects (which do not usually grow big enough).<p>Most TDD experiments result in failures for the reasons listed. This is why you read so many comments on TDD being useless, wrong, a religion, or Uncle Bob cult. However, it seems that people who keep practicing TDD have been programming for more years than people who have not tried it, or have abandoned the practice. I have yet to meet a TDD practitioner who started programming that way and has not considered any alternatives. The ideas have born out of really bad - serious - experiences with existing approaches.