24

Is there any sort of style consensus on the following two coding styles? I'm more curious if this is the sort of thing where one is generally preferred in good code in C#, or if this the sort of thing that gets decided when picking a style for a coding project.

Style 1: Using the ! sign to indicate a not in a conditional

if (!myBool)
{
  //Do Stuff...
}

Style 2: Using == false to indicate a check for falsehood in a conditional

if (myBool == false)
{
  //Do Stuff...
} 

Thanks!

mweber
  • 1,292
  • 2
  • 10
  • 13
  • 3
    I personally prefer `if (!expr)` -- be consistent. –  Sep 23 '11 at 23:27
  • 23
    I prefer: `if (!!!!!!!!!!!!!myBool) { /* do stuff */ }` – Christopher Currens Sep 23 '11 at 23:27
  • 1
    This has been asked in various ways across SO such as: [Is it bad to explicitly compare against boolean constants e.g. if (b == false) in Java?](http://stackoverflow.com/questions/2661110/is-it-bad-to-explicitly-compare-against-boolean-constants-e-g-if-b-false-in). – Metro Smurf Sep 23 '11 at 23:29
  • We also have `if (myBool is false)` as well now. the idiom in C# is `if (!myBool)` but personally I quite like the `is false`. your eye is less likely to miss the `!` – RooN3y Jul 18 '23 at 12:55

3 Answers3

42

The normal convention is

if (!myBool)

The one place where I don't go this route is with nullable booleans. In that case I will do

if (myBool == true)
{

}

Which is equivalent to

if (myBool.HasValue && myBool.Value)
aquinas
  • 23,318
  • 5
  • 58
  • 81
  • 4
    for Nul,able you can use an extesion method provided by the framework varaible.GetValueOrDefault() aswell – Miguel Apr 04 '16 at 17:57
  • Disagree - For coding readability == true/false is clearer than empty/! – N.D.B Jun 05 '17 at 07:21
  • I like this style of nullable bool check, but others want to do: (myBool ?? false) and they say this is the default value to use for null, but i personally think that is harder to read/understand. Though, they came back and said what happens if null ends up == true someday, which is weird since null == null is false anyway. :) – Daniel Lorenz May 02 '18 at 20:29
6

I don't know of any language for which the latter is preferred. Use the former.

Warning!

There's a reason for this!

This indeed does what you expect, in most languages:

if (x == false)
    ...

But in e.g. C++, because true is just a synonym for 1 (so 2 isn't true or false), this doesn't work:

if (x != true)
    ...

although it's fine in C#.

In fact, it can also get tricky in .NET -- you can trick a boolean to take an integer value, and mess it up with bitwise arithmetic (e.g. a & b can be false when a is 1 and b is 2, even though both are "true").

In general, just use the former instead of worrying about boolean literals.

Community
  • 1
  • 1
user541686
  • 205,094
  • 128
  • 528
  • 886
3
if(!myBool)
{
  // Do Stuff here...
}

This is the preferred version, as since you already have a bool variable that contains a true or false, there is no reason to do an additional evaluation in the if statement.


Update

Based on what aquinas has stated, this format is good to use unless you do have a nullable boolean (ex: bool? myBool). If this is the case, use the former:

bool? myBool
if (myBool == false)
{
  // Do stuff here...
}
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
5StringRyan
  • 3,604
  • 5
  • 46
  • 69