Composition Vs Inheritance - Why You Should Stop Using Inheritance

preview_player
Показать описание
🚨 IMPORTANT:

Use code KYLE for an additional $50

Object oriented programming has been around for a very long time and is one of the most popular programming paradigms, especially in academic curriculum. Unfortunately, pure object oriented programming is not the best solution to many problems. This is why in certain industries, like game development, the idea of composition has risen in popularity to overtake inheritance from object oriented programming. In this video I will explain the differences between inheritance and composition through one example program. I will also go in depth about the problems with using inheritance that composition solves. This was an eye opening concept for me when I first learned about it, and I know use composition all the time in my code.

📚 Materials/References:

🧠 Concepts Covered:

- What inheritance is
- What composition is
- The differences between inheritance and composition
- Why composition is better than inheritance
- How to write composition based code

🌎 Find Me Here:

#CompositionVsInheritance #WDS #CleanCode
Рекомендации по теме
Комментарии
Автор

its all good but i dont think shark can walk

ErrorDebug
Автор

Incredible feeling when I finally understood why you'd need composition: "what if you wanted a monster that can swim and fly" and, as someones who *hates* copy-pasting code it actually struck me. Thanks !!

neverchesterfield
Автор

Although many people would argue memory isn't much of an issue in most code except a few specific use cases, I think it should still be mentioned in a video like this. When you're creating functions in this way, without prototypal inheritance, you're creating separate functions for each object instance. With prototypes you'd only have to create them once. This can impact your memory usage, although this is generally only an issue for very large functions or if you're creating thousands of instances per frame, or if you continuously add instances that aren't removed from memory. Those are relatively rare use cases. Most often you'll be creating singletons or under 100 instances of an object accessible in any given frame, in which case the maintainability and expandability of composition should be preferred. However, if you ever did have to switch to prototypal inheritance for performance, you'd have to change your entire implementation.

Valyssi
Автор

For how many years of just copy-pasting codes now I understand, I am amazed at the principles and the art behind the logic and you've nailed it by explaining it well, very straight to the point. Thank you man!

romarpatindol
Автор

I just discovered your channel and I must say you have a great way of explaining things.
Thank you for excellent work ♥

Nerffye
Автор

JavaScripts (functional): missing something, lets add Classes

JavaScript(classes): you know what, Classes sucks back to functional

ericcartman
Автор

when he said "the ability to create a new type of monster is really powerful" I cracked lol

rajeevsinxh
Автор

This just blew my mind away! For a few months now, I have been looking for a way to write better classes. Rather than having all my functions defined in a class, which makes it bigger that it should be, I wanted to define classes outside and just reference them when I construct my class. This is really awesome! Thanx for sharing!!!

knightmarerip
Автор

I love that you can talk about high level concepts while still writing real code. Amazing, and subbed!

kevinrobertandrews
Автор

Good stuff. As always there's more than one approach. This one I'm sure to use.

nerdiloo
Автор

I have a small problem with this solution. swimmer() and flyer() functions appear to get access to the "name" value via closure. So, if I update the name property after creation the object, it would not be reflected in swim() and fly() functions. They would still output the original name.

mrfreddiep
Автор

What about memory efficiency for example: What if we have tons of monsters? All the monster methods will be copied in every monster that we created without using prototypal inheritance.

emibademi
Автор

Solid explanation and tutorial. I think this is very useful implementation of a factory design pattern. I'd use interfaces as return types to keep it type safe in ts.

cburys
Автор

This guy reminds me of Code Drip. No bs, just straight to the point. I don't really tend to subscribe to too many of these types of channels but this guy always gives good advice.

pete
Автор

Awesome tutorial . thanks for making this type of contents 👍♥️

neozoid
Автор

I don't even do javaScript and I understood you perfectly. Beautiful explanation!

mynoghra
Автор

Because inheritance is so important in object-oriented programming, it is often highly emphasized, and the new programmer can get the idea that inheritance should be used everywhere. This can result in awkward and overly complicated designs. Instead, you should first look to composition when creating new classes, since it is simpler and more flexible. If you take this approach, your designs will be cleaner. Once you’ve had some experience, it will be reasonably obvious when you need inheritance. (Bruce Eckel)

arifwahyudiansyah
Автор

I'm a web dev learning to make games, so this example was super relevant for me! Thanks for the video!

rebelmachine
Автор

I'm a little late seeing this but the comparison of these ideas along with the examples you gave is a pretty good example of the differences between object oriented programming and functional programming.

With inheritance, you are following the oop paradigms. Everything is an object extending the capabilities of other objects.

Composition on the other hand has you break your objects into separate functions. You can then just take which functionality you want to inherit by adding the function to the object your creating. Think of this like a Lego set. The functions you create are your Lego pieces. To create an object you just piece together your Lego pieces.

Which is better is a verry debatable topic, both having their pros and cons. Just remember what one can do, the other can as well and it's typically best to follow either oop or functional programing practices and not mix them in the same project. It can be confusing for other developers, especially if they aren't familiar with functional programming

xKhfanx
Автор

This is gold, excellent explanation, thank you very much

ohiwantyoutobelieve