devenum: Fix order of operations bug (Coverity id 709)
chris.kcat at gmail.com
Wed Sep 10 16:32:42 CDT 2008
On Wednesday 10 September 2008 01:52:02 pm David Laight wrote:
> On Wed, Sep 10, 2008 at 01:38:03PM -0700, Juan Lang wrote:
> > Why use two parentheses when 6 will do, eh? Sorry, I don't agree that
> > this is clearer.
> Agreed, if you add too many parenthesis it gets difficult to see
> where they line up (which some tool to show you).
Any good editor should be able to highlight parenthesis and bracket pairs.
> I get peeved that gcc always warns about && and || - where the order
> is obvious and correctly defined.
The order may be properly defined, but it's hardly obvious to everyone, all of
the time. And just because you know it doesn't mean someone who'll look over
the code later will, and make an incorrect assumption based off it.
> Blindly putting parenthesis around conditional expressions leads people
> to write:
> dwMediaTypeSize + (dwMediaTypeSize < 2) ? 1 : dwMediaTypeSize / 2
> which is unlikely to have the desired effect!
That's exactly the point. It's better to be verbose than to confuse oneself.
I've personally had several cases where I thought the order of operation was
obvious (mostly with the ?: op), didn't use parenthesis, and ran into
inexplicable bugs. It wasn't until I went back over the code and added
explicit parenthesis that I found out the problem.
Of course, I never went to school to learn this stuff. All I know about
programming has largely been self-taught. But, for me at least, it helps
visualizing what happens when things are properly grouped (spacing helps, eg.
foo<2 ? 1 : bar/2, but it still leaves a question since the spacing can be
wrong given the order of operations, and give unexpected results; parenthesis
gaurantees the evaluation is as specified).
More information about the wine-devel