a style question

Karl Heuer karl at haddock.ima.isc.com
Tue Oct 2 12:50:09 AEST 1990


In article <1990Sep30.194448 at IASTATE.EDU> john at IASTATE.EDU (Hascall John Paul) writes:
>[Re ++x vs x++] I find myself using ++x (which in my mind I attribute to some
>long gone, brain-damaged optimizer from my past -- but I still persist).

I also prefer ++x, not because it's more efficient but because it's the
conceptually simpler of the two: ++x is by definition x=x+1, while x++ is
defined as (temp=x, x=x+1, temp).  (Of course the arguments are the same:
the reason for suspecting it *might* be less efficient is because it has
the more complex semantics in the general case.)  So ++x wins under the rule
of "use the least powerful tool that does the job".  (Btw, this is the same
rule that makes GOTO a bad idea in most circumstances.)

>My `favorite' is:  --x;  and  x++;  (pre-decrement, post-increment!!!)

That's the other side of the coin.  Many algorithms turn out to be simpler
when described in terms of half-open intervals like [0,N).  Stepping through
such an interval is best done with post-decrement (*p++ or a[i++]) and
pre-decrement (*--p or a[--i]), and it's reasonable to use this convention
even when the value isn't being used (e.g. "if (*p == '-') p++", where the
"p++" is conceptually "(void)*p++").

The important point: it's not worth fighting over.

Karl W. Z. Heuer (karl at kelp.ima.isc.com or ima!kelp!karl), The Walking Lint



More information about the Comp.lang.c mailing list