Composition over Inheritance

preview_player
Показать описание
💖 Support the show by becoming a Patreon

This is a weekly show where we try to become more confident and excited about programming by learning intriguing things that we didn’t know before. Today, we are are going to talk about composition over inheritance. Inheritance is when you design your types after what they are, while composition is when you design your types after what they cando. We are going to learn about the limitations of inheritance, and how to solve them using composition.

💛 Follow on Twitch
We record the show live Mondays 7 AM PT

💛 Fun Fun Forum

💛 mpj on Twitter

💛 CircleCI (Show sponsor)
Robust and sleek Docker-based Continuous Integration as a service. I used CircleCI prior to them becoming a sponsor and I love that their free tier is powerful enough for small personal projects, even if they are private. Use this link when you sign up to let them know you came from here:

💛 Quokka (Show sponsor)
Wonder how MPJ evaluates JavaScript inline his editor. Quokka is the answer - use this link when you buy to let them know you came from here:

💛 FUN FUN FUNCTION
Since 2015, Fun Fun Function (FFF) is one of the longest running weekly YouTube shows on programming 🏅 thanks to its consistency and quality reaching 200,000+ developers.

🤦‍♂️ The Failing Together concept is what makes FFF unique. Most coding content out there focus on step-by-step tutorials. We think tutorials are too far removed from what everyday development is like. Instead, FFF has created a completely new learning environment where we grow from failure, by solving problems while intensively interacting with a live audience.

Tutorials try to solve a problem. Failing Together makes you grow as a developer and coworker.

📹 Each show is recorded live on Twitch in a 2-hour livestream on Mondays. The host, assisted by the audience, is tasked to complete a programming challenge by an expert guest. Like in the real world, we often fail, and learn from it. This, of course, reflects what the audience identifies with, and is one of the most praised aspects of the show.

⏯ On Fridays, an edited version of the show is adapted for and published on YouTube.

Content Topics revolve around: JavaScript, Functional Programming, Software Architecture, Quality Processes, Developer Career and Health, Software Development, Project Management
Рекомендации по теме
Комментарии
Автор

"The problem with inheritance is that encourages you to go predict the future..." Just genius Matt!

thiagotimm
Автор

07:03 "Inheritance encourages you to build this taxonomy of objects very early on in the project. And you are most likely going to make big design mistakes while doing that. Because humans cannot predict the future."

"I think it's just better to use composition from the start. It's more flexible, it's more powerful, and it's really easy to do."

AlexandriaRohn
Автор

Composition gets better with Object Spread (sugar for Object.assign):

return {...barker(state), ...driver(state), ...killer(state)}

Aleh_Loup
Автор

Inheritance is appropriate for single-level abstract classes where you define expectations of abstract methods that MUST be defined in a subclass. You effectively have written a API-contract. In almost all other cases, I favor composition. I love your videos and the style of teaching [often] techno-jargon.

ThomasBurleson
Автор

07:03 turning point of my perception of programming in general

I always found it hard to implement inheritance in real world applications but still used it thinking that's how I was taught oop and that's how I'm suppose to write programs

thanks a lot for such a great video

ahmarsiddiqui
Автор

I really love how extremely simple your example are presented! it really makes me roll my eyes when "beginner" guides use long-winded examples where you have to think about a thousand things at the same time and get confused and lost in concepts unrelated to what you're actually trying to learn.

SlhT-xecc
Автор

I blame the overuse of inheritance on college focusing too much on OOP and training us to overuse that pattern, and cram inheritance into our projects.
A perfect inheritance hierarchy is a wonderful thing that gives you a sense of structure and a mental model. But it's not feasible in today's agile world cause it's unmaintanable. Most agile projects would benefit from using composition.

BenRangel
Автор

Here from The Odin Project. Great video, very helpful, and explained in a fun way too! Thank you!

Avatan
Автор

This is a nice explanation. What you consider "composition" is however called "concatenative inheritance". Composition is achieved by passing more specialized components into a more general one. By doing so, you create an object model tree in which objects reference and use other objects rather than making a type tree (taxanomy) in which objects are tightly coupled to their parents (prototypal delegates or parent classes) as opposed to loose coupling in case of composition.

praguepanda
Автор

For those browsers that don't support Object.assign yet:

if( !Object.assign ) {
Object.assign = function( obj, items ) {
var src = Object( items), target = Object( obj )
Object.getOwnPropertyNames( src ).forEach( function( k ) {
target[ k ] = src[ k ]
})
return target;
}
}

thinkcritically
Автор

I'd LOVE it if you gave sample "tasks" for us to do to test ourselves. it sounds suspiciously like homework but it's the only way I could ever confirm I understood anything. haha

anthonychung
Автор

MPJME - I've been a developer for almost 20 years. I've developed in multiple languages - COBOL, ActionScript, Objective C, Java, JavaScript, PHP, etc. I feel like I've always been "behind the curve", because I've had to learn so many languages (frequently starting over). I just discovered your channel last week, and I've been binge watching, because I have learned a LOT from them. You are filling in the pieces that I've been missing from being a "self-taught" developer. I feel like I'm now becoming the top-notch developer that I've always wanted to be. THANK YOU!!!

Rashomon
Автор

Program to an interface and not an implementation - this is a good practice

youAmera
Автор

I have spent about a week reading things that dance around what you have explained so clearly here. Thank you for all you do

bobbyadamson
Автор

I just have to say: You give the best example of WHY composition should be favored that I've ever seen. Anytime someone asks me, or this topic comes up; I immediately link them to your video. You explain so much better than I ever could. Thanks for that!

robroem
Автор

At First.. I felt reluctant to watch the video with such Handsome Poster Image ( : Scarcastic :D )
But then...
I am spellbound by the way you have conveyed such a concept with such innovative, indulging and entertaining way..
Kudos. Yo!! your video rocks. I ill subscribe and follow your Videos :)

With love from India !

peripona
Автор

I had a dream about trying to flesh out a new program with inheritance. I woke up sweating. That's totally true. Man this video was great. I watched quite a few of your vids now, you crack me up.

darrenandveronica
Автор

The amount of time saved by my brain trying to figure out how to adapt classes for abstraction.
I've just switched back to more of a functional paradigm from OOP, and it's quite refreshing.
Composition for the win.

MopeyFand
Автор

hell the first few lines were enough to put it in place. i found you after15 plus videos and yours was the only one who truly explained the difference in how to use them directly while thinking
sanks

tarekghosn
Автор

This is so cool! I'm totally on board with composition. Also HUGE thanks for your work! Awesome stuff! The Odin Project represent!

FairyRat