Composition over Inheritance 👾🕹️
Tags: programming, development, architecture, softwareengineering
Published: 2024-02-08 14:05:15

A lot can be learned about software development by playing retro games.

Video Version:
{% embed https://www.youtube.com/watch?v=HNzP1aLAffM %}

Tetris, for example, was a popular puzzle game back in the '80s. If you don't know what Tetris is (maybe you weren't born yet?!) let me quickly catch you up!

The whole point of the game is to complete as many lines as possible using a series of blocks that dropped from the top of the game board. Completing lines will clear the blocks from the board making space allowing you to play longer - and obtain a higher score.

It sounds easy, but let me tell you... it can get pretty intense when the board starts filling up with blocks and all you're trying to do is not freak out!



![Tetris Game Board](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8007ntmm8nyho571b5a.gif)



Now you may be thinking: _"What can Tetris teach us about software development?"_

In Tetris the whole goal of the game is to compose shapes together. You could say, the goal of the game is to create a new shape: a _Complete Line_, using existing shapes .

Likewise, in programming, we are often creating something new by utilising existing components. In both cases we combine existing components to create what we need.

This principle is called: Composition.


![Tetris Block using Composition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0s1c4tc2cfs2y4ks05u.gif)



## Composition



Composing objects together (which may be composed of other objects themselves) is a powerful way to build complex systems with a high degree of flexibility and modularity.

If we were to pretend our `CompleteLine` was an object, we could say: _"our completed line **has** four shapes"_.

Composition creates a _"has a"_ relationship while inheritance is useful for modelling an _"is a"_ relationship.

When we want to compose other objects together, just like shapes and Tetris, to achieve the functionality we want: this is Composition.





![Mario](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/if1fk4p275r77ablu15n.gif)

## Inheritance


What can we learn about Inheritance from retro games?

Let's think about... Mario!

In most Mario games, when  Mario obtains a Power Up (such as a Fire Flower or a Super Mushroom) he gains a specific ability or enhancement.

However, he can only have one power up at a time.

This limitation of a single Power Up mirrors the restrictions of Single Inheritance.

In some programming languages a class can only inherit from one superclass at a time.

This creates a hierarchy where subclasses inherit the properties and behaviours of only a single parent class.

As we mentioned when explaining Composition.

Inheritance creates an _"is a"_ relationship.


![Super and Fire Marios](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xi6et06q2uh2z9jq257d.gif)


Super Mario _is a_ Mario.

Fire Mario _is a_ Mario.

And so on.

Inheritance is when we design our types around what they are - and can be an intuitive way to model a simple system.

So, how come people often say we should: _"Favour Composition over Inheritance?"_

The main arguments for choosing Composition over Inheritance primarily revolve around the maintainability and flexibility of a system once it starts to grow.


![Separating Tetris Blocks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bup7317ycn3dxbdrajxg.gif)


## Separation is not Organisation

A reason often stated why we should prefer Composition over Inheritance is the ability to promote code reuse. However, I don't believe Composition should be the reason we separate code.

Scott Yu-Jan describes this perfectly in the intro to one of his videos:

{% embed https://www.youtube.com/embed/-s74phtezf4?end=54 %}

> They just kinda divide up your stuff into smaller units of mess.

If we considered composition as being the driving factor for creating smaller units of code, we would end up with the programming equivalent of a _drawer organiser_.

Would we have smaller units of code? _Yes._

Would we still have a mess? _Yes!_


It's for this reason I don't believe Composition to be a good driving factor for code reuse.

Instead I believe it's following the Single Responsibility Principle that helps us build smaller units that can later be composed together. Smaller units of code that adhere to the Single Responsibility Principle are easier to maintain and easier to update. They are more flexible because they were designed to do one thing and do that one thing well!

Composition's role is to help us take advantage of these existing smaller units of code - not define them! It's what allows us to build more complex functionality in ways inheritance can't.

Inheritance is also one of the strongest possible types of coupling. Changes to a base class can inadvertently affect the functionality of its child classes.

How often does this happen?

Well, it happens often enough to be called: _The Fragile Base Class Problem._



![PacMan Ghosts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4phswgtn18tyyzb1yzd.gif)


## The Fragile Base Class Problem

Let's explore what this means by using the ghosts from PacMan.

Imagine a ghost base class with four other ghost subclasses. If we were to introduce a change to the ghost base class - all subclasses would be impacted.



Composition however would provide us with _Object Containment_ - reducing the impact of changes.

Instead of using Inheritance to reuse code, we can use _Dependency Injection_ and utilise Composition instead. We also spoke about about _Single Responsibility_ earlier, too.

That's two of the five SOLID principles.

I'll be covering the SOLID principles in more depth in future content, but for now - seems like it's game over for inheritance, right?

Well...

Would I advise you to favour Composition over Inheritance?

Yeah, I would - because of all the reasons mentioned above. But not _all_ inheritance is bad and we should not blindly avoid it. Composition and Inheritance are just tools.

Instead, I love this perspective from Mathias Verraes on defining heuristics on when, and when not, to use Inheritance:

{% embed https://twitter.com/mathiasverraes/status/1153630516756852738 %}