Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Simplify conditional ternary expression"

Tags:

var foo = context.FOOTABLE.FirstOrDefault();  var bar = foo != null ? foo.SomeBool : false; 

Resharper tells me to Simplify conditional ternary expression. But I feel like a null check is necessary here, since FirstOrDefault() can return null.

So, who's wrong, me or Resharper?

like image 585
Johan Avatar asked Jan 31 '13 09:01

Johan


1 Answers

First, a full example:

class Foo {     public bool SomeBool { get; set; } }  class Program {     static void Main(string[] args)     {         var foo = MaybeFoo();          var bar = foo != null && foo.SomeBool;      }      static Foo MaybeFoo()     {         return new Random().Next() < 10 ? null : new Foo();     } } 

Here MaybeFoo is a method that sometimes returns null and sometimes returns a Foo. I've used Random so that R# doesn't automatically work out that it's always null or not-null.

Now, as you say on this line:

        var bar = foo != null ? foo.SomeBool : false; 

R# offers the inspection Simplify conditional operator. What does it mean by this? Well, as usual, we can Alt+Enter and accept the suggestion and see what it wants to replace it with, which in this case is:

        var bar = foo != null && foo.SomeBool; 

Now, as to your concern:

But I feel like a null check is necessary here, since FirstOrDefault() can return null.

So, who's wrong, me or Resharper?

Well, to put it shortly, you are. There's still a null check taking place here, and the && operator is short-circuiting, so the second operand (foo.SomeBool) will only be evaluated if the first one is true. So there won't be a NullReferenceException in the case when foo is null; the first check will fail and bar will be assigned false.

So the two lines

        var bar = foo != null ? foo.SomeBool : false; 

and

        var bar = foo != null && foo.SomeBool; 

are semantically equivalent, and R# as usual prefers the more concise version (in particular, explicit trues and falses in conditionals are often a mark of redundant code). You may not, in which case you could turn off this inspection.

like image 122
AakashM Avatar answered Sep 29 '22 14:09

AakashM