The death of the post/pre increment/decrement operators?
In this post
Nola wonders about the lack of the post/pre increment/decrement operators in
Ruby.
I’m by no mean an expert of Ruby (Total lines of code
written in Ruby: 2), but I can certainly say that I’ve noticed this
situation in other languages and tools that I’m using. Both C# and Java
support the ++/-- operators to add/decrement one from a number. The root of
this operator is from the days of C, when the compiler wasn’t smart
enough to do optimizations.
The whole idea was that i++; would be translated to INC I, a
single CPU instruction. The issue wasn’t being developer friendly or
anything like it. It was all about getting the correct output from the
compiler. Remember, C was built in the days where people wrote whole OS using
ASM. It had to allow high level constructs while allowing the optimizations
that the programmers already knew and the compiler just couldn’t supply those
optimizations.
Fast forward to today, I would like an honest opinion about
it, how many of you use these operators outside of a for() loop? Let’s
see the possible use cases:
- I++; è On a
line all on its own, incrementing the variable by one. This is just a
shorthand to I+=1; or I = I + 1; Many books teaches that no true C-based programmer would the
other options when they can use the first.
- arrayOfSomething[i++]; è Access
the array and increment the indexer in one line. A true time saver for
those who are debited by lines of codes. (You wrote 100 lines to do that,
WTF? J )
In the first case, I get a sense of uneasiness when I see a
line like this:
I++;
Something in me tells me that it’s not right; it looks
like an expression, not a statement. I just get uncomfortable around this code,
and I always change it to:
I+=1;
Not without some sense of guilt that now I’ve wasted
two whole characters and I’m not concise enough.
Then there is the access the array and increment the indexer
in one go:
arrayOfSomething[i++];
Invariably I’ve found that the above can cause
problems. Sure, it’s fast to write and it looks elegant, but how often
would your eyes just skip it? It’s
a side effecty code, and that is never good. Sure, the increment operator is
widely known and it’s well understood what is going on their, but I still
have hard feeling about this type of code. Invariably I would change it to:
arrayOfSomething[i];
I+=1;
I wasted a couple of characters, but I get a much clearer
code this way. The compilers nowadays are more than capable of doing these
sorts of optimizations by themselves without me holding their hands. I’m
a firm believer in throwing as much as possible on the compiler. If I’ve
two code patterns, one efficient and concise[1] and
the second less efficient yet elegant, I would almost always go with the second
until performance tuning forced me the other way.
Readable code is more important than fast
code.
[1]
It goes without saying that such a code is also probably not elegant. There is no question if both
are elegant and one is more efficient.
Comments
Comment preview