views:

228

answers:

7

Hi,

I hope this hasn't been asked before.

I have a nullable boolean called boolIsAllowed and a if condition like so:

if(boolIsAllowed.HasValue && boolIsAllowed.Value)
{
 //do something
}

My question is this good code or would I be better separating it into a nested if statement? Will the second condition get checked if boolIsAllowed.HasValue is equal to false and then throw an exception?

I hope this question isn't too stupid.

Thanks in advance.

A: 

you can just do this:

if(boolIsAllowed.GetValueOrDefault(false))
{

}

But your original code would not throw an exception, because if the first test fails, then the whole test bunks out because && is 'and also', so if the first test is false, there's no way the test can succeed.

Andras Zoltan
+5  A: 

What about:

if (boolIsAllowed ?? false)
{
}
Philippe Leybaert
A: 

The second operand is only evaluated if the first operand evaluates to true. There is no need to nest if statements.

David Neale
+8  A: 

You can check for true value even if it's null:

bool? val = null;
if( val == true ) // Works
{
  //do something
}
simendsjo
Good one, but one should make sure to understand the [details](http://stackoverflow.com/questions/447408/why-do-nullable-bools-not-allow-ifnullable-but-do-allow-ifnullable-true).
0xA3
+17  A: 

It's fine as is. The second condition won't be checked if HasValue is false, so it won't throw an exception. It's like this sort of thing:

string name = ...;
if (name != null && name.Length > 5)

Again, that's fine - you won't get a NullReferenceException if name is null, because && is short-circuiting.

Likewise the || operator is short-circuiting, but in the reverse way - there, if the left hand operand is true, the overall expression evaluates to true without checking the right hand operand. For example:

// Treat null as if it were an empty string
if (name == null || name.Length == 0)

EDIT: As noted in comments, this only applies to && and || - it doesn't apply to & and |, which always evaluate both operands.

Jon Skeet
OMG - Jon Skeet answered my question and said my code is fine...this day is epic!! :) In all seriousness, thanks.
Zaps
You know that, omniscient as he might seem, isn't actually a god?
David Neale
BLASPHEMY! *SCNR*
Bobby
Josh Smeaton
Also, the forlorn XOR (^) operator doesn't have a short-circuiting version because you can't decide the outcome of the operation just by looking at the left operand.
Jordão
A: 

You are safe doing that. C# short-circuits boolean expressions, that is why:

if (list != null && list.Count > 0)

Works. The code will not bother trying to evaluate the second condition because it knows it cannot possibly be true as the first result was false.

Not all languages do this, lots do. In VB.NET you have to do it explicitly with OrElse and AndAlso.

Adam
+1  A: 

More generally if you have a multiple conditions in your if statement consider extracting them into a method. This is not really necessary in this specific instance as some of the other answers have demonstrated. But it can be a lot simpler in more complex cases. Would you prefer to maintain:

if (taxApplied && taxValue > minimumTax && customerIsPreferred)
{
  // Do something
}

or

if (CustomerGetsTaxRebate())
{
  // Do Something
}
AbstractCode