Slowing Succumbing to TDD

You always hear things about great athletes just doing the right thing. Golfers couldn’t really tell you how they manage to sync up their entire body in order to slap a ball down the fairway. They’ve just practiced like crazy and let their body do the execution. This kind of unknown benefit is something I’ve seen others bring up regarding TDD. At this point, I’m apt to believe them.

Writing tests make you feel good. Tests help improve your confidence because there is something to point to that says, “see, this works”.

What is interesting though is that the more you test, the more you depend on your tests. It no longer a side story for the code, it is what makes the code valid. I made this transition recently and if I hadn’t noticed it on a personal level and acknowledged it, I’d continue to have a slim argument for TDD.

One facet that is key to TDD is that you write tests first. Up until this point, my assumption was that it was due to the benefits of taking the time. You preload your app with a client library that you need to make work. You have to think about the design from the user’s perspective, which is a Good Thing. The thing is, once you get started writing more tests and you’ve reached that point where the tests are what validate the code, it becomes apparent it is easier to change the tests before changing the code.

Changing the tests means that you not only have something to fix instead of something to write. This is a subtle constraint that helps to improve focus. It also keeps you designing when you may very well be doing maintenance. Ironically, it is also a little faster. TDD must be really slow is a really popular argument, but I’m inclined to suggest it is wrong. It actually should make you much faster.

Writing code quickly requires complete ideas. If you know exactly what the library does and what you want to do with it along with a rough idea of the algorithm, code can really fly into the editor. Writing tests first almost forces complete ideas. Since it forces design as well, you often don’t need to rewrite or refactor as much code. With practice, the speed only increases.

I’m really glad that I’ve taken the time to find an interest in testing. It is a hassle, but like exercise, you start to need it. Also, like exercise, it really improves the entire experience. It is a lot of fun to write code really fast. You feel confident and a feeling of accomplishment. Sooner or later you’ll find yourself churning out tons of great code and people might wonder how you got to be such a great coder. When they ask you can have a real answer beyond some buzz words.