2 ms·
> Prefer polymorphism to “if/else” and “switch” Wtf? First time I hear about this one, and it sounds like a dumb dogma. > It’s a base class for a shape with a
by charles_f 4y ago
> Prefer polymorphism to “if/else” and “switch”
Wtf? First time I hear about this one, and it sounds like a dumb dogma.
> It’s a base class for a shape with a few specific shapes derived from it: circle, triangle, rectangle, square. We then have a virtual function that computes the area.
Quite literally the first and simplest example for why you should prefer composition over inheritance[^1] (that and ducks and chickens).
Good strawman. I am unconvinced.
1: https://en.m.wikipedia.org/wiki/Composition_over_inheritance https://en.m.wikipedia.org/wiki/Composition_over_inheritance