i often say “how am i gonna test this?” but – language being what it is – my meaning in asking that is not likely graspable by a noob. to get at what i’m really wondering when i ask this, i may have to take a few asides & detours.
first. why am i writing a test at all? what is the test doing for me?
this touches a mantra you’ll hear me mutter over and over. “i don’t work for you. you work for me.” i tell the code this, and the ‘puter, and the “engineering process”, all the parts of a development system.
the only reason i use a tool is because it is helping me (or when i’m mistaken or guessing). when it isn’t helping me i don’t use it. so what am i getting from these tests i write? what are they doing for me that earns their keep?
several things. but i can label them all with the sort of general label: “closing the mental shoebox”. this launches another tangent..
the code i work on is big. the mind i work with is tiny. in order to do the work at all, i have to bring these two scales into contact. i’m not an essentialist, but just the same, hear me: the essence of programming well is the management of mental scope. i absolutely have to arrange things so that my tiny mind can think about my enormous code through tiny temporary windows onto it.
i think of these as tiny project shoeboxes. i put a label on the box. and i put just the parts of the code i need for that label. in OO, the class is the shoebox. the name of the class is the label. the class does Just One Thing[tm]. i take out a shoebox, mess with it, then put it back on the rack and take another. that’s controlling mental scope.
the shoeboxes are hierarchical, too, of course. i have boxes that only contains a little bit of code and a bunch of other shoebox labels. in some apps, of course, those hierarchies are quite tall. arranging the shoeboxes so it works is non-trivial. we call that “architecture”. but there’s no clear line between architecture, design, and the code. it’s just a spectrum from closest-to-metal up to furthest-from-metal. and i go to great lengths – tremendous lengths – to keep those boxes small, simple, and arranged so their contents are at the same level.
so. sliding back from that tangent. the things the tests do for me are all ways to secure the lid on the shoebox. when the lid is closed, i don’t have to think about what’s inside it. i just have to remember the label & the Just One Thing[tm] it invokes.
the first thing those tests do for me: they assure me that the box does what the label says it does. that lets me, when i’m in another box that uses this box, not worry that the box does what i said it did. huge benefit mental-scope-wise. that’s one reason the tests make me a more-features-faster geek.
it will probably surprise you to realize this, but i refuse to let you slumber on in dreamland. you must accept this: i’m not perfect. that means that sometimes i make boxes, close them with tests, walk whistling away from them, only to later discover i was wrong. clues in the larger world force me to open a shoebox i thot was closed, one i haven’t visited in months.
and here’s a second way the tests are working for me. the tests tell me quickly how the stuff in that box was spozed to work. i can scan a list of tests and see right away whether i thot i covered the case in question. if i didn’t, i do so now. if the new test passes as is, well, hell, must not have been that. the joy is when the new test fails. with my little failer happily in hand, i can make it green. when i’ve done that, i can put the lid back on and go back to what i was doing.
btw, not only do i make the new one green, i keep all the old ones green, too, and that’s the third way the test works for me. those old tests keep me from fail-toggling, where you fix thing 1 and in so doing break thing 2. then fix thing 2 and in so doing break 1. the tests never let me do that without telling me about it.
there are handful of lesser things those tests do for me, too. they give me exempla for usage of the box, for instance. and i don’t work solo, so instructions for usage are important to more than just me.
the tests themselves are actually tiny little shoeboxes, too. that is, one test = one thing to make work. that’s a very tiny focus.
so. when i say, “how am i gonna test this?” i mean, “how am i gonna close this shoebox?”
and there’s a part of this that is central, but not obvious so far: i ask that question before i have any code. in fact, i usually start asking it as soon as i start conceiving of the box. the box has API. i build boxes by building API’s i want.
and among the very earliest questions i ponder while i’m doing this: how’m’i gonna test this? (that’s not the only early question by any stretch, but those are for another muse.)
anyway. for me, programming is TDD and TDD is shoebox-closing and shoebox-closing is managing mental scope.
if you want more features faster, this is the way i know how to get that from myself, and it might help you. thanks for nodding along.