I just read Dan North's November 2003
Java Developer's Journal article (subscription required) on this subject. His article did a great job of clearly condensing all the different things I've read and lectures I've heard on the subject.
Think of the following interview question: What is the point of test driven development?
Well, Dan points out that it has nothing to do with testing. His answer to the question is "Its about seeing how little you actually need to do and how cleanly you can do it!"
The test serves as a machine that simulates the customer's acceptance of your solution. So the idea is to write a test first that checks to see if a requirement is met. You'll be writing this test code as though the program we're testing was already written. Obviously it won't even compile because it doesn't exist yet, so your next step is to write some code that lets the test compile (and nothing more!!!!). Now the code compiles but the test fails because you haven't written any code to pass the test yet. Next you write code that will pass the test in the simplest way possible. You'll likely hardcode a response initially. As you write more and more tests you'll refactor ruthlessly to whip the code into an acceptable shape.
You might ask, "Why go through all this trouble? I'd rather write the code to begin with and skip all this extra nonsense." Sure you can do it that way too, but by testing first you focus on delivering only what's absolutely necessary. Dan North points out that "In the large, this means that the system you develop does exactly what's necessary and no more. This in turn means that it is easy to modify to make it do more things in the future as they are driven out by more tests." In addition, as the collection of tests gets larger and larger, it can serve as a way of validating that we didn't break any existing code with our new mods. That makes us more confident as we develop. And that confidence lets us be bolder in our refactoring which lets to code become better over time. What I've found is that by doing test first development, I've come up with solutions that weren't obvious right off the bat.
So get off your duff and do some test driven development today! Or at least read more about it on the
Agile Alliance website.