Then what's your "definition" of programming language abuse? ;-)
Actually re-reading I didn't quite mean "rubbish" to be so rude
while(*d++=*s++) is perfectly sound C : it is good C. It encapsulates
C IMO. How do you term it abuse? You did the accusing so you do the
explanations
No, if anything, they are too "clever". Keep it simple and
This is simple and explicit. if you can not understand
while(x--)
then you have no business programming in C. If anything your example
convolutes the problem by adding more lines and comparisons. Of course
we can argue this as a style issue but no programmer worth his salt in
my, not inconsiderable, experience would extend such a trivial C line so.
what you mean and mean what you say. It's much easier to reason about
the correctness of a program if multiple side-effects and side-effects
in expressions are avoided.
Except there are none.
while(x--) could not be simpler.
Example:
while (k > 0) {
...
}
/* Here we know that k <= 0 just by looking at the guard `k > 0'.*/
This is C, not C++.
while(x--) is perfectly readable and legitimate.
With side-effects in the "wrong" places you have to decode the meaning
in your head in an unnatural way:
`while (numThingsToDo--)' translates to something like "while
numThingsToDo is not equal to zero before we increment it and then we
increment it and do..." (phew!) There is something inherently ugly about
it and I consider it poor communication.
Most C programmers would see this immediately as "while there are
things to do". it is a bedrock example of C programming. No
embellishment or cleverness required.
`while (numThingsToDo > 0)' translates naturally to, well, the obvious.
Well, you are actually right. As the program runs the numThingsToDo
decreases, so it makes sense to decrement it. On the other hand, if we
for instance have a variable named `size' it would be unnatural to use
it as a loop counter if the size it represents doesn't change:
while (size > 0) {
...
size--;
}
This is a strawman. We are not talking about size : this would be a
nomenclature thing, not a style thing.
There is another reason for doing the decrement in the loop condition
: it is not requried elsewhere in a potentially complicated area of
code within the loop - for all intents and purposes the counter is
"const" inside the loop and program flow doesnt have to worry about
doing the decrement. e.g Imagine a "break" or a "continue" or
whatever. has the variable been decremented?
To sum up : Let the loop control look after the loop counters : it is
compact, easy to maintain and basic good programming practice.
richard.