Object orientation — not as natural as we thought?

29 Sep 2009

Back in 1999 or so I held a course in object oriented (OO) programming and design. I remember myself saying that an important idea of OO is that it reflects how we humans think. We think of the world as consisting of objects and how they interact. That maps well with OO’s view of a system as a set of objects that send messages to each other. So in theory, OO should feel natural to us humans. It should be easy to express our thoughts as objects that pass messages to each other.

So how come object oriented programs are so riddled with classes that are nothing but shallow wrappers for functions? Those classes don’t represent a natural object in the problem domain. They are just clumsy object oriented shells around a functional or procedural design. If you have programmed Java you know what I mean; those classes that typically have names ending in -Handler or -Manager. Their sole purpose is to provide functionality and would be better implemented as functions. Java doesn’t allow free functions. Instead, a function has to be a member of a class, so typical Java code contains lots of pointless classes that just serve as function wrappers.

Now some people say that this is not “proper” object orientation, and they would be right. But think about it. Even though Java has practically forced us to design computer systems based on objects and classes for over a decade now, we still resist. We think of our systems as — at least partially — consisting of functions and not just objects. It seems like our minds are not so naturally inclined to solve problems by dividing them into objects after all.

OO is a powerful tool, but it probably shouldn’t be the one and only paradigm that you use in a system. You should be able to mix procedural, functional and OO style in a pragmatic way. Steve Yegge’s old well written rant about the Kingdom of Nouns says this in more clever and amusing way than I can, so I’ll leave it at that.

Instead of Java, I’d recommend programming language that allows multi-paradigm programming. Either that, or use proper OO. Whatever that is. One thing’s obvious: It’s not something that comes naturally to us.

blog comments powered by Disqus