TDD. The most common meaning of this abbreviation is probably Test Driven Development or Test Driven Design. Simply put, it’s all about writing test code before writing production code. Why would one like to do that, you may ask? The answer is simple; your code will eventually become a lot better and easier to maintain. Not only for yourself, but for everyone that have to understand and work with your code. I won’t write a lot about pros and cons, or what it is (Check out the link in the final paragraph!). I’ll just list a few pointers to how to get started and how to get better. I myself can barely claim to perform TDD, but I strive to get there! So..I don’t claim to be an expert, I just want to do some TDD advertising
Unit tests
The first thing to do is to actually learn how to write unit tests, and how to do it right. When I first tried to do some work using TDD, after a while I figured out that my tests weren’t that good. I thought I knew what a unit test was and that I could jump right into it. Turned out that I had some things to learn
What’s a mock? Or a stub? What is the difference between a unit test and an integration test? What is meant by Inversion of Control / Dependency Injection? You’ll find answers to all of these questions by doing a little bit of Google magic. Still, I want to recommend The Art of Unit Testing by Roy Osherove. Though the examples are done using C#, everyone will make use of this book. This book will teach you a lot about unit testing!
Test first
A common problem with unit tests is that the production code is hard to test. There can be many reasons for this, and I guess the most common is dealing with dependencies. Though there are many factors that can affect the testability of code, a common divisor is that code that is hard to test is bad code. One of the advantages when writing the test on beforehand is that you are forced to write the code in a way that is testable! This can be quite difficult to get started with (at least I think it is/was). The only thing to do is practice. Of course, you should also read up a bit on the topic, but in the end I think it’s all about practice. And if not able to write the test up front, at least write it while you are working on the code . The book Test Driven Development: By Example by Kent Beck is highly recommended!
Kata
One way to practice TDD is to perform so called code Kata‘s. These are exercises that are meant to be done repeatedly. In addition to practicing TDD and test first thinking, you will become better at using your IDE. Some Kata’s that I know of are string calculator and bowling game.
Inspiration
There is a lot of good resources, books and blogs with content related to TDD. “Uncle Bob” has done a lot of amazing presentations. and I suggest checking out this one (if you’re in a hurry, jump to ~23:28). I bet it will motivate you to start doing test driven development!