Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Created May 11, 2018 14:39
Show Gist options
  • Save bdkosher/4492a9d371ff93e3594884da432db2b0 to your computer and use it in GitHub Desktop.
Save bdkosher/4492a9d371ff93e3594884da432db2b0 to your computer and use it in GitHub Desktop.
Michael Feathers' rules for inheritance

Michael Feathers @mfeathers https://twitter.com/mfeathers/status/994661651466010624

Inheritance might be the most unjustly maligned language feature. The amount of effort that has been expended at the language level to tame it over the decades has been extraordinary. Java dropped multiple inheritance and offered interfaces to cut away some hazard. Scala introduced traits; Ruby introduced modules.

But you can use inheritance safely if you just follow a few guidelines.

  1. Only inherit to complete an abstraction. Abtract classes have empty spaces that you fill. Don't override anything concrete.
  2. Preserve client context. No existing client should be surprised by the behavior you add via a subclass. When you follow guideline 1 this is easy.
  3. Don't mix concerns in a hierarchy. The hierarchy is the unit of responsibility. Not the class.

That's it. You end up with shallow manageable hierarchies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment