Hacker News new | ask | show | jobs
by scriptkiddy 3365 days ago
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.