20

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!

38
0

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)
share | improve this answer | |
  • 7
    Good call on the nullable booleans! – 5StringRyan Sep 23 '11 at 23:29
  • 4
    for Nul,able<bool> you can use an extesion method provided by the framework varaible.GetValueOrDefault() aswell – Miguel Apr 4 '16 at 17:57
  • Disagree - For coding readability == true/false is clearer than empty/! – N.D.B Jun 5 '17 at 7: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 2 '18 at 20:29
4
0
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? myBoolif (myBool == false){  // Do stuff here...}
share | improve this answer | |
3
0

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.

share | improve this answer | |
  • 2
    In languages with type coercion, you would probably want to use === – Sky Kelsey Sep 23 '11 at 23:26
  • .... Provided that you want to rely on type coercion for the specific conditional – Chris Laplante Sep 23 '11 at 23:27
  • 3
    @SkyKelsey: Might as well just say "in Javascript" :P. – user541686 Sep 23 '11 at 23:31

Not the answer you're looking for? Browse other questions tagged c# coding-style or ask your own question.