Programming

Monday, June 26, 2006

A comparison of programming paradigms

Why is object-oriented programming (OOP) so popular?

It is often said that OOP "models the real world". I would argue that this is not true - OOP models the way we think. That sounds radical, however the way we perceive the real world is via the way we think, so there is not such a huge difference after all.

OOP also models natural language. There are nouns (objects), verbs (methods/functions), adjectives (properties) and adverbs (parameters). Natural language is also closely related to the way we think.

Functional programming is more abstract, because although solutions can be more elegant, it does not lend itself quite so naturally to human thought. People think in terms of concrete things that they can push around, so stateful languages are therefore easier for our brains to manage.

Logic programming languages are even harder to write complex algorithms in. The problem as as soon as you have recursion, your head needs to start abstracting again, and it's difficult to visualise too many things at once. At least with functional programming you have inputs and outputs, which are concrete things - with logic programming you have partially-filled in inputs and outputs and algorithms can frequently be run in reverse.

For expressing simple problems, logic programming can express things very nicely, since effectively the program and the data are combined. Predicates are data that are executable. However you still need to understand the execution model and backtracking to achieve non-trivial tasks, and this is where logic programming falls down. For example, implementing a merge-sort algorithm in Prolog (which I have done) is very satisfying but I can't help thinking that it is out of reach for the average programmer.

For simple, declarative tasks, I don't really consider that to be programming since there are no algorithms involved. I would not class data transformation as programming (though it is of course an extremely useful activity).

Imperative styles, do A, do B, do C, are pretty easy for our brains to manage. Therefore, scripting languages, and procedural languages are pretty popular. However OOP takes this one step further by creating objects that react to things, rather that data which is operated on by a procedure. Objects reacting to things is an easier way to think about things since it models our own perceptions of the world.

I therefore argue that the relative popularity of programming paradigms comes from the ease at which the paradigms can be used, which in turn comes from how natural we find the language. If you require an IQ > 130 to master a paradigm, perhaps the paradigm isn't so useful after all.

0 Comments:

Post a Comment

<< Home