views:

66

answers:

5

I recently read in Code Complete that the recommended way of handling expressions that involve numbers is to order them like a number line.

The book has 2 examples:

if ( (MIN_ELEMENTS <= i) && (i <= MAX_ELEMENTS) )

if ( (i < MIN_ELEMENTS) || (MAX_ELEMENTS < i ) )

With the first example showing that i is between the min and max elements, and the second example being that i falls outside the range between the elements.

I've been trying to adopt it, and I'm not sure if it's just the way I think, but I don't think it's making code any clearer.

Example:

 if (m_Health > BOSS_HALF_HEALTH) // The way it was

 if (BOSS_HALF_HEALTH <= m_Health) // The "number line" method

Is it just me, or does the number line method seem less clear? What are your thoughts regarding this practice?

Edit: It's also odd that he mentions putting constants on the left side of comparisons contradicts the number-line-method, but here it seems that the number line method leads to putting the constant on the left side.

A: 

I think this is not a method that is inherently superior. The advantage comes in having the expressions the same way throughout the code, making the code faster to read when you're used to it.

xpda
+1  A: 

I think it should be written the way you expect to read it.

Thus I would do:

if (i > MIN_ELEMENTS &&
    i <= MAX_ELEMENTS)

instead of:

if ( (MIN_ELEMENTS <= i) && (i <= MAX_ELEMENTS) )

Because I read C alike english, like

if i is less than min and more than max

instead of

if min is less than is and is is less than max

because I care about i, not min.

I know, we're talking about readability here.
Anonymous
I think the question is more about comparisons than assignments.
xpda
I'm sorry about not reading the question correctly the first time.
+2  A: 

I think the original motivation comes from having more than one comparison in the same logical expression. Both the quoted examples are comparing between both a lower and upper bound of a range. This ordering method may have value in those situations.

However, I don't think it's necessarily applicable if you're testing a single condition, such as m_Health > BOSS_HALF_HEALTH. In that case, the comparison you're making is whether something (a variable) is greater than something else. That's perfectly logical and doesn't need to be ordered in any particular way.

If you always ordered your comparisons in a "number line" way, you would never even need the > or >= comparison operators. They exist for good reasons.

Greg Hewgill
Yes, even with multi part conditionals I think it makes more sense deal with the "conditional element" first rather than the constant, simply because that's how (some?) humans think.
Anonymous
Right, I don't use the style of putting the constant first in any case, because my compiler warns me if I do that (of course it wasn't always like that, but it's true today). I find that using the variable on the left of the comparison reads much more smoothly.
Greg Hewgill
And, for sane languages that don't permit assignments inside expressions, the original reason for putting the constant on the left isn't even applicable.
Greg Hewgill
A: 

Steve McConnell's work is superb but there are little things I don't agree with him on. This may be one of those things with you.

If you feel it's making your code less clear, then don't do it just because Steve McConnell thinks it's the way to go.

John at CashCommons
Totally agree with you here. I just wanted to make sure it wasn't just me, and that it's much more clear to others.
Anonymous
A: 

Hi Thomas,

I guess the original intention is to just make a habit.

Language is just a familiar arrangement of words. Once it becomes a habit, you get accustomed to it.

If you can train your brain to read:

if ( CONST == i )   //[1]

in the same way as:

if ( i == CONST)    //[2]

you will never fall prey to the bug of:

if ( i = CONST)     //[3]

However, it should be noted that most modern compilers today give a warning on construct [3]

In conclusion, if you are fix all your compiler warnings, you can use either of the coding style.

Meera