Tech IndustryDec 18, 2018
NewGenral

Composition over Inheritance

Always / sometimes / never?

Poll
79 Participants
Select only one answer
Add a comment
Airbnb avqx65 Dec 18, 2018

The answer to any sort of question like this is “it depends”. There are rarely any one-size-fits-all solutions

Intuit ➗❓➗ Dec 18, 2018

Rarely are there 100% certainties in best practices. They’re largely true, but we shouldn’t get lost in them if they somehow lower the bar for the quality of the codebase.

Tableau Zero Cool Dec 18, 2018

But the problem is that everyone thinks they have the exception to the rule.

Intuit ➗❓➗ Dec 18, 2018

If my colleagues want to forego norms, I ask for reasons, and they must be particularly compelling.

Google DayNNight Dec 18, 2018

I would take a good inheritance all day. Otoh , I suck at music so no compositions happening anytime soon.

Salesforce blackroach Dec 18, 2018

Mostly. Way easier to unit test.

Citadel Securities wagecuck3 Dec 18, 2018

In C++, public inheritance should only be used when you want to establish an "is-a" relationship (substitutability). It's a very strong relationship. In all other cases you should use composition to import desired functionality. (In some cases, private inheritance can be used for composition.)

New
behooman Dec 18, 2018

Composition is a “has a” relationship and inheritance is a “is a” relationship. Here’s an easy way to figure out whether you should use one or the other. If I inherit the class will I be writing less code ? Inheritance saves you variable duplication because lone and behold... the parent contains those variables. If I want to extend a function and be able to decorate the parent function. Composition would it make sense to compose this object and use it else where ? To reduce coding more ? These key points are how you basically write oop code.