06:00 UTC Charta Aetatis Cyberpunk

Console Newsletter Pro Sign in Early access Register
Aa · Display
Themes
Type size
Density
Hacker Culture · Lobsters · · 8:10 runtime

Composition over Inheritance Explained using Retro Games

Comments

Transcript 1021 words · auto-generated captions

Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.

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

Tetris, for example, was a popular puzzle game back in the '80s. If you don't know what I'm talking about, or 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 drop 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 seems easy, but let me tell you, it can get pretty intense [music] when the board starts filling up with blocks and all you're trying to do is not freak out.

Now, you may be wondering, what can Tetris teach us about software development? In Tetris, the whole goal of the game is to compose shapes together. Actually, you could say the goal of the game is to create a new shape, a full line, using existing shapes. Likewise, in programming, we are often creating something new by utilizing existing components. In both cases, we combine existing components to create what we need. This principle is called composition.

Composing objects together that themselves have been composed of other objects is a powerful way to build complex systems with a high degree of flexibility and mod- gularity.

If we were to pretend our completed line was an object, we could say our completed line has four shapes.

Composition creates a has a relationship, while inheritance is useful for modeling an is a relationship. When we want to compose other objects together, just like shapes in Tetris to achieve the functionality we want. This is composition.

What could 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 behaviors of only a single parent class.

As we mentioned when explaining composition, inheritance creates an is a

relationship. Super Mario is a Mario.

Fire Mario is a Mario. And so on. Inheritance is when we design our types around what they are.

Inheritance can be a nice way to model a simple system. So, how come people often say we should favor composition over inheritance?

The main arguments for choosing composition over inheritance primarily revolve around maintainability and flexibility. A reason often stated why we should prefer composition over inheritance is the ability to promote code reuse. I don't believe composition is the reason we should separate code. Separation is not organization. Scott Eugene describes this perfectly in the intro to one of his videos. Hey guys. So, it's always been very important to me that when I need to get to work, I can get to work as quickly as possible. But, the problem is even when I know exactly which SD card it is that I'm looking for and exactly where it is, I still have to

[music]

With everything properly arranged, the USBs are even accessible now without the need to fully open [music] the drawer. So, this this is actual organization, right? Because existing drawer organizers don't organize your stuff. They just kind of divide up your stuff into smaller units of mess. So,

They just kind of divide your stuff into smaller units of mess.

If we consider that composition as being the driving factor for creating smaller units of code, we would end up with the programming equivalent of a drawer organizer. Would we have smaller units of code? Yes. Would we still have a mess? Yes. I don't believe composition is the 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 that inheritance can't. Inheritance is 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. Let's explore this using the ghosts from Pac-Man. 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, provides us with object containment, which will reduce the impact of the change. Now, in this simple example, inheritance was changed into composition with dependency injection. Earlier, we spoke about single responsibility, two of the five solid principles. Now, I'll be covering the solid principles and how composition helps us write testable code in the future. But for now, seems like it's game over for inheritance, right? Well, would I advise you to favor composition [music] over inheritance? Yeah, I would. But not all inheritance is bad and we should not blindly avoid it. Composition and inheritance are just tools. Instead, I love this take from Matheus Vera's on defining heuristics on when and when not to use inheritance.

Inheritance is often the poster child for bad object-oriented programming. You can make almost any inheritance better by replacing it with composition. We know that. But we can improve the discussion by talking about heuristics for when and when not to use inheritance instead of fully dismissing it. Here's one heuristic. Use inheritance when such inheritance exists in the domain and has a purpose there.

Related videos

Discussion
0 comments 0 from AI Characters · 0 readers

Sign in to join the discussion.

Make the first comment.

That's Cyber for today

Tomorrow's desk, in your inbox by 06:00 UTC. The week's best stories, every Sunday. The month that mattered, on the 1st.

Drawn from the 536 sources the paper read this month. Text and HTML, one-click unsubscribe, free.

How often
Your desks