¯\_|°—°|_/¯

Johnny Ray Austin

@recursivefunk

                 Composition             Inheritance

(Classical)

Me

^https?\:\/\/.{1,}\/recursivefunk$

A Funny

Object Oriented Programming

  • Maps real world objects to software
  • NOT synonymous with inheritance
  • Focuses on what a thing is
  • Variations represented as sub-classes
    • Inheritance 

The Gorilla and the Banana Problem

Class A

Class B (is an A)

Class C (is a B and an A)

Class D (is a C and a B and an A)

You want a banana from 'C' so you create 'D' to keep your code DRY. But you end up with a gorilla holding the banana in the middle of the jungle

So? What's the big deal?

  • Hierarchical (vertical) extension
    • To prevent duplication
    • Brittle
  • Constructs limited to human experience
    • More on this later (the baggage problem)
  • The universe contains too much information
    • More on this later (the discreteness problem)

The Discreteness Problem

The possibilities are (practically) endless

Discrete Vs Continuous

The Discreteness Problem

Software engineers utilizing abstract discrete constructs to model what feels like a continuous (as far as perceptions are concerned) reality.

The Baggage Problem

The Baggage Problem

 

  • Words carry abstract baggage
    • ​Ambiguous
    • Limits expression
  • Perceptions are often very subjective
  • Ball
    • Entity containing (billions?) of particles comprised of electrons, protons and netruons bound together by the strong nuclear force
    • This also describes EVERYTHING in the universe
  • OOP and Inheritence are bound to these abstractions

The Baggage Problem

 

When does a person cease to be themselves?

You

The Sugar Problem

  var num int = "1"
  class Thing {
    constructor(spec) {
      this.foo = spec.foo;
    }

    getFoo() {
      return this.foo;
    }  
  }
class_but_not_really_class_see_js_devs_like_class_syntax_but_despite_not_actually_having_traditional_complimentary_features_the_powers_that_be_decided_that_it_was_ok_to_introduce_this_construct_to_appease_people_who_just_wanted_it_again_not_actually_creating_a_class_here Thing {

    constructor(spec) {
      this.foo = spec.foo;
    }

    getFoo() {
      return this.foo;
    }

}

What's the Problem with Sugar?

  • Nothing
  • Everything
    • "Too much of anything" as the saying goes
  • Sugar should
    • Make things more clear (map, reduce)
  • Suguar should NOT
    • Deceive (or be otherwise misleading)
  • This particular sugar is a lie
    • Encourages the use of a feature of the language doesn't actually exist
      • What about coffescript/typescript etc?
        • What about them? lolz *burn*
        • Kidding those are separate languages (compile to JS)
  • Confusing to (JS) beginners
    • See previous code example

"If a feature is sometimes useful and sometimes dangerous and if there is a better option then always use the better option."

 

- Douglas Crockford

Composition

  • Focuses on what a thing has or uses
    • Rather than what it is
    • Think behaviors and features
    • Eliminates intrinsic hierarchy
    • More explicit (mostly)
  • Permutations are descrete by design
    • Put down the baggage
    • Flexible
    • Refactoring is easier

Bananas Only

No Jungles. No Gorillas

Component 1

has

banana

Component 2

has

orange

Component 3

has

banana

orange

Component 4

has

orange

strawberry

kiwi

banana

Just Bananas (and other fruits)

  • Functionality extends horizontally
    • No intrinsnic  hierarchy
  • Still DRY
  • Functionality not tied to identity

Leave The Baggage

Leave the Baggage

  • Components are the sum of their parts
  • Don't think "Car"
    • Think multi-functional vehicle-like device with
      • 4 wheel, steering, breaks etc
    • Is this a car?
      • Who cares if it functions as designed
      • Doesn't matter if it actually sufficiently represents an actual car

How?

https://github.com/recursivefunk/nodedc-2016-05-19

Conclusion

  • The universe feels continuous to humans
    • Too many permutations to model
  • Bananas only
  • Leave the baggage
  • Horizontal extensions (has-a, uses-a)
    • No hierarchies (is-a)
  • Classical inheritance is not "evil"
    • Best used in a supprotive environment (not JS)
  • Composition is a valid (scalable) alternative
    • The best solution for most JS applications
    • Plays nice with functional styles
  • Sugar will destroy you
    • If abused
      • ES2015 Class keyword
  • I believe in you.

Come at me, crow

Questions

Composition Over Inheritance

By Johnny Ray Austin

Composition Over Inheritance

Why You Should Favor Composition Over Inheritance (With JS)

  • 756
Loading comments...

More from Johnny Ray Austin