News for hackers. Stuff that matters.
Re:Programmer Competency Matrix (2016) by scriptkiddy on Monday March 27, 2017 @11:54AM (#13968747)
Inheritance is a useful tool when used sparingly. Ideally it should be used to encapsulate logic that is going to be re-used multiple times. I don't really think there are pros and cons as much as there are trade-offs between the 2 paradigms.
Re:Programmer Competency Matrix (2016) by no_wave on Monday March 27, 2017 @02:50PM (#13970548)
When is it preferable to composition?
Re:Programmer Competency Matrix (2016) by scriptkiddy on Monday March 27, 2017 @03:05PM (#13970686)
When modeling objects that need to encapsulate mutable data and pass messages between one another.
Re:Programmer Competency Matrix (2016) by pka on Monday March 27, 2017 @06:55PM (#13972571)
Inheritance is orthogonal to that. I.e. Erlang, an (impure) functional language without inheritance was literally built for that sort of thing.
Re:Programmer Competency Matrix (2016) by scriptkiddy on Tuesday March 28, 2017 @12:23PM (#13977967)
Oh, you were asking about inheritance, not objects. My bad. I'd say composition is almost always preferable.

That said, single level inheritance can be useful if you need to have multiple different objects that require very similar business logic but contain vastly different data.

A good example of this is Django's class based `View` class. I'm not talking about all the mixin views Django offers, just the simple generic `View` class. It handles routing to specific methods based on HTTP request METHOD. That eliminates the need to write the same ~100loc over and over. The alternative would be to use function based views. However, with function based views you end up basically recreating what the class does anyways after checking for each HTTP request method.