It is official, at least according to the young man (he’s 20, I can’t call him a boy) I give private programming lessons to. He said that his class is full of “old people” – most of them is even 30 or older! Scary! Anyway I have been programming for more years of my life than not and most of them have been spent on the C language. Maybe that classifies me as very old nonetheless. After all C had been designed and developed nearly 4 decades ago, when I was 4.
One of the question in an exam attended by my tutored boy was:
Define the result of the following expression given x=N and y=kx = (y+1>x) ? x++ : y++;
Suddenly a bell rang in my head. Red alert was buzzing at full-volume, but another bell was ringing as well.
The main red alert was labeled as “Undefined Behavior”. Every bad thing could happen when U.B. is invoked by the unknowing programmer. If he is lucky then he just gets something out of the order he expects, otherwise he can blow everything up.
Back in the days where Real Men wrote their own compiler, the C designers decided to relax some constraints in the language semantics so that more aggressive optimizations could be implemented in the compilers. So within a C expression it is not defined the relative order of the side effects. If two or more side effects apply to the same operand, then you have a problem. Moreover the assignment operator (=) in the C language is just an operator with a side effect, so the expression:
is undefined behavior.
The day after I investigated on the C language FAQ and found that the ternary operator (? 🙂 introduces sequence points (a jargon to say that all the side effects have to happen before a given point in a language phrase), so the expression may, after all, be well defined.
The scaring part is that a random C programmer like me, with over 20 years of exercise in the language, could be baffled by such expression and unable to tell apart the result of such a line.
And that’s why the second buzzer triggered. Why young blank minds have to be troubled with such visions? If a program contains one expression like that chances are high that it is a bug. If it is not a bug you may well count it as such because you are going to have a hard time figuring out what’s going on in that code.
There are some values in programming that ought to be taught before putting hands to the keyboard. Simplicity is one of the most valuable principles – keep it simple. You have to understand it, even after months, others have to read and understand it as well.
Simplicity is meant to fight against the common distortion of the young programmer trying to assert himself considering (and unfortunately, writing) “concise, hard to read” for good code. “Hey, I bet you cannot find what this piece of code does”.
I think teachers should make serious effort, especially when dealing with languages such as C, where cryptic and short code could be easily written, to discourage one-liner approaches and striking that the great programmer is the one who is able to tackle complex subjects with simple and clear code.
Writing this could be another sign of my age…