Avoid Implementation Inheritance: GeePaw Goes All Code-y

so, day off before travel before onsite, playing ONI, having fun, but i want to muse about a highly geeky matter anyway…

do you ever override in a sub-class a method that’s already implemented in a super-class? i want to recommend to you a policy of avoiding that like the plague, whenever you can.

i will go further: i avoid, maybe not as much as the plague, but surely as much as the measles, even deriving any sub-class off of any super that has any implementations at all. (anything not a “pure virtual base class” in c++, “interface” in java. in languages that have no such concept, any super-class that has an implementation other than “throw new ForgotToOverride”.)

i came to this philosophy very slowly over an embarrassingly large number of years. i say “embarrassingly” because, nearly as soon as i came to it, it made itself clear almost immediately how much better/stronger/faster/kinder my resulting designs are. i came up through the OO revolution, you see, and i’m a confident and capable OO geek, surrounded by this thing, i’ll call it “implementation inheritance”. it was the air i breathed.

when i started TDD, i had an enormous amount to learn just in first the basics and then the intermediates. it took some time. as it progressed, i learned about a kind of meta-pattern i’d been engaging in for years & years. it’s a hard one to see, this meta-pattern, and that’s what took me so damned long. it’s trading the major value of getting what i’ve elsewhere called “rigorous” results for a variety of lesser values that essentially amounted to local convenience.

or, maybe, that’s not quite right.

let’s try another take: i was trading to get a kind of local appearance of convenience, and in so doing giving up an actual *global* improvement in my ability to ship more value faster.

and the worst part of it was, i wasn’t even getting that much local feeling of convenience. just a little. and i was giving up a *lot* of global value-shipping speed. and this wasn’t just one feature of my coding technique, i was doing it all over the place in myriad ways. TDD slowly helped me find those ways and choke them down, one by one. i will never not think of it fondly for doing that. one of the things i was doing, tho, was being what i thought was a facile and even gifted — i get perky at times — object-oriented programmer. i used implementation inheritance heavily.

the benefit of implementation inheritance is dreadfully obvious: it’s the fastest possible way in a statically typed language to re-use code you or someone else already wrote. and if you’re good at it, i was, you could carry it around with you all over the place, like an overloaded saddle-bag draped around your neck, and never even notice it, except occasionally if you needed to feel good about yourself.

TDD — as always i mean by this “the modern software development synthesis” broke me of a lot of “good programming practice” habits that were not in fact good programming practice, but it took me a really long time to get past this one. i was already pretty much a TDD master before i finally set it aside.

why set implementation inheritance aside?

  1. you almost never need it. that is, anything you can do with it and a modern optimizing compiler you can do just as well without it. the sole exception i’ve found is in some obscure performance situations that, trust me, neither you nor i are likely to find outselves in.
  2. it complicates in a particular way: you see, implementation inheritance impedes quite sharply what i regard as the central skill or discipline of being able to ship more value faster, the management of mental scope. every implementation inheritance — every single one — creates a hole in the little conceptual box of “the code right in front of me”. and it does it implicitly, almost invisibly. it is a guaranteed violation of “locality of reference”, the ability to see what you need right when you need to see it.

how did TDD help me finally see this? TDD helped me to see that implementation inheritance was undesirable because it made me *think*, *more*, about stuff that wasn’t *there*. well. mama dint raise no thinkers. thinking more about stuff that’s not in front of me is exactly the opposite of managing mental scope. when i have to TDD implementation inherited classes, tho, that’s exactly what i have to do. and the thing is? half of that thinking has almost no relevance to the current value i’m trying to ship faster.

maybe you’ve never seen hardcore implementation inheritance. i see it all the time, particularly in older larger codebases dominated by a so-called “senior architect” who memorized the GOF book in ’95 and hasn’t shipped value in the last 20 years.

but if you’re wondering, here: this chart — part 1 of 3 — is illustrative. almost none of those boxes is an interface. at the bottom of that dependency tree there are leaf nodes that have upwards of seven or ten *implementing* super-classes. every one of them with dozens of methods, every one a partial override of its super.

we don’t ship more value faster by juggling more balls, but by juggling fewer. implementation inheritance always adds balls to what you have to juggle in your head while you’re working.

so let it go. if you’re having trouble seeing a new design without it, come find me, or any of dozens of serious TDD’ers out there. we *like* solving design problems. we do it the way people work crosswords in the sunday times. ask us, you’ll see.

Leave a Reply

Your email address will not be published. Required fields are marked *