Introducing TDD: How I Would Do It
Posted by Davy Brion on September 7th, 2008
I recently asked you how you would introduce TDD to an audience that has little to no knowledge of, or experience with TDD. Oh, with a time limit of 10 minutes. This post is pretty much the outline of everything i hope to say in those 10 minutes.
What Is TDD
- It is an iterative and incremental way of working, on a coding level.
- It comes down to a series of little steps that we follow step by step, while never trying to do too much at once.
- We try to break down our coding activities in small, easy to manage pieces
-
When we need to add/modify behavior in code, we:
- write a small failing test (sometimes a couple of them, but never too many at once) that indicates the lacking of, or the incorrectness of that behavior
- write/modify the code to make the test pass, but we do that as simple as possible, perhaps even quick-n-dirty
- after the test passes, we clean up the code where necessary. While doing so, we focus on keeping the code clean and readable
- run the tests we just wrote again to make sure everything still works and we didn’t break anything
- run the entire test suite before committing our code to make sure we didn’t introduce bugs in other places
- We try to follow these rules for all of the code we write (not always possible though)
Why do we prefer this way of working?
- If we stick to the rules, we’ll always have clean code that works
- Our design (in general) is flexible, simple, and never more complex than it has to be
- It allows us to make changes in the code without fear and with confidence, no matter who wrote the code in the first place. After all, we have an extensive test suite which will alert us immediately when we make a mistake.
- We can minimize the time we waste debugging and trying to understand why something doesn’t work anymore.
- We won’t have any code that isn’t being used anywhere.
- We can increase the skill level and the effectiveness of every developer on the team.
How can you get started with TDD
- It takes some time before you can do TDD effectively. Don’t expect all of the benefits to come immediately.
- Read Kent Beck’s Test Driven Development By Example to get into the TDD mindstate
- Try it, and stick with it for a while
- Realize that you will need to learn some new tricks to write code that is easily testable. Read xUnit Test Patterns to improve your skills.
- After a while, you will not want to go back
What do you think? Anything i left out that i really should mention? Are there things i need to stress more or discuss differently? I still have a few days left to make changes, so let me know ![]()
September 7th, 2008 at 9:08 pm
[...] Introducing TDD: How I Would Do It (Davy Brion) [...]
September 8th, 2008 at 12:27 am
you should add a practical example to illustrate to the audience, that will drive home your point. The example should be short and precise.
September 8th, 2008 at 8:45 am
I dunno, keep in mind that i only have 10 minutes… i can’t really think of a good example to show in only 10 minutes, while still being able to say the things i’d really like to say. Also, with a short example you run the risk of having people think “oh sure, this works with trivial stuff, but what about ‘real’ code?”
September 8th, 2008 at 9:30 am
[...] Introducing TDD: How I Would Do It - Davy Brion is putting together a 10 minute pitch for Test Driven Development. Here is what he has so far, and he’s canvassing opinions for anything more that should be added or expressed differently [...]
September 8th, 2008 at 8:02 pm
I’d add to the point “Why we prefer working this way”:
- To make sure the tests actually get written.
September 8th, 2008 at 10:47 pm
hah, great point actually… totally forgot about that one