Tuesday, June 26, 2012

Where's my cheese

Imperative programming is pretty difficult.

The main problem is that once you've put something somewhere you expect to find it where you put it.

“Hej, who moved my cheese?” you're thinking.

If you put a chunk of cheeses in the fridge, its nice if you can find it where you put it.  You put things in places, and later you expect to find them where you put them.  What this boils down to in terms of programming languages is the notion of variables.

In imperative programming languages, variables are the names of places where you can put things.

If I say

     int x;

In C this means there is a place called x where I can put an integer.

     x = 7;

means put the integer 7 into the place called x. Having put my 7 into my x I'd really like it to stay there forever.

“Why forever?” I can hear you asking.

“Because all other alternatives are worse”

If I can change x later, then my program will be very difficult to understand, the value of x might change many times and I'll have to understand the complete history of the program in order to figure out with the value is.

Even worse, what happens in a concurrent world, suppose several parallel processes can change the value of x at any time - if this could happen things could get really difficult to understand.

Functional programming language don't have this problem. If you say x is 7 then it is 7 for ever and ever and ever and ever.

This is really nice.

Believe me.

No comments: