3

I m having trouble while writing if statements as follows:

public void foo(List<String> list)
{
     if(list == null || list.isEmpty()){
        // something
     }

     // else
}

here is what bothers me: if list is null list.IsEmpty() would fail (throw exception, i m calling a method on a null object), because list is null. However, since I m doing || (or) if list is null (which is true) , it would execute the second block. which would throw exception.

is this correct? in english, if list if null or empty makes sense. is this the case here?

WHat about for && ? or short circuits?

Thanks.

kurtzbot
  • 512
  • 6
  • 19
Foo Bar
  • 141
  • 1
  • 1
  • 9

4 Answers4

28

here is what bothers me: if list is null list.IsEmpty() would fail (throw exception, i m calling a method on a null object), because list is null.

No it won't. The || operator in both C# and Java is short-circuiting. If the first operand is true, the second operand won't be evaluated.

See section 15.24 of the Java Language Specification, and section 7.12 of the C# 4 specification for details.

The && operator is similar, except this time it won't evaluate the second operand if the first operand is false. So it's fine to write:

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

as the opposite condition.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
6

The || and && operators do short circuit evaluation. It stops evaluating operands as soon as the result is known.

That makes it possible to use a value after a null check. If the second operand is evaluated, you know the state of the variable. It's more often used with the && operator than the || operator:

if (x != null && x.something == 42) ...

The | and & operators doesn't do short circuit evaluation, so both operands are guaranteed to be evaluated.

This could be useful if you do something in the conditions, and you need both things to be done:

int x, y;
if ((x = GetValue()) != 0 & (y = GetValue()) != 0) {
  // use x
  // use y
}

However, having the conditions have side effects is often a code smell, so code like this is not used very often.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

Generally the machine will evaluate the leftmost condition first, and then shortcut the second if it can.

A good rule of thumb is, if the truth can be determined completely by the leftmost being a certain value, (in your case, if list==null evaluates to true), than it will ignore the right part.

  • 1
    The compiler applies no particular analysis here. It emits code which *always* evaluates the first operand, and if that returns `true` it *never* evaluates the second operand. It's not like it's working out whether the value can possibly be null... – Jon Skeet Nov 05 '12 at 19:58
2

here is what bothers me: if list is null list.IsEmpty() would fail

Answer is NO

When || i.e. OR operator is used then second condition is only evaluated when the first is failed. When the first condition is true, second condition is not evaluated. Its called Short Circuit behavior.

This is because of the fact that :

  true || any value is always result into true

Same happens with && (AND) but in opposite condition i.e. if first condition fails then second condition is not evaluated as :

   false && any value is always going to be false.

Hope this helps you understand the behavior.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73