On Wednesday 10 September 2008 11:34:03 am Juan Lang wrote:
dwMediaTypeSize + (dwMediaTypeSize < 2 ? 1 : dwMediaTypeSize / 2)
I'm pretty sure this would come out as: dwMediaTypeSize + (dwMediaTypeSize < (2 ? 1 : dwMediaTypeSize) / 2)
But even if it doesn't, I don't think it'd hurt to be more explicit: dwMediaTypeSize + ((dwMediaTypeSize < 2) ? 1 : (dwMediaTypeSize/2))
I'm pretty sure this would come out as: dwMediaTypeSize + (dwMediaTypeSize < (2 ? 1 : dwMediaTypeSize) / 2)
It doesn't, check the order of operations again.
But even if it doesn't, I don't think it'd hurt to be more explicit: dwMediaTypeSize + ((dwMediaTypeSize < 2) ? 1 : (dwMediaTypeSize/2))
Why use two parentheses when 6 will do, eh? Sorry, I don't agree that this is clearer. --Juan
On Wed, Sep 10, 2008 at 01:38:03PM -0700, Juan Lang wrote:
I'm pretty sure this would come out as: dwMediaTypeSize + (dwMediaTypeSize < (2 ? 1 : dwMediaTypeSize) / 2)
It doesn't, check the order of operations again.
But even if it doesn't, I don't think it'd hurt to be more explicit: dwMediaTypeSize + ((dwMediaTypeSize < 2) ? 1 : (dwMediaTypeSize/2))
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). I get peeved that gcc always warns about && and || - where the order is obvious and correctly defined.
Blindly putting parenthesis around conditional expressions leads people to write: dwMediaTypeSize + (dwMediaTypeSize < 2) ? 1 : dwMediaTypeSize / 2 which is unlikely to have the desired effect!
David
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).