In object-oriented programming, it is common to extract a superclass when two classes share some common behavior. There are other ways to structure the code, such as using composition and delegation, but for this post, I’ll use inheritance.
This is a pretty standard style of inheritance hierarchy, and we learn to write code like this very early on. However, there’s brittleness here, even though it may not be that obvious.
A new feature request comes in that requires another subclass in this hierarchy:
Aaaaannd, I just introduced a bug. Did you see it?
All of the
Guide subclasses are supposed to know the answer to life,
the universe, and everything. Why doesn’t
The answer, of course, is that I forgot to send
super in the new
configure method. This is an easy mistake to make, but of course
there are tests to catch things like this, right? Right?
I could leave things in this state, beat myself up for making this simple mistake yet again, and move on to the next feature.
There is another way. I will apply some Poka-yoke. Poka-yoke is a
Japanese term meaning “mistake-proofing”, and it is well-known in lean
manufacturing. It’s a good technique to apply any time we find
ourselves making the same little mistakes over and over again, like
forgetting to send
This fixes the bug and makes it much harder for my teammates and I to introduce a similar bug later.
In Practical Object-Oriented Design in Ruby: An Agile Primer, Sandi Metz calls this a hook method, and distinguishes the technique from Template Method, which she also discusses. She also raises a very interesting point:
When a subclass sends
superit’s effectively declaring that it knows the algorithm; it depends on this knowledge.
I hadn’t thought of it this way before, but it’s a fascinating thought.
Whatever you call it, it’s a handy way to make code a little less brittle.
I wrote the original draft of this post several days ago. Just before I was going to post it, Sandi Metz tweeted a link to another article that makes this same point, but in a broader context. It’s an interesting read.