A comment on this question: Calling A Method that returns a boolean value inside a conditional statement says that you should use !boolean
instead of boolean == false
when testing conditions. Why? To me boolean == false
is much more natural in English and is more explicit. I apologise if this is just a matter of style, but I was wondering if there was some other reason for this preference of !boolean
?
|
||||
When I see a line like They're identical in meaning, but the former is closer to how the equivalent English sentence would be constructed. |
|||||||||||||||||||||
|
Writing
Then why not
Or why not
The moral of this story is that if |
|||||||||||||||||
|
In C and some similar languages, comparing boolean expressions for equality to In C any scalar expression (numeric or pointer) can be used in a boolean context, for example as the condition of an This means that
and
do not mean the same thing. The first is true if For example, the It happens that C++ has slightly different rules; for example, its Some other languages have what one might call better behaved booleans, such that And often you can avoid the issue and improve clarity by rearranging the code slightly. For example, rather than:
you might write:
That's not always better, but it doesn't hurt to look for opportunities where it is. Summary: In C and C++, equality comparisons to |
|||||||||
|
The two are functionally identical, so which one to use is a matter of taste. The major reason that I use Having been bitten severely by this, I've made a habit of making it very clear when testing for false. Had the operator been named |
|||||||||||||||||||||
|
If
Compare that to
That said, I agree that the single, slender For languages that require
Notice that every programmer should translate this automatically, without second thought, to “not condition” in their head. Acquiring this kind of fluency in reading code idioms is among the first steps in becoming a good programmer. |
||||
|
When I see |
|||||
|
because sometimes you might write |
||||
|
|
|||||||||
|
In (much) older compilers, I believe they would break (boolean == false) into 2 register assignments and a compare code in machine language. The first example would be broken into one assignment and a NOT operator. In terms of performance, the compare operation would take a number of clock cycles, depending on the size of the register being compared, compared to a bitwise invert (1 clock) and would be slower to execute. That being said, I believe newer compilers do away with this, so it should be OK to go with either. |
||||
|
At work I often am dealing with Booleans which can be null so I'll often code this case as
because I personally feel the symmetry makes it easier to read. Especially if there are other Booleans being tested as well. I don't really care one way or the other. |
|||||||||||||||||
|
When you're testing the true condition, it makes sense to do just For a C/C++/Java/etc. programmer, the meaning of the '!' operator is completely assimilated, to the point that we automatically have 'not' in our minds when we see it. So having Aside that, it's always preferable to test boolean values without comparing them with literals. For instance, it's dangerous to test |
||||
|
Size matters ;) In mixed expressions it is easier to read:
And especial for ruby an example where it is a big difference:
nil is not false, but it is also not true. |
||||
|
Based from my experience and the answers from my quesion that you have linked to. Some people prefer to use if(condition), for the reason is that one it is shorter to write. and for me it actually makes sense, for example (! isValidated()) I read this as Not Validated. but for me it's all based on personal preferences, and it depends on the logical structure of isValidated() method, if it either returns true or false |
||||
|
If you named your variable properly, then |
||||
|
For some folks, the sooner a meaning is expressed the better. For those folks having "if ! ..." compares faster to "if ..." then having to read through the entire condition (which could actually be pretty long, e.g. (thisThing = thatThing or something = the other thing) OR (thinga = thingb and thinga = thingd), etc.) just to find the ==false at the end. Having the ! (I actually prefer a not when the language allows it) right up front gets the english 'not' for this condition in there sooner. This issue also leads to consideration for using |
||||
|
For precisely the same reason that one doesn’t (normally) query “Is it false that it is time to get up?” It is periphrasic. |
||||
|
It is just the matter of style. !boolean is more confusing to me. I can say that code bool working = true; if (working == false) is much more readable than if (!working) I got GPA of 4.0 in Software-Engineering-I class. |
|||||||||||||||||
|
boolean == true
: it doesn't make sense. Expressions insideif
statements are just that: expressions. If something already evaluates to a boolean expression, why would you add a check to force it to evaluate to that? – Maxpm Feb 25 '12 at 20:43!boolean_variable
is the way most C programmers do it, I agree. – Keith Thompson Feb 25 '12 at 23:06boolean != true
? – user34530 Feb 26 '12 at 0:00