20 March 2013

how simple things can go awfully wrong

You know programming languages? At least one or two?

A two-liner, without any regular expression, should not be a too-great adventure...

Try the following in your favorite language:

i = 1
i += ++i + ++i

A simple assignment to an integer variable, some forms of add-operators.

They're evil - believe me. Things go wrong from here:

Let's analyse the expression in a functional way:

  • With i set to 1, the first ++i should yield 2.
  • Calculate the two of them one-after-the other, the second one yields 3.
  • Calculate the two of them in parallel, both yield 2.

So - the result should be something between 5 and 6.

Let's try Ruby first:


Slightly off any sensible calculation. Consider it "failed".

Move on with Java:

java funny expression

Believe me, C# and JavaScript and some other JVM languages come to the same conclusion.

But we're not finished here. Fire up your command line and type:

awk 'BEGIN {i=1; i+= ++i + ++i; printf i}'

Puuh - yields 8. Eight. Seems a bit off for me, but awk might have reasons.

We'll check that with php in a second:




Eight again. So many commercial websites are build with php - and they all calculate like that? re-consider your e-commerce behaviour...

Now, if you happen to have one of those wonderful GNU-C compilers installed - try out the objective-C variant of this expression: You will see eight again. Two more than with Java.


Finally, the mother of all programming languages, the common ground of all platforms, the C programming language:

#include <stdio.h>
int main(int argc, const char* argv[]) {
int i = 1;
i += ++i + ++i;
printf("i+= ++i + ++i liefert %i\n", i);
return 0;
==> 9

I have to declare it the winner of this informal competition. At least numerically the winner: Nine. 9. Three times three.

Three times what Ruby calculates. 30% more than Java and C#. Btw, compiled with the same GNU compiler that found 8 to be the answer for Objective-C. 

 If you check that with Perl, fine: Nine again:

$i += ++$i + ++$i;
print "i = ".$i."\n";
print "\n\n";
==> 9

I personally stopped laughing here: If we stumble upon two-liners, what about real programs, with memory, sync/async, concurrency and UI issues? With changing requirements?


Eventually, thinking (!) about your software might help, so instead of "test driven" or "behavior driven" or "nerd driven", what about "thought driven development" (as Rich Hickey pointed out in his famous speech "Hammmock Driven Development"


As a sidenote: Cobol outputs 6. I did not find any solution less than 3, nor one greater than 9.

PS: Thanx to Franz Antesberger for this nice riddle!