For some of my recent source, I just did
grep "+= 1;" */[a-z]*.c
I was surprised at how many instances there were!
A large number were of the form
csp->cu_probe += 1;
or, in a more extreme example,
Xtab[nix].xnum[strlen(Xtab[nix].xnum) - 1] += 1;
Now, in the vast majority of these cases it would
make no difference whether I use pre-increment or
post-increment. But my tendency is to use
pre-increment in those don't-care cases, partly
because the post-increment is a *special* C op,
and partly because the "++" is more visible at the
*beginning*. Yet in
++foop->next->count;
the "++" is distant from the "count" which is incremented.
Hence my preference here for
foop->next->count += 1;
I hear an objection coming. Why not just sacrifice my
tiny aversion to an unnecessary post-increment and
write
foop->next->count++;
instead of
foop->next->count += 1;
*But that rewrite presupposes that " += 1" is
confusing which, quite frankly, is an opinion
I'd never heard before.*
Redoing my grep to exclude "->" and "[]", I see
I still have numerous instances of "+= 1;" in the code
I surveyed. The first ones that caught my eye
were
argv += 1;
Those of you who process your own args probably do
argv += 2;
a lot, as well as the "+= 1" form. Doesn't it make
the code more elegant and readable to use the "+= N"
consistently, rather than applying an abbreviation
only useful in one case?
And, a question for those of you who thought my
'1's should be defined MAGIC numbers: In the common
case where " argv += 2 " is what's needed, do you
actually use a defined constant rather than 2 ??
If your answer is a sincere Yes, then that's another
style point on which we must agree to disagree.
I confess that there's still plenty of "+= 1"
instances in my code other than the examples just given.
Here's one I coded just a few months ago, for
a card-game simulation:
void giveback(int rank)
{
Deck[rank] += 1;
Sizedeck += 1;
}
Do I expect I may redefine giveback() to give two
cards back at once? Not really. Did I know the
" += 1 " would confuse otherwise-expert C programmers
or that they would prefer
#define NUMBER_OF_CARDS_GIVEN_BACK 1
Definitely not.
Let me appeal to the general audience. Sometimes I
like to present my code into the public domain.
Do I really need to get rid of these "confusing" instances
of " += 1 " ?
Incrementing to walk over an array or similar structure is pretty much
generic and idiomatic. "+= 1", being used instead of the increment
operator, suggests that there is some reason not to increment, such as
a concern that the value 1 might not always be the right choice.
I just counted my instances of "+= 1". I didn't count "++" but am
rather sure it would be much more common. Anywhere, there are
many *many* things besides array walks where 1 is the standard
increment,
and many array walks where 1 is NOT the increment.
On the topic of understanding or debugging someone else's poorish
code, I've only limited experience but that experience has been
successful. I'm of the opinion that someone looking at an
uncommented "+= 1" and trying to psychoanalyze its hidden
meaning is asking for problems where none exist.
James Dow Allen