The End Of Object Inheritance & The Beginning Of A New Modularity

The End Of Object Inheritance & The Beginning Of A New Modularity

Composition over Inheritance Link to heading

(4:30) “If you are explaining, even if you are write, you are losing.”.

Inheritance approach:

class MyAbstractBase:
    def OrangeMethod(self):
        ...

    def GreenMethod(self):
        """
        Do not call OrangeMethod() in your implementation...
        """
        raise NotImplementedError()

    def BlueMethod(self):
        ...

vs composition approach:

class Green:
    def GreenMethod(self):
        raise NotImplementedError()

class ImplementedGreen(Green):
    def __init__(self, blue):
        self._blue = blue

    def GreenMethod(self):
        ...

Object Inheritance Fails At Fault Intolerance Link to heading

(12:55)

  • What happens if an extending class violates its parent’s stages of abstraction?
    • You might get silent state corruption if the class stores mutable state.
    • You might get an infinite loop.
    • You might get perfect behavior until a year later when the library in which the superclass is defined is upgraded.

Composition Fails At Fault Tolerance Link to heading

(14:25)

  • Doing the illegal is impossible because composition substitutes unidirectional references for bidirectional references.
  • “Make Illegal States Unrepresentable” (Yaron Minsky)
  • “Make Illegal Behavioral Interactions Impossible”

The right way to break code into small methods and small objects Link to heading

(16:10)

  1. Break it so that relationships are minimized among the resulting pieces.
  2. Break it so that unidirectional relationships dominate and bidirectional relationships are absent.
  • bidirectional relationships (inheritance approach) are never necessary - “in 10 years I haven’t seen one that I haven’t been able to eliminate”