C obscenities – the shortcut operators

If there is truly somewhat abhorred about the use of so many symbols in C, it is the shortcut operators. The use of == gave similar rise to horrific operators such as –, ++, && and || (the latter two need a discussion all their own). Why are they so terrible? – because of the side effects they produce, and their use by novice programmers to produce code which visually “seems” complex – even though they may often not completely understand what they are doing with these operators. Part of the issue may lie with the difficulty in determining what is happening. The ++ operator, for example can be used either before or after an operator: ++a or a++. In a singular statement, both do the same thing. When coupled with other expressions, they act very differently. For example, consider the following code:

b = x * ++a;
b = x * a++;

The first expression says “increment a by 1, and then multiply x”. The second expression says “multiply x by a, and then increment a by 1”. But ultimately it leads to people writing code like  a++ + b++ or c = a++ + ++b.  The use of these operators is problematic only in so much as they can be abused, leading to obfuscated code. Consider the following piece of code: 

int a, b;
a = 5;
b = 7;
z = a+++b;

The value of z becomes 12, because the expression is parsed as a++ + b

Those other, more palatable shortcut operators, like += likely originated from the use of plusab in Algol68, which stood for “plus and becomes”. They aren’t as bad because there is no double meaning attached to them. Legend has it that those horrible shortcut operators evolved due to the need to derive efficient machine code, which makes sense in the low-resource world of early computing. It makes little sense now.

These shortcut operators evolved due to their efficiency in equivalent assembler. Used in the right context, there is nothing intrinsically wrong with them, except maybe for the novice programmer, who may find their use perplexing.

Advertisements

One thought on “C obscenities – the shortcut operators

  1. codeinfig says:

    theyre really not *all* bad. && and || have their place, but worse is that people dont question these things. i realize that people often say “focusing on syntax is the most pointless aspect of langauge design” but its far from pointless, even if other things are more interesting.

    when you complain about these features (which are too common, imo) people say “well you dont have to use them.” that misses half the point, doesnt it? code has to be read as well as written, so any haphazard design that becomes overly common affects every coder– at least the ones that have to read code.

    thats more than sufficient reason to debate the merits of such features. few people seem to think so.

    you see a lot of posts on wordpress explaining and demonstrating these features. you almost never see a post anywhere weighing the pros and especially cons. pity that its mostly your blog, and not several others as well. do point your readers to more if you find some.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s