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_开发者_StackOverflowELEMENTS) || (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?
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.
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.
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 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.
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.
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.
精彩评论