This post was previously on the Pathfinder Software site. Pathfinder Software changed its name to Orthogonal in 2016. Read more.
Many developers I talk to say they have tried Test Driven Development (TDD), but it “didn’t work for them”. Why is this so often the case? I believe that the main reasons are 1) it wasn’t done correctly 2) the effort was too quickly abandoned 3) the benefits were not obvious to the developer.
1) It wasn’t done correctly.
Doing TDD the right way is beyond the scope of this post. The best ways to learn are pairing with a TDD expert and reading some of the good books on the subject. I really like The RSpec Book and Test Driven.
2) Abandoning the effort too quickly.
At first, development time will increase until proficiency in TDD is obtained. This improvement ravine is very well documented by Martin Fowler.
3) TDD benefits
Though the TDD benefits at the micro-level are significant, the macro-benefits are outstanding! Preventing or fixing bugs early provides a huge cost benefit to the customer. The cost of fixing design or coding bugs exponentially increases with time from bug authoring.
Some argue that the same benefits can be achieved with Test After Development (TAD). In my experience, TDD is much better than TAD for the following reasons:
- Keeps you focused. At any time during the day, you should either be writing a test for a feature or writing code to make that test pass.
- Less code. TDD promotes Do The Simplest Thing That Could Possibly Work (DTSTTCPW). Let’s write code that gets the product delivered instead of frameworks that may or may not ever get used again!
- Necessarily promotes testable code
- FASTER. It’s a lot faster getting it right the first the time! It’s much faster fixing bugs as soon as they are authored
- Testing the feature before it’s implemented tests the intent of the code. When TADing, the intent could have been forgotten
- Refactor with confidence. Behavior driven tests ‘protect’ the required features, which means that refactorings should cause test failures
- Thinking of and designing for edge cases up front promotes a better and more stable design