3 ms·
This is why Go has standalone functions and interfaces. You can easily share logic between types without having the types be the same thing. The biggest probl
by NateDad 12y ago
This is why Go has standalone functions and interfaces. You can easily share logic between types without having the types be the same thing.
The biggest problem with inheritance is that it's a lot like monkey patching (except in production)... the rest of the base class's code expects method X to do ABC, and you've just swapped out the implementation to do LMNO ... and if that never bites you in the ass, you're luckier than most OO programmers I know.
- jrockway 12y agoThis is the big insight. Instead of trying to inherit code, figure out what that code does that interesting, and allow that code to do the interesting things without being inside the class. For example, imagine your class has a "WriteToFile" method. Instead of that, it should just have a method to return its representation, delegating the responsibility of writing to a file to something else. (Of course, the fact that a file can be written to should also be one of those "interesting things", and the thing that writes shouldn't care that it's backed by a file.) Instead of foo.WriteToFile("/tmp/foo") You might write: file.Write(foo.Representation()). I promise that most inheritance in the real world is attempting to reuse something like "WriteToFile". "I wouldn't want to copy-paste the file-writing code, so I'll inherit from something that can write itself to the file." No. Don't do that.