How I learned TDD


TLDR; checkout this blog post if you want to know how to make TDD you second nature, read below for my story:).

My TDD experience is that TDD is some kind of mystic programming technique that everybody heard of but nobody practiced. In my first job I didn't really have the opportunity to learn TDD (no one was using it or I didn't know if they were), so I later decided to learn it on my own. I started with a book by Kent Beck called Test Driven Development: By Example. I read it but for some reason I still couldn't get a grasp of it.

Then, when reading Architecture patterns with Python, there was a mention about katas and a link to Peter Provost's article about TDD katas. The link is dead now, but I ended up on this page. Knowing Uncle Bob's reputation, I decided to give the bowling kata a try.

I did the kata exactly how it is written in the article for a week or so. In the beginning it took me more than a half an hour, but later I think it was around 20 minutes to complete it all.

At that moment I couldn't use TDD in my job, because I was rarely writing the code from scratch. I changed teams, but it took about 2 months to do so.

What's interesting is that even after 2 months, I still was able to follow along with the TDD process.

Write a failing test for the simplest feature, make it pass, commit the code, refactor, check if it passes, commit, repeat.

What's really great about it is:

  • after every commit you have a working code, so in case of some emergency you can always stop and later go back to the code and just write a new test (I feel like the context switch is much smaller comparing it to the 'no-TDD' way); I guess you could even pass the development along to somebody else, and it would be easier for them to continue the work (but I haven't tried it yet)

  • refactoring is a part of the process, so after every step you have a refactored code; normally I tend to refactor the code as the last step, and sometimes this step, or part of it, is sacrificed for the sake of time. When I give it more thought, I guess it is also just harder to refactor the code that wasn't written in the TDD way.

I didn't write about what everybody says is the most important benefit of TDD - that it helps with the design of the code. I didn't notice it myself yet, but I also just tipped my toes in the TDD waters, so I guess I will have the 'aha!' moment later.