Profile picture
Michael Feathers @mfeathers
, 9 tweets, 1 min read Read on Twitter
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.
You could always “prefer composition to inheritance” but that’s too far. Some inheritance based solutions are elegant and if you follow the three guidelines you can easily refactor if you need to.
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Michael Feathers
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member and get exclusive features!

Premium member ($3.00/month or $30.00/year)

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!