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: Suspect Demeter Chains

 31. August 2018

TDD pro-tip #2: demeter chains are anti-microtestable.

what is a demeter chain? it’s code where we talk to a collaborator’s collaborator. u can look up the origins of the name in your copious free time, but in regular code, it looks like this:

x = a.b.c.d.e.f;

each one of those dots is a link in a demeter chain out of whatever the object is that contains that assignment. a is our direct collaborator. we might have been passed her, or given it in a constructor. we might have new’d her, tho that’s another tip in the making. so objects of type a themselves have collaborators of type b. and b’s have c’s, and so on. in a demeter chain, we walk down that line looking for some value we need. (demeter chains are instances of what we call the pyramid problem. there are other varieties of it, but that’s neither here nor there.)

all even mildly interesting classes have collaborators, and most of those collaborators are themselves mildly interesting. and so on and so forth. “it’s turtles all the way down.”

never mind the larger design questions, what it is the impact of a demeter chain on microtest testability?

well, it’s simple. microtests are all about taking control over the object. they completely control the input so they can know for sure the output and validate it. and they work cuz they’re cheap, fast, and plentiful. for me to microtest code that includes x = a.b.c.d.e.f, i have to control the value of x. that means i have to pass an a. but to make an a, i have to pass a b. and so on. now, to make my supposedly cheap fast test, i have to create 5 objects and hook them up correctly.

SHARE HOW THAT MAKES ME FEEL! (hint: it makes me feel like it’s too much trouble to test my object.)

if i don’t write that test, tho, the question of whether my code does exactly what i meant it to do is left open. things that are left open are things you have to bear in mind when you work near them. things you have to bear in mind are mental bandwidth. we’ve powerful evidence that, as embodied humans, our mental bandwidth is sorely limited.

so do u get the causal connections here? demeter chains lead to one of two choices: a) don’t test. b) test slowly and painfully. either choice we make makes us slower to change layered branching logic.

a lot slower.

so. what do we do? well. as always in programming, there are a variety of answers, ranging from cheap hacks to deep re-design. which one i use in a given situation is a judgment call based on a bunch of my specific contextual factors, like deadline pressure for instance.

the cheapest possible hack? in order to calculate x i need an object of type F, which i’m currently getting by chaining. what if i didn’t chain, but just made my calculation take an argument of type F?

essentially, i’m passing the problem from being my problem to being my caller’s problem. after all, my caller had to give me that A object. she’s got just as much info as i have for getting to the F. let her do it! that might not work so well in context – remember, there’s not one right way to get past demeter. there’s tons of them.

the right answer is just noticing the problem and doing something about it.

what’s another way? well. we need that A for other stuff in the very same calculation, so we’re getting an A no matter what. what if, instead of asking a friend of a friend of a friend of A, we just asked A? give objects of type A the api to get the value of f for us. our code looks like this:

x = a.f;

now to test that code i just have to have a nice rigged A to give the answer i want. that’s way cheaper.

now, if you do either of those things, or any of the other variety of changes that can kill off demeter, do you see what you did? you changed your design to make it more testable.

you exercised the steering premise: “Tests & testability are first-class participants in design.”

in the TDD world, anything that makes us not test the local branching logic or calculation we write is seen as a design problem. mastering TDD means a) learning to see the problems, c) learning the variety of ways each of those problems can be solved.