C programming style
Stew Rubenstein
stew at harvard.ARPA
Mon Jul 15 08:36:46 AEST 1985
Summary: i++ leads to confusion, but ++i is often an excellent alternative.
In article <480 at mmintl.UUCP> franka at mmintl.UUCP (Frank Adams) writes:
>
>I agree that "i++" is an abomination. (I do use it, however, to be
>consistent with the rest of the code I work with.) Actually, C has
>a third way to represent this operation: "i += 1". Personally, I
>think this is the superior notation. It is concise, yet easy enough
>for a person unfamiliar with the language to interpret.
One opinion that I haven't heard expressed: I, too, agree that "i++" is
not the greatest. But why do people always use this postfix form when
the prefix form, "++i", is available? From my vantage point as a long-
time programmer, it is hard to say with certainty, but isn't it a whole
lot more clear that ++ is an operator and is doing something to "i" in
this case?
The only excuse for i++ is that you want to use the value before
incrementing. A good compiler, or a very bad one, might generate better
code in some cases where i++ is used for its value, but frequently this
results in the program doing extra work to stuff the old value in a
temporary until the expression is evaluated! There are some common
constructs in which i++ (or ptr++) is useful, but in the hands of many
programmers, this construct leads to confusing statements performing
two unrelated functions.
>Incidently, "i = i + 1" is not all that obvious, either. Many people's
>response, on seeing such an expression for the first time, is "impossible.
>'i' can't equal 'i + 1'.".
Once a beginning programmer learns about assignment statements, this is
a non-issue. It would be nice if we had chosen some other character,
but someone decided that underscore was more important than left-arrow
back in the 60's, and now we're stuck.
More information about the Comp.lang.c
mailing list