Hey Folks! It’s a new site. If you see bad links or odd behavior, please contact me right away. Thanks! – GeePaw
 Home  Home  hom  Meet with GeePaw  Meet with GeePaw  mee  Work with GeePaw  Work with GeePaw  wor  Who's GeePaw  Who's GeePaw  who  Blog  Blog  blo  Contact  Contact  con

TDD Pro-Tip: Graceful and Awkward

 26. December 2018

TDD Pro-Tip: The first step past beginner-TDD lies in grasping the difference between awkward and graceful collaborations, and learning the myriad contextualized patterns for moving from the former to the latter.

folks invest a little in TDD, maybe cuz they’re excited, maybe cuz they’re ordered. they read a little. they try an exercise or two. they learn a little about their xUnit options. maybe they boldly even use TDD to write a method or a class in their day job. all good stuff – except the dumb-assed ordering of people to do a complex activity without adequate training or support, but that’s a topic for another day – and all, well, beginner stuff.

there’s a lot of text out there about that stuff: tons of exercises, kilo-tons of opinion, some good videos and some bad ones, and so on. but there’s not a whole lot out there for folks who want to move past beginner-dom.

so here we go.

testing a collaboration is never free. sometimes, though, it’s dead-cheap. other times, it’s wildly expensive. and mostly, it’s somewhere in between, neither a no-brainer nor quite rocket-science, but something else.

“awkward” and “graceful” are my words for describing not so much the endpoints of this expense-spectrum for testing collaborations, as for the relative positions in between.

a pre-understanding: we’re speaking here of satisfying geek concerns: “does this code do what i wanted it to do?” being the primary one. there are other places s/w dev can go wrong, but they’re out of scope today. we’re specifically limiting ourselves to that question.

a second pre-understanding: for any fixed stable requirement, there are an infinite number of turing tapes – programs – that could deliver it. that is, there’s not only more than one way to skin a cat, there are a vast number of ways to do it. (who coined this expression?)

so we’re talking here about various rearrangements of the text of a program fragment and what impact they have on our ability to satisfy us that it does what its author wanted it to do. “awkward” means that a given arrangement is closer to the expensive end, and “graceful” means that arrangement is closer to the cheap end. a TDD’er given two such arrangements, if she can see that one is more graceful than the other, will prefer the graceful one.

let’s concretize that a little, eh? consider a java program that takes a string on the command line and interprets it as a file name, opens the file, reads its lines, sorts them, and prints them out.

the java.io.File class is a fairly standard starting point. grab that filename, stick it in a File, open that File, and read bytes. do the sort. the System.println(…) method sits at the other end, and print out the strings when we’re done sorting them.

it would be a simple matter to arrange this program as a single method with just such a sequence of instructions in it. (there’s even a sort(…) we can use in the middle.) fifty lines or so, and bob’s yer uncle – i’ll spare u the origins of that phrase today – what’s next?

a TDD’er though doesn’t just want “done”. that’s a noob thing. olbs know that there really is very little “done” in software: it lives in a state of perpetual change until the day it stops making its org enough money to stop supporting it.

a TDD’er wants that code to be change-enabled, and believes that microtesting the logic of it is a critical part of that change-enablement. so she’s not gonna arrange it as a single long method, she’s going to break it into pieces.

why? because in that arrangement it is quite awkward to test her logic. basically, she will be forced to use the command line to find out what she wants, and to run the entire program, and to supply it a physical file, and to somehow capture its output, and so on. and if she wants to be sure she’s done what she thinks she has, she’ll want repeatable tests for a variety of situations – empty files, empty lines, duplicate lines, no trailing delimiter – and so on.

so she rearranges. java.io.File must be resolved to a physical file on a drive. the console represented on the other end is slightly better, but still represents difficulties. she sees them both as “awkward”. the truth is, they make her want to not test, because it’s tedious, it takes time and effort to set up, extra-source data that has to be managed, and so on.

if she knows herr java, tho, the rearrangement is easy: split the method into pieces. “makeReader(…)” takes the cmdline argument and turns it into a java.io.Reader. “readAndSort(…)” takes a Reader and returns the sorted lines. “writeOutput(…)” prints them.

this arrangement leaves us with two tiny pieces, makeReader and writeOutput, and one larger piece, read and sort. the main program is just the three calls. it works exactly the same. it’s just a re-arrangement. so why do it?

we do it because that larger piece in the middle, readAndSort(…), can now be passed a Reader, and Readers are easy to set up and pre-load in java, far easier than Files.

they’re so easy that we can do it inline, in JUnit. each test method basically takes a long java string, runs it trhough readAndSort(…), and asserts on the size and ordering of its result. No files. no clever i/o trapping. easy-peasy.

look at that.

that is one pitiful example, isn’t it? i mean, c’mon, it’s a trivial problem, the after’s only slightly easier/smaller/faster than the before, and its miniscule compared to the code we really write for a living.

is that the dumbest example you’ve ever seen of a technique?

ummmm, yeah, pretty much.

but even in this primitive case, we see the beginnings of a far richer conception of TDD than most of the beginner exercises and frankly at least 34 of the internet verbiage about TDD. let’s enumerate what we’re seeing.

1) we see that “awkward” and “graceful” interactions are respectively more and less expensive to test.

2) we see that straightforward largely typographical rearrangements of working code can alter these values from more awkward to more graceful. (and vice-versa, one supposes.)

3) we see that we can readily go from having one file of shipping code and X files of tests, to just two files, one of shipping code, one of testing code.

4) we see the single most common pattern for dealing with awkwardness: splitting methods so that awkward parts are small and boring and graceful parts are larger and interesting.

5) and off in the distance, we can imagine the outline of the second most common pattern for dealing with awkwardness: pushing it to the rim of our code. we’ve used “awkward bookends”, wrapped around graceful guts.

when you get past beginner-TDD, learning the testkit, learning to take tiny steps, learning to to use tests to push desired code-changes, the next area of study you will find is about the awkward-graceful spectrum, and the many patterns we have for moving code around on it.

if tests were free, we’d all have perfect tests all the time. no one serious doubts the absolute value of having such tests laying around, what they doubt is the relative value – cost vs. benefit.

there’s nothing we can do to make tests free. but we can make them cheaper.

we can even very often make tests cheaper cheaply.

all it takes to start is to recognize what makes some code awkward to test and other code graceful. all it takes to finish is – well – practice practice practice.

when you’re ready to get serious about using TDD in your day job, start here: what is graceful, what is awkward? it leads on for a while: can i move between them? can i learn to anticipate when my code is going to be awkward and resist it? can i take old code and “de-awkward” it?