Which is clearer form: if(!value) or if(flag == value)?
Solution 1:
if (!value)
is easier/faster to follow. Subjective as you said. As long as you are consistent, this is the main thing.
EDIT
One other point to add - omitting the true/false keywords should also (hopefully) force the coder to use better named variables. Bool variables should always indicate meaning or state purpose, such as:
if (MyWallet.IsEmpty)
There is no reason with the above to use == false
or == true
as it's redundant. The above is human readable immediately.
Far better than having to decipher:
if (MyWallet.EmptyStatus == true)
or something ridiculous like this.
Solution 2:
I personally like
if ((value == false) == true)
...
cause this is verifying that the statement value is false
is actually evaluating to a boolean true...
and, then, obviously, covering both posssibilites adds even more clarity,
if ((value == false) == true && (value == false) != false)
<grin/>
and for those of you who are real gluttons for clarity, and demand incontrovertible readability, I'd suggest
if (((value == false) == true && (value == false) != false) == true)
But seriously, and I just thought of adding this, creating appropriate and meaningful variable Names is the key to this issue. You can easily, in the class where value is declared, add a computed variable as in (say "value "is actually "LaunchMissile"),public bool AbortLaunch => !LaunchMissile
,
then all you need isif (AbortLaunch) ...
,
and then it is terse, eminently readable, and avoids the negation operator.
Solution 3:
if (!value)
This is always clearer in my opinion.
if (value == false)
I hate to say this, because it sounds kind of mean, but this normally shows that the person writing the code doesn't really understand the use of boolean values. You don't need to re-validate what a boolean is in an if statement. It's redundant.
(Personally, I would be annoyed at the person too if they named the variable value
instead of something more meaningful. I have a feeling what you posted is just psuedo code, I would definitely ding that on a review.)
Edit (in response to a comment below):
It may look trivial, but often it is a sign of much bigger things. Truthfully, most people who do use var == true etc. don't understand. It's just a fact. I'm not saying their stupid or they shouldn't be programmers just that there is probably something that they need to review and learn. The problem is that when logic gets much more complex, not understanding concepts like this can lead to much much bigger problems down the road. Some people say "it's a style." That's fine. The real question in this case is, "How is it beneficial for me to do it this way? What do I or other people get from it?" If you can't solidly answer that question, then you need to ask yourself "Why is this a good idea?"
Solution 4:
I would never use if(value == true)
, so just for consistency I would also not use if(value != false)
.