Ged schrieb:
James Hawkins wrote:
The parentheses are not needed? You better check
your C book for
operator precedence. && has higher precedence than ||, so in this
case the parentheses are needed. This is also why using parentheses
is a good idea, because you never know if the original author has any
idea about operator precedence. The use of parentheses makes the
original intention clear to other authors.
An absolutely perfect example which nicely backs up my original argument.
I feel I now have nothing more to add. :)
Ok, but I have.
The example that I fucked up, was an example where I wanted to say that
parantheses are useful.
Whereas I think in the case of || and && they should not be added as you
can break a warning.
The arguments that have been brought up pro parenthesing are perfectly
valid (except Alex' one maybe :)) when it comes to generic use of
parentheses. But none of them is valid is this special case.
If you want to generalize this ad infinitum, let's start adding
parentheses around other things, too.
What about
printf(Format, String + strlen(Prefix), i + 1);
You better add some parentheses around (i + 3) so it won't be mistaken for
printf(Format, String + (strlen(Prefix), i) + 1);
What about this:
(Callback->RoutineBlock).Object = NULL;
ExfReleaseRundownProtectionEx((&(CallbackRoutineBlock->RundownProtect)),
((Value.RefCnt) + 1));
NewValue.Object =
(InterlockedCompareExchangePointer((&(FastRef->Object)),
(NewValue.Object),
(Value.Object)));
for (((Entry = (Head->Flink)), (i = 0)); ((i < (Info->Depth)) &&
((Entry->Flink) != NULL)); ((i++), (Entry = (Entry->Flink))))
Looks like crap, doesn't it?
You need to draw the line somewhere.
IMO there's no use in adding parentheses around something when any
different parenthesing wouldn't make sense.
You cannot mistake if (a < 1 && b < 2) for if (a < (1 && b) <
2) as the
second doesn't make sense.
Now I'd just like to see only one concrete example of when adding
parentheses around comparisons seperated by || / && has an advantage
over not adding them.