Post

One simple programming lesson I'd LOVE to help new programmers get right: Stop using inheritance for everything. I don't know what it is, but I feel like it has to be something about how educational content is structured by either: - The formal education system - and/or online learning platforms

... but anecdotally, I have continued to see inheritance get completely overused when developing software. I'm also speaking about this because *I* was a huge offender of this early in my career. I wrote absurd amounts of code that completely abused inheritance.

Why the heck do you think I have an entire course on refactoring?! My hunch, and I don't want to upset too many people (just a few), is the following: - People start learning to code - They eventually come across Dont Repeat Yourself (DRY)

- They start to see patterns in their code and start over-using DRY because if it looks remotely similar, we might as well refactor it to a common spot! - These developers start learning OOP principles

- Inheritance is the one that sticks because we always talk about memorable things like how a car is a vehicle and a cat and dog are animals - People suck at creating composition examples I guess, so nobody remembers to use it??? (The jury is still out on this)

But then a discovery is made... One that will shape the next few months or few years of terrible programming practices:

0 reposts0 likes

Since DRY is the single most important thing evarrrr (please pick up on the sarcasm, I beg you) ANDDD we need a common spot to put that reusable code for easy access... We can put it into the base class!

Nice, now any time we want to use that helper method, we can just make sure our class inherits from the base class. Awesome! Oh, now we need to customize it a bit for this new class? No worries, we'll just override that behavior in this class. Perfect!

... after 42 months and 1337 derived classes going up to 69 levels deep in a hierarchy, you realize something unfortunate. Composition was the way.

Don't spend the next 42 months refactoring your code -- get a head start and the next time you type "abstract class" or you skip sealing a class, ask yourself "Could I compose this instead?" This rant is brought to you by my receding hairline and the white in my beard. Thanks!