On Friday, 18 January 2013 at 10:49:52 UTC, Jonathan M Davis
wrote:
On Friday, January 18, 2013 11:30:32 deadalnix wrote:
If you believe you know operator precedence, you are probably
wrong. And even if you are right, most other programmer don't.
You really should know the precedence of at least the common
operators. For
instance, I don't think that there's much excuse for code like
if((a == b) && (c != d))
The parens just clutter the code, and any programmer worth
their salt should
know that the comparison operators has higher precedence than
the logical
operators. And programmers should definitley know the
precedence of the
arithmetic operators, such that if they're putting parens
around ever
expression using + or *, then there's a problem.
On the other hand, I can totally understand if someone is
nervous about doing
stuff like
if(++*var == a)
I fully expect that your average programmer won't fully know
the operator
precedence table. _I_ don't know it perfectly. But for the more
common
operators, you should know it, and using extra parens in those
cases just
makes the code harder to read.
- Jonathan M Davis
Keep in mind that us, as enthusiasts, have a warped view on the
matter.
A lot of my "programmer" colleagues don't really give a damn,
and, for them, precedence stops at arithmetic operators. I'm not
saying they are bad programmers, but they just can't be arsed
with that kind of stuff: They just don't care.
*Personally*, I prefer
if ((a == b) && (c != d))
over
if (a == b && c != d)
I *know* the precedence here, but I still find it clearer with
grouping.
HOWEVER, I 100% agree that in such code, the format chosen is
very very important. Place spaces in the wrong spots:
if ( ( a == b ) && ( c != d ) )
And the eyes begin to bleed...
As a side note, I've taken to ALWAYS placing parenthesis with
ternary operators. That thing has some nasty precedence rules...