3 ms·
but composition is much, much easier to understand and work with. The article you linked mentions IMHO the biggest disadvantage: One common drawback of using
by userbinator 7y ago
but composition is much, much easier to understand and work with.
The article you linked mentions IMHO the biggest disadvantage:
One common drawback of using composition instead of inheritance is that methods being provided by individual components may have to be implemented in the derived type, even if they are only forwarding methods
Code whose only purpose is to "appease the design" and otherwise does absolutely nothing of value is a strong-enough negative reason. It's pure bloat, overhead that gets in the way of both programmers trying to understand/debug and machines trying to execute.
Too much inheritance can lead to a "where is the method" problem, but I think that's still better than the alternative of dozens upon dozens of lines of otherwise useless code, because the former at least does not increase the amount of code that needs to be written/debugged/maintained.
- wwright 7y agoHow often do you need to write methods just to forward to another method that _stay_ that way, and don’t evolve more logic later on? I avoid inheritance like the plague, but I feel like I don’t really write forwarding methods very often.
- pojzon 7y agoFew of design patterns work exactly like that: forward to another method. Evolving that API afterwords is not a lot of hassle. I really fail to see the argument here..
- cartlidge 7y agoThat isn't an inherent issue of composition though. You could say {...students(ajaxDb), ...teachers(ajaxDb)} to create an object that can handle both students and teachers. Or you could say public class StudentsAndTeachers extends Students implements IStudents, ITeachers { private _teachers; public constructor(Database db, Teachers teachers) { super(db); _teachers = teachers; } public getTeacher(TeacherId teacherId) { return _teachers.getTeacher(teacherId); } } new StudentsAndTeachers(ajaxDb, new Teachers(ajaxDb)); It seems clear that the features of the language define what is verbose and what isn't verbose. Even the inheritance in the language that encourages inheritance is more verbose than the composition in the neutral language. (More likely, you wouldn't write this, but every example that is colloquial in inheritance is non-colloquial in composition. The composition oriented solution can be used cleanly with strong guarantees provided to its users; whereas all inheritance oriented solutions are so tacky and hard to use that you will demand a framework with dependency injection which half your team won't be able to understand and will have to treat as magical incantations.)
- ridiculous_fish 7y agoComposition is entirely capable of creating an ad-hoc informally specified bug-ridden slow implementation of inheritance.
- cartlidge 7y agoThat relates to my comment in what way?
- AnimalMuppet 7y agoPretty obviously, ridiculous_fish is claiming that that's what you're doing. Note: I'm not saying that ridiculous_fish is right. But it's pretty obvious that that's the claim.