time, finally, for the steering premise, from the five underplayed TDD premises.
the steering premise says “tests & testability help steer design & development”. what we’re saying here is that tests are first-class citizens in the mob of factors that shape our system, with a voice that counts, all the way through development.
think of the factors we take in to account when we make software. they range all over, from market considerations, to platform, from our geek skillset to our tech stack. we operate within this mob of factors. we decide what to write, when to write, how to write. and the factors contribute their voices all along. the steering premise says that tests & testability are as central and important as each of the others.
it might be rhetorical to class this with the underplayed. after all, the movement is test-DRIVEN-development. a thousand heartaches ago, when TDD first hit these mean streets, the most shocking thing about it was that we wrote a test *before* we wrote the code to pass it. it was often called tFd. (no, not *that* F: “first”. “test-FIRST-development”. get yer head outta the gutter.)
writing a test before you change the code to pass that test, this is the steering premise at its most micro most rigorous level. and when geeks like me tried to adhere to this most rigorous advice, we discovered several remarkable things.
you can’t write tests before code very many times without discovering that some tests are dead easy to write, and some tests are hard as hell. and you think it’s cuz you’re a noob and you’re full of vigor, so you keep at it. and you eventually make two discoveries or give up TDD.
- discovery #1: hard testing problems are patterned. that is, they resemble each other in detectable and describable ways. “the database needs loading.” “the threads have to context-switch.” “the pixels can’t be interrogated.” “the answer is stochastic.”
- discovery #2: hard testing problems can very often be turned into easy testing problems by rearranging where code lives and how it’s called.
and once we’ve made these discoveries, the steering premise goes from its micro scope to the macro. why? well. we’re not *stupid*. if we can rearrange things to make hard tests easy tests, why not arrange things more nearly towards easy in the first place?
a real, if stupid, example. spoze we get a filename, and we have to get a hundred comma-delimited values out of that file.by way of psuedo-code, i’ll show you the paragraphing comments the noob types in.
// make file from filename
// make stream from file
// parse stream
now if we put all this in one method. to test that method, we’re going to have to litter the arena with sample files. (normally, we stash all these in some /test/ hierarchy). the parse might need a dozen examples to satisfy us that it works, so that’s a dozen files. so we make all the files, and we write one, then we copy/paste/edit the others. and as the code develops we do more and more of this, and we cook up an ingenious naming scheme for them.
and because you can’t “de-duplicate” files, if we ever change the comma delimiting to tab delimiting — see “why developers hate everyone” — we go back and manually edit it all. the point is, this is *hard*. it’s not a cheap set of tests to scan/read/write/run/change/diagnose.
a slight rearrangement, the slightest, will greatly ease our pain. put the first two lines, whose effect is to go from filename to stream, in one method. put the third line in another method.
testing the first method is still tedious, cuz we have to have some files to test it on. but a) there are fewer cases, having to do with file-nature, not file-content. b) in real-life we’d prolly elide the test altogether, as it’s a sequential series of pure library calls.
testing the second method can now be done by passing in streams. streams are far easier to make and organize and edit and de-duplicate than files, because they can be made with one line of code and a string constant. (as i say, this case is dumb, the answer is easy, the rearrangement trivial. but this is real. i’ve been in shops all around the world that use files in tests when they need not do so.)
do you see what we did? we changed our *design* to make it *easier* to *test*. we steered.
“i’ll be damned,” you say, “so we did. we changed our design to make it easier to test. damnedest thing i ever saw.” well. you’re kinda impressionable. but the truth is, it *is* the damnedest thing, for a slew of reasons.
there are lots of these cases, where we can rearrange things to make testing cheap. they form the set of patterns that experienced TDD’ers carry around with them.some of the rearrangements are easy to learn, others harder. the answers don’t usually just drop in effortlessly, but require custom fit, so they’re true patterns.
they do nearly always fit a meta-pattern: “eliminate or mitigate awkward collaborations”. that’s a muse for another day.
our case here is low-level code. to take the steering premise to its full extent, tho, we need to grow both “low-level” and “code” in our vision.”low-level” suggests that this kind of thing only applies to, idunno, casting about for a phrase, “leaf nodes”: close to the bottom code chunks, as opposed to “big picture abstractions”. this would be mistaking one case for the range of cases.
a lot of folks layer their thinking — i don’t, and am generally opposed to too much of this, but i know what they mean — they think there’s “code”, “design”, “architecture”, in ascending levels of abstraction.
the more complete version of the steering premise aims at the *entire* pyramid of abstractions. it’s not just about the breakout of functions, it’s about functions, objects, layers, subsystems, programs, systems, and apps. that is, refactoring — that’s what we did in that simple code case — can be done at any level of the abstraction hierarchy. in fact, it’s often easier to make tests cheap well above the level of leaf nodes.
we grow our vision of steering vertically by saying that we steer *all* the levels, from highest to lowest, by taking tests & testability seriously as factors.
and what about “code”? does the steering premise only apply when we’re looking at structured UTF-8 text that’s to be executed by a computer? no. we can incorporate tests & testability as factors not just in the code, but all through the process of shipping more value faster. making tests & testability first-class factors let’s us move out from code-per-se to coding-per-se, and from there to shipping-per-se.
consider that TDD *enables* continuous integration (CI) and that in turn *enables* continuous deployment (CD), and that has huge impact on our customers and our market. i think of this as expanding steering horizontally. the steering premise reaches far beyond the scope of “arranging structured text to be exceuted by von neumann architecture devices”. it doesn’t just change how we see code, it changes how we see the whole activity-set.
the steering premise says we treat tests & testability as first-class citizens throughout the entire range of the software development game. it is at the very heart of TDD, and everything TDD’ers do depends on and draws from it.