John Powel
John Powel

Reputation: 1424

Should I separate AND with two if statements?

I have the following lines in my code:

    if (command.equals("sort") && args.length == 2) {
    //run some program
    }

Someone suggests that I should use two separate if statements because there's no need to evaluate any of the other if statements if the command does not equal to "sort"​, regardless of whether or not the args length is correct.

So according to that that, I need to rewrite my code to:

if (command.equals("sort")) {
  if (args.length == 2) {
    //run some program
  }
}

I know they both do the job, but my question is which one is better and more efficient?

Upvotes: 1

Views: 957

Answers (9)

MildWolfie
MildWolfie

Reputation: 2542

As stated, short circuit will cause the program to exit the if statement the moment a condition fails, meaning any further conditions will not be evaluated, so there's no real difference in the way the two formats are evaluated.

I would like to note that code legibility is negatively affected when you have several if statements nested within each other, and that to me is the main reason not to nest. For example:

if( conditionA && conditionaB && !conditionC ){
    // Do Something
}

is much cleaner than:

if( conditionA ){
    if( conditionB ){
        if( !conditionC ){
            // Do Something
        }
    }
}

Imagine that with 20 nested if statements? Not a common occurrence, sure, but possible.

Upvotes: 2

boskop
boskop

Reputation: 619

yeah, their suggestions are completely right. What I suggest you is to write the first check as:

"sort".equals(command)

Maybe it does not make sense in this case but in future. Use the static type first so you never need a null check before

Upvotes: -1

user1898956
user1898956

Reputation: 46

That's not really the question but note that if you want the two if evaluated, you can use & :

if (methodA() & methodB()) {
    // 
}

instead of

boolean a = methodA();
boolean b = methodB();
if (a && b) {
    //
}

Upvotes: 0

rduga
rduga

Reputation: 114

Actually, it is called [Lazy_evaluation]: http://en.wikipedia.org/wiki/Lazy_evaluation

Upvotes: 0

Srujan Kumar Gulla
Srujan Kumar Gulla

Reputation: 5851

short circuiting is better which is done by && if you are check null case for a value and then apply a function on that object, short circuit operator works well. It stops from condition 2 to be executed if condition 1 is false.

ex:

String s=null;

if(s!=null && s.length())

This doesnt throw exceptions and also in most cases you save one more if check.

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213401

Well, since && is a short-circuit operator. So both the if statements are effectively the same.

So, in first case, if your command.equals("sort"), returns false, the following condition will not be evaluated at all. So, in my opinion, just go with the first one. It's clearer.

Upvotes: 7

Mike
Mike

Reputation: 49523

If the conditions are in the same order, they are exactly the same in terms of efficient.

if (command.equals("sort") && args.length == 2)

Will drop out if command.squals("sort") returns false and args.length will never be checked. That's the short-circuit operation of the && operator.

What it comes down to is a matter of style and readability. IMO When you start chaining too many together in a single if statement it can get hard to read.

Upvotes: 0

Peter Cetinski
Peter Cetinski

Reputation: 2336

They are the same. For your first example, any modern runtime will ignore the second expression if the first expression is false.

Upvotes: 1

Roozbeh Zabihollahi
Roozbeh Zabihollahi

Reputation: 7347

No, that's not true. They call it short circuit, if the first condition evaluates as false, the second one would not be evaluated at all.

Upvotes: 14

Related Questions