0

I often structure my code like this:

public void doSomething() {
    if (!condition1) {
        println("problem 1");
        return;
    }
    if (!condition2) {
        println("problem 2");
        return;
    }
    if (!condition3) {
        println("problem 3");
        return;
    }

    // code to run if
    // everything is OK
}

rather than nesting like this:

public void doSomething() {
    if (condition1) {
        if (condition2) {
            if (condition3) {
                // code to run if
                // everything is OK
            }
            else {
                println("problem 3");
            }   
        }
        else {
            println("problem 2");
        }
    }
    else {
        println("problem 1");
    }

}

Is there any benefit to one over the other? Is one more "correct" than the other? Thanks!

5
  • public void doSomething() { if (condition1 && condition2 && condition3) { // code to run if // everything is OK } else if(!condition1) { println("problem 1"); } else if (!conditoin2) { println("problem 2"); } else { println("problem 3"); } } Commented Jul 26, 2013 at 5:17
  • Changing branching structure will not lead to performance improvement IMO. Rather focus on Readability compared to such micro-optimization. Search for some major optimization opportunities in your code. Commented Jul 26, 2013 at 5:46
  • @Juvanis, these two examples will produce the exact same output. What are you talking about? Commented Jul 26, 2013 at 15:35
  • @Veera, that will cause conditionals to be evaluated more than once. I am dealing with moderately expensive conditional operations, so that would be a bad idea. I only want to evaluate condition2 if condition1 was true. Commented Jul 26, 2013 at 15:38
  • @Juvanis please see here for proof that this code produces the same output: ideone.com/LGTgP3 Commented Jul 26, 2013 at 15:50

4 Answers 4

1

You should profile your code, see which condition is true most of the times. If there is a significant discrepancy in the number of times each condition is true, then by all means use the first structure, arranging your code to take advantage of your domain-specific input. This may gain you some significant performance benefit.

Sign up to request clarification or add additional context in comments.

Comments

0

whenever i see multiple if's, i think of moving to switch statements, for more readability..and avoid multiple repeated ....if words....

1 Comment

its because the code in satisfying condition might run to 100s of lines of code, and becomes very difficult to trace the starting and ending braces...
0
if(condition1){

} else if(condition2){

} else if(condition3){

} else {

}

or use switch statements. There's no significant performance benefit here. Use the one that is more readable/suitable in your situation. One more thing I should add is that in some cases switch statements are faster as with if..else you will have checking of different clauses until it reaches the one matches or there's no match at all.

5 Comments

This could also work, but sometimes I want to do things right before an if-statement (like declare a variable), so I use multiple ifs and returns instead of else if.
Here is an example of why I think I should do this: pastebin.com/22hx0NrR please advise.
@BLuFeNiX It looks a bit scattered to me. It's ok to use multiple returns/exits in a method but it can be abusive and needs a little extra care..imagine you forgot to put return statement in one of your if's in the code and then it would print out "Everything OK!" when it is not.
Yes, I understand that it is more typo-prone, but I think the code is much easier to read when I have 10 conditions to be met, each requiring their own setup before the conditional can be run. Perhaps I'll make a ConditionEvaluator class to get the best of both worlds.
@BLuFeNiX Don't make another class for that..for fewer evaluations, you can use either if..return or if..else if..etc. Should be fine either way.
0

Either code structure is fine, but having multiple return statements adds another layer of complication when debugging, and can be more prone to errors when typing and/or editing code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.