By writing the tests first, you confront your new code with a real client of its API. This cuts way down on imaginitive design-ahead that later (when you finally try to use it) proves problematic.
I used to try to think three or four steps ahead when designing and implementing class hierarchies, before actually using the code. It's a fun head game, but more often than not I ended up with unused, untested artifacts that bit back months later. Now I limit implementation to one "story" at a time, and (largely) avoid having unused, untested code.
By having a body of unit tests to cover the work already implemented, the feedback loop for discovering that I've broken something "way over there" is very short. The code I've just implemented is still fresh in my head, and I can think through consequences without having to take hours to rebuild mental context.
By keeping the bug count down (ideally to zero) at each step of development, there's no chance for the bugs to breed while nobody is looking. The number of "cascading" bugs I find in my own code has gone down to effectively zero.
The other Extreme Programming practice I used with this is Refactoring. After each development iteration, I look at the code, and refactor where necessary (refactor classes, break long routines into smaller pieces where appropriate, etc.). (And rerun the unit tests!) Think of it as cleaning the kitchen after cooking. It's such a bummer to walk into the kitchen to start cooking, only to find dirty pots and pans that need to be cleaned first. I find that it's the same way with code.
Shifting my development habits to writing test cases first wasn't easy. The temptation to jump right in and start coding the "real" stuff was strong. If you go this route, expect there to be starting friction, and hang with it a while until you notice your debugging time dropping off.
In reply to Re: Early reflections on extreme programming
by dws
in thread Early reflections on extreme programming
by nop
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |