3 ms·
It's just as true in other languages. More often than not, programmers thinking about code reuse attempt to solve it via inheritance, which is almost always the
by mercurial 13y ago
It's just as true in other languages. More often than not, programmers thinking about code reuse attempt to solve it via inheritance, which is almost always the wrong answer. With some persistence, you can end up with a nice four or five level deep inheritance tree with 10 methods randomly overriden at various levels in the tree. Good luck figuring out how the damn thing actually works if you didn't write it.
- dgcoffman 13y agoOkay, but that kind of polymorphism is actually a replacement for conditional logic, e.g. switch on type antipattern. People hate conditionals. People hate polymorphism. Everybody is wrong about everything pretty much all of the time, it appears.
- cmbaus 13y agoIn JavaScript, like other duck typed languages, you have dynamic dispatch without inheritance. For example: var objA = {doIt:function(){console.log('hello from a')}}; var objB = {doIt:function(){console.log('hello from b')}}; var doItDynamically = function(doitObj) { doitObj.doIt(); }; doItDynamically(objA); doItDynamically(objB); a and b do not share a common class (since classes do not exist in JavaScript), but they implement the same interface. For this reason, they can be used polymorphically, as if they shared a common base class or interface in Java or C++.
- deleted 13y ago[deleted]
- mercurial 13y agoYou can usually get code reuse via composition. If need be, you can have your objects implement one or more common interfaces (which may themselves inherit from other interfaces, that's not a problem).
- MartinCron 13y agoEverybody is wrong about everything pretty much all of the time, it appears. Misanthropy Driven Development? I would like it if I didn't already hate everyone and everything.