I love memes.
They have this incredible ability to condense not just a message, but also culture, social context –and even– historical context.
They're powerful.
Picking the right meme is an art, and it depends even on the timing of the message; that's why this message must use the «Man Taking a Stand» meme (formally known as the Freedom of Speech meme)
Man Taking a Stand Meme declaring 'Hierarchy was a mistake'.
I picked this particular meme because class hierarchy is so deeply inception into programmers' minds that I feel this need to stand up.
My perception is that every programmer -whether formally educated or self-taught– has inherited (🥁🥁💥) this artificial need for:
class Dog extends Animal {}
I loathe you, fake, unrealistic, forced, unnatural, fabricated, yet harmful
Dog
class.
In Inception ( originally Paprika ) they explain how powerful an idea is.
the Dog
class represents this idea that "formal programming uses hierarchy because it leads to simpler and easier-to-understand code".
No, it does not.
Use composition It's Easier to understand
Beside textbooks, no one is using a Dog
class in the real world. Let’s look at a more realistic example, taken out of context, from an actual library:
class ExclusionBlend extends BlendModeFilter {}
class BlendModeFilter extends Filter {}
class Filter extends Shader
class Shader extends EventEmitter {}
abstract class EventEmitter {}
Now, I’m not sure how our minds work exactly, but there’s a limit to how much we can process and retain.
-> The number of logical structures we can keep in our short-term memory (like a mind-cache) is finite.
So, at some point, we forget:
"Where was the rendered
property declared?" "Was it in the Filter
class, or was it at the Shader
level?"
"Is the compileShaderFilter()
overriden for every Filter
, or do I need to use some specific property of my particular Blend
subclass?".
This is how i picture this: In abstract terms, the amount of this "short-term memory space" needed to visualize a class hierarchy is bigger than it should be, leading to conceptual overlaps and mental overhead.
Every item we add to the chain, we sum accumulated complexity to the mental model.
What about Composition? Won’t it require the same 'short-term memory' thing you mentioned?
🤔
No, I don’t think so.
Now –I’m going anthropology here– I think this relates to how real-life society works.
We live in a system modeled by composition, not hierarchy.
Sure, there’s a subtle hierarchy in life, like in DNA, but how does the day-to-day work?
-> My immediate world works through composition.
The other day at work, I needed to fill out some paperwork for HR.
I sent the request via email and received the procedure.
That request involved a different department than the one handling invoices.
So they forwarded my request to another department, where a different/specialized employee handled the details, solved the task, and sent the solution back to the operator who initially contacted me.
Every item we add to the chain does not accumulate complexity.
Later, I grabbed a coffee. There were two different baristas—one took my order, and the other prepared the drink.
What I didn’t encounter was some kind of Barista-Gestalt-Hive-Mind, an all-knowing employee where every barista "plugs in" each morning and inherits (🥁🥁💥) the ability to.....
The Barista-Gestalt-Hive-Mind 8-bit generated by Grok
You may find this metaphor ridiculous.
It is.
I’m trying to inception the idea that the human world runs on composition.
While hierarchy demands more mental space, a system built on composition requires less.
*Our minds naturally create "black box systems" in daily life.
We don’t need to know how every part works, just what it returns.
*
This is what makes it easier to keep all the pieces in our short-term memory. Our minds are perfectly fine with isolating components and simply expecting results—just like when we order a coffee.
Systems based on composition are also easier to unit test, but this is one of the longer articles I've published, and I like to keep this one abstract.
I WILL post a follow-up using pure JavaScript, pure Node, and/or pure React, defending the superiority of composition.
Stay tuned 📻
--
thanks for reading 💛
Top comments (7)
Interesting post, looking forward to how you approach the argument from a unit testing perspective!
Some people prefer to think in hierarchies. Code is primarily meant to convey the thoughts of the developers and only secondary to be run on hardware (otherwise we'd all write machine code), so naturally many languages present them with the tools to do in a way that feels natural to them. Coding is a way of storytelling.
That being said, you are right: hierarchical thinking means thinking in (encapsulated) boxes, which breeds unnecessary complexity.
A good story doesn't need to tell the reader that a dog is an animal. We want to take it for a walk, play fetch and go hiking.
Some people are neurodivergent (or maybe there is no such thing as neurotypical anymore)
This means OOP feels more natural for some, but for others it's FP or FRP.
Maybe someone else will come up with an entirely different paradigm one day that feels even more natural... to them.
I'm having fun with °streams oriented programming°. It makes a lot of sense to me 😎
Anyway, I agree with you. 😂
Inheritance and composition both have their pros and cons. It depends on the cohesion of the code.
Let me use the barista example. What if the barista that has to make the coffee is in training, and the barista at the register is the trainer. The barista at the register has to do two jobs. In programming jargon this is called chatty connections.
While you should favour composition over inheritance, Inheritance is a good tool to have.
For example a class can have methods where the functionality is not going to change in the subclasses. When only composition is used, you have to come up with a name to group those methods, create an instance of that class and inject it.
I think you represented the object hierarchy in the upside down order to make it confusing.
I don't know enough about the difference between Shader and Filter, but from Filter on it is clear the task of the classes is more and more refined.
no i mean, the actual order you open the files as a reader.
ExclsionBlend
, ah, ok so this is aBlendModeFilter
BlendModeFilter
is a .....I post it the oder you, as a human, read
You are right, if you want to do that manually.
When I want to find a method/property I use a search. Why should I do that work spitting through files?
And when a method causes a lot of problems then I will remember the place without a search.
If you're looking to sell my house for cash in Hayward, CA, look no further than Countywide Homebuyers. We specialize in providing homeowners with fast, hassle-free solutions to sell their property quickly and at a fair price. Whether you're dealing with foreclosure, divorce, or simply need to move fast, our process ensures you get a fair cash offer for your home in as little as 24 hours. Skip the lengthy traditional selling process and choose Countywide Homebuyers to sell your house for cash and move forward with ease.