here’s an illustration of the most basic principle of TDD, what i call the “Driven Premise”: there once was a queen who had a thousand consorts, each more brave and handsome than the next. but only if you lined them up that way.
the driven premise says that tests & testability are first-class participants in design.
when they are, then producing rock-solid code that does exactly what the author wanted it to do is easy. the result is a kind of soaring confidence, an ability to add new function to an existing system very nearly at the speed of thought.
further, those tests serve as a source of confidence & authority not just for the original author, but for all comers. and if you’re a hard-TDD’er, which i am, you’ll go even further: making those tests make yours design better even w/o the tests.
so working in this way, following the driven premise with vigor, produces great results. but. only if you line them up that way.
tests written for code where tests & testability are not first-class design citizens just don’t work as well. the expense of creating those tests starts to mount as the tests gain complexity, which they inevitably do because we didn’t line things up. the more expensive the test, the less likely i am to write it or run it, the less likely you are to read it or even parse it.
TDD works because we line the code up in a highly testable way. if doesn’t work when we don’t line it up that way.
so if you’re plunging in to TDD, the first thing you have to do is learn to change your design to account for tests & testability. what you are doing is lining up your system so that each element is more brave and handsome than the last.