views:

177

answers:

9

I was wondering what was the most clean and understandable syntax for doing condition checks on nullable bools.

Is the following good or bad coding style? Is there a way to express the condition better/more cleanly?

bool? nullableBool = true;
if (nullableBool ?? false) { ... }
else { ... }

especially the if (nullableBool ?? false) part. I don't like the if (x.HasValue && x.Value) style ...

(not sure whether the question has been asked before ... couldn't find something similar with the search)

A: 

Looks OK to me, quite readable. Short and to the point.

devstuff
A: 

I think its up to you. I certainly think the .HasValue approach is more readable, especially with developers not familiar with the ?? syntax.

The other point of a nullable boolean type is that it is tristate, so you may want to do something else when it is just null, and not default to false.

James Westgate
+1  A: 

You may not like it, but personally I find

if (x.HasValue && x.Value)

the most readable. It makes it clear you are working with a nullable type and it makes it clear you are first checking whether the nullable type has a value before acting on it conditionally.

If you take your version and replace the variable with x also it reads:

if (x ?? false)

Is that as clear? Is it obvious x is a nullable type? I'll let you decide.

Dan Diplo
afaik, ?? only works on nullable types. Plus the variable should have a nicer name than x :)
FireSnake
By "nullable type" I meant specifically System.Nullable types. Any reference type can be null. Also, if you need to use the type of a variable as part of its name then that is indicative your code isn't clear.
Dan Diplo
+3  A: 

If you want to treat a null as false, then I would say that the most succinct way to do that is to use the null coalesce operator (??), as you describe:

if (nullableBool ?? false) { ... }
Oded
+5  A: 

Use extensions.

public static class NullableMixin {
    public static bool IsTrue(this System.Nullable<bool> val) {
        return val ?? false;
    }
    public static bool IsFalse(this System.Nullable<bool> val) {
        return !val ?? false;
    }
    public static bool IsNull(this System.Nullable<bool> val) {
        return !val.HasValue
    }
    public static bool IsNotNull(this System.Nullable<bool> val) {
        return val.HasValue
    }
}


Nullable<bool> value = null;
if(value.IsTrue())
 ... 
Random
Your `IsNotNull` extension method is incorrect.
Oded
fixed .........
Random
What if you want to consider `null` as `true` ?
Thibault Falise
IsTrue() | IsNull().. :) I reproduced logic how SQL works with nulls. I think it is the most clean and understandable syntax.
Random
Really?! This is, like, quadruple overkill! O_o
Artiom Chilaru
It should be public static bool IsFalse(this System.Nullable val) { return !val ?? true; } to consider null as false
Michael Freidgeim
+5  A: 

How about using GetValueOrDefault, which is pretty self-explaining and allows to use whatever default you want:

if (nullableBool.GetValueOrDefault(false)) {
}
Lucero
A: 

Given enum

public enum PublishMode { Edit, View }

you can do it like here

 void MyMethod(PublishMode? mode)
    {
       var publishMode = mode ?? PublishMode.Edit;

//or
       if (mode?? PublishMode.Edit == someValue)
       ....
    }
Gopher
+1  A: 

I think a lot of people concentrate on the fact that this value is nullable, and don't think about what they actually want :)

bool? nullableBool = true;
if (nullableBool == true) { ... } // true
else { ... } // false or null

Or if you want more options...

bool? nullableBool = true;
if (nullableBool == true) { ... } // true
else if (nullableBool == false) { ... } // false
else { ... } // null

(nullableBool == true) will never return true if the bool? is null :P

Artiom Chilaru
A: 

Comment for Random's IsFalse extensions. It should be public static bool IsFalse(this System.Nullable val) { return !val ?? true; } to consider null as false

Michael Freidgeim
-1: This should be a comment, not an answer stating it's a comment.
JYelton
To leave comments I needed 50+ reputation, which I didn't have at that time.
Michael Freidgeim