Prefer composition over inheritance
See original GitHub issueAs the rule from title tells us, we should favor composition when we code.
So maybe it’s worth to replace all examples using class
with proper ones based on composition?
Let’s take the “Avoid conditionals” section for instance. It shows the example of inheritance while the goal was to encourage the developer to use composition. Below you can see how the definition of different airplanes could look like (using composition):
const Airplane = {
getName() {
return `${this.name}`
}
}
const AirbusA380 = {
name: 'Airbus A380',
getName() {
return `${Airplane.getName.call(this)} of ${this.airline}`
}
}
const Cessna = {
name: 'Cessna'
}
// define factory functions (airbusA380, cessna)
const airbusA380 = (state) => Object.assign(
{},
Airplane,
AirbusA380,
state
)
const cessna = (state) => Object.assign(
{},
Airplane,
Cessna,
state
)
// create instances of airplanes
const emiratesAirbus = airbusA380({
airline: 'Emirates'
})
const privateCessnaJet = cessna()
console.log(emiratesAirbus.getName()) // prints "Airbus A380 of Emirates"
console.log(privateCessnaJet.getName()) // prints "Cessna"
What do you think about replacing all class
examples with composition-based
ones?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:26
- Comments:33 (4 by maintainers)
Top Results From Across the Web
Prefer composition over inheritance? - Stack Overflow
Prefer composition over inheritance as it is more malleable / easy to modify later, but do not use a compose-always approach. With composition,...
Read more >Favoring Composition Over Inheritance In Java With Examples
Another reason why composition is preferred over inheritance is flexibility. If you use Composition, you are flexible enough to replace the ...
Read more >Composition over Inheritance - Medium
Composition over inheritance is oops concept that states classes should achieve polymorphic behavior and reuse classes using composition.
Read more >Prefer Composition Over Inheritance | by Nick Hodges
Inheritance is cool and everything, but it has its problems. ... That is a perfect example of why you should prefer composition over...
Read more >Composition vs Inheritance | DigitalOcean
One more benefit of composition over inheritance is testing scope. Unit testing is easy in composition because we know what all methods we...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
👍
@Jeff-Mott-OR let’s try to keep things civil and respectful. You can discuss the merits or pitfalls of an idea without maligning the character of someone else.
It’s obvious that you are passionate about this discussion, let’s just try to be passionate and constructive.