4 ms·
Inheritance should be used for interfaces, not code reuse (unless that comes free with it).
by _a_a_a_ 3y ago
Inheritance should be used for interfaces, not code reuse (unless that comes free with it).
- nradov 3y agoThen how do you avoid duplicate code without using inheritance? Other solutions like placing shared functions in "helper" classes are far worse from a complexity and maintainability standpoint.
- javcasas 3y ago"helper" classes are a symptom of an object-obsessed programming language, a language where functions can't live in a module, they must always exist in a class. Especially if it doesn't make sense for a class to hold them. You avoid duplicated code by making generic functions, putting them in modules, and making your objects accept them as parameters.
- nradov 3y agoWell I've done that before and the results were no better than using inheritance. What's the point?
- _a_a_a_ 3y agoI guess it's like this – implement interfaces, and common code comes with it when you inherit (then override to modify behaviour). A common interface really should have common code so this is appropriate. What is wrong is to inherit from an object with a different interface just to get your hands on the code - that might seem obvious but when OOP started to be used in industry, that was acceptable or even recommended practice. That malpractice was formalised away by Barbara Liskov with the Liskov principle. I'm pretty certain you're doing the right thing already so it was a clarification rather than a criticism.