question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prefer composition over inheritance

See original GitHub issue

As 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:closed
  • Created 7 years ago
  • Reactions:26
  • Comments:33 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
ericelliottcommented, Jan 6, 2017

👍

11reactions
jokeyrhymecommented, Jan 26, 2017

Eric Elliott sold you blue water and called it medicine.

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found