Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This 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.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: