When you're testing the true condition, it makes sense to do just if (condition)
, especially when you apply the convention of naming boolean variables beginning with 'is': if (isOpen)
is perfectly clear and using != false
would be redundant.
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 if (!isOpen)
is as clear as if (_NOT_ isOpen)
for me. But you're not familiar enough, in C/C++ you could create a macro with #define _NOT_ !
. But trust me, after a few years this is completely unnecessary.
Aside that, it's always preferrablepreferable to test boolean values without comparing them with literals. For instance, it's dangerous to test if (x == true)
because a boolean value is considered true if it's not zero, and the literal true has just one specific value, so x could be 'true' (i.e. nonzero) and still the comparision evaluate to false (because it contains 2 and the literal true is, say, 1.) Of course that doesn't apply to a comparision with false, but if you don't use it when testing for true, why use it when testing for false?