8
private void test() {           
        // Nested if block 
        if (true) { // If condition is true
            if (true) { // If condition is true
                if (true) { // If condition is true
                    if (true) { // If condition is true
                         // statement
                    }
                }
            }
        }

        // Above block breaking in below way
        // Breaking nested if in multiple with return statement
        if (false) { // If condition is false
            return;
        }

        if (false) { // If condition is false
            return;
        }

        if (false) { // If condition is false
            return;
        }

        if (false) { // If condition is false
           return;
        }
    }

OOPs suggest multiple return statement is bad idea. But if I used multiple nested if then its look very complex. Please suggest what is best way to use nested if condition

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • Take a look at [if-else-trees-vs-solid-principles](https://blog.mjouan.fr/if-else-trees-vs-solid-principles/) – B001ᛦ Dec 01 '17 at 11:13
  • 1
    When you nest too many if statements, that's usually a pretty clear sign that your function is doing too much. Try refactoring your method into smaller parts (methods) which each have their own specific functionality. – nbokmans Dec 01 '17 at 11:13
  • 4
    "OOPs suggest multiple return statement is bad idea." I find that's overhyped dogma, personally. It *can* be an indicator of a method doing too much, but it's often the simplest way of writing a method which *isn't* doing too much. – Jon Skeet Dec 01 '17 at 11:14
  • If your method is potentially expensive to execute, it's helpful to `return` early, when a part of your validation fails. So doing `if(false){return;}` might increase the performance of your code when done at the earliest possible point in the method. – QBrute Dec 01 '17 at 11:15
  • 1
    Possible duplicate of [What is better ? Multiple if statements, or one if with multiple conditions](https://stackoverflow.com/questions/5259938/what-is-better-multiple-if-statements-or-one-if-with-multiple-conditions) – Julien Lopez Dec 01 '17 at 11:27
  • I think it's functional programming which suggests multiple returns is a bad idea, not sure OOP does. – Peter Lawrey Dec 01 '17 at 11:54

3 Answers3

6

Multiple return statements is not good or bad in itself.
Early return and fail fast principle are not bad ways of coding and yet they rely on multiple return statements.
A single thing matters : the consistence and the code readability.

Your second way uses the "early return" pattern :

if (false) { // If condition is false
    return;
}

if (false) { // If condition is false
    return;
}

if (false) { // If condition is false
    return;
}

if (false) { // If condition is false
   return;
}

Now nothing prevents you from improving this code by removing the duplication : if (...) returns ...;.
Abusing of these can indeed finish to produce a less readable code because too noises and scattered rules.

Your example is really too abstract to provide a specific answer.
So, suppose that multiple of your conditional statements rely on a object of a specific class to be evaluated.

In this case, you could store conditional statements in a List and iterate on it to apply each rule. As soon as one is not valid, you exit.

List<Predicate<MyObject>> predicates = Arrays.asList(MyObject::isValid, 
                                                    (m)-> m.getColor()==BLUE);

And now apply rules :

MyObject myObj = ...;
for (Predicate<MyObject> predicate : predicates) {
    if (!predicate.test(myObj)) {
      return false;
    }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
2

Understand that no rules are cast in stone. (Software) engineering is always about balancing different requirements/rules.

In your case: option 2, using multiple return statements is clearly much easier to comprehend for human readers.

In that sense: go for that. As this solution also works much better in regards of the single layer of abstraction principle!

But I also agree with the comments - a lengthy sequence of if/else checks is most often a symptom of insufficient design. One doesn't need to completely abandon ifs (see here) - but assuming that your main concern is to improve the quality of your current code you should rather invest your time looking into that question why are there so many if statements in that method.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Another option:

if (condition || condition || condition)
    return;

or either

if (condition && condition && condition) {
    statement's
}

obviously each condition may be on its own line It just depends on the conditions, what exactly is being done, ... and somehow also on what you want to 'express'.

user85421
  • 28,957
  • 10
  • 64
  • 87