Wednesday, April 16, 2008

Inheritance is overrated

I like the object-oriented way of developing software -- especially if there is some functional flawor in it. In most language it is fairly easy to at least emulate a functional programming language by simply changing the way you think about the problem and the solution.

When I think in an functional way about a problem I have to solve using an object-oriented language, objects become collections of related functions (with this I mean pure function, i.e., they have no side effects). That is, I think about the program as lambdas that is passed around, rather than instances of classes. This may sound like a trivial and superficial difference but it is not.

I have found that if I solve a problem in a functional way, the components of the solution (functions, classes, etc) are less coupled than if I solve it in an object-oriented way. Why is this?

One reason it that an object A is provided with objects B..Z that A needs for doing whatever it needs to do. That is, A only relies on that it gets something that it useful for its purposes, instead on relying on a particular implementation. Another reason is that classes' methods are often pure functions, which decreases coupling because classes does not depend on the state of another class or in which order methods are called.

Enough rambling. Now to the point. The first reason basically says that a functional mind-set results in an structure of has-a relations between objects, instead of the "object-oriented way" is-a. With "is-a" I mean class-inheritance (the extends keyword in Java), which is the strongest way of coupling two classes and the most difficult to reuse, refactor, and understand -- at least for me.

On the other hand, I find interface-inheritance (the implements keyword in Java) very useful and I rely on it dayly.

If find it a bit funny that during the years I have used object-oriented languages, I have not once used inheritance... without regretting it. I'm getting better and better, of course, and during the last year I haven't used inheritance at all... and I'm not regretting it.

Maybe it just me, but I find inheritance overrated.

3 comments:

Dhananjay Nene said...

This is way too general a post to infer anything. Can you please extend it with some examples. Show a case where inheritance seems like the appropriate way given the existence of an is-a relationship and then argue your way through why implementing it without inheritance is better.

Torgny said...

Sure, I'll get back to this topic in a later post and discuss it again.

Note that the post describes *my* experience of implementations with and implementations without inheritance, thought. You're perfectly welcome to use inheritance. :)

Stephan.Schmidt said...

Exactly my thinking.

Don't use inheritance.

I call this style FOOP-Programming, functional, OO and procedural, because some parts of my application are functional (I'll try to keep all methods side effect free, not depend on the state of an objects but only on the input params and objects immutable), I use Java objects (the OO part) and some parts of the application are services, which have no state and exist mostly for their side effects (procedural).

Peace
-stephan