-2

As I couldn't find a question similar to my construct, I am going to ask it.

I have following code:

for (Object o : array1) {
    for (Object o2 : array2) {
        if (condition1) {
            //get a variable for condition2 (depends on iteration elements)
            if (condition2) {
                if (condition3) {
                    //do something
                } else if (condition4) {
                    //do something
                }
            }
        }
    }
}

I could also write it as

for (Object o : array1) {
    for (Object o2 : array2) {
        if (condition1 && condition3) {
            // get a variable for condition2 (depends on iteration elements)
            if (condition2) {
                // do something
            }
        } else if (condition1 && condition4) {
            // get a variable for condition2
            if (condition2) {
                // do something
            }
        }
    }
}

which would work the same way. In the first example, I tried to use code multiple times as much as possible, where as in the second example I had to use the same code multiple times.

Now I read on this question, that one should avoid nesting if possible. Yet in my understanding it is also cleaner not to have the same code multiple times which I would have in the first example.

My question is, should one avoid nesting on the cost of having the same code multiple times? I am asking about best practices/standards here and not opinions.


I know that I could move it into a separate method, but in the end I would probably have about the same amount of code.

EDIT:

With the help of Lino's answer and comments I came up with following construct:

for (Object o : array1) {
    // get a variable for condition2 (depends on iteration elements)
    if (condition2) {
        for (Object o2 : array2) {
            if (condition1) {
                if (condition3) {
                    // do something
                } else if (condition4) {
                    // do something
                }
            }
        }
    }
}
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • 2
    Currently both snippets don't do the same. In the first condition 3 and 4 are only executed if condition 1 and 2 are met. in the second its somewhat different, in the way that condition 2 can either be met if 1 and 3 are or if 1 and 4 are met – Lino Jun 22 '18 at 07:29

1 Answers1

1

Assuming condition 3 and 4 are only executed if condition 1 and 2 must evaluate to true then the following snippet would work the best:

for (Object o : array1) {
    for (Object o2 : array2) {
        if (condition1 && condition2) {
            if (condition3) {
                //do something
            } else if (condition4) {
                //do something
            }
        }
    }
}

If you still don't like nested ifs you may aswell store the result from condition1 && condition2:

for (Object o : array1) {
    for (Object o2 : array2) {
        final boolean condition1And2 = condition1 && condition2;
        if (condition1And2 && condition3) {
             //do something
        } else if (condition1And2 && condition4) {
             //do something
        }
    }
}
Lino
  • 19,604
  • 6
  • 47
  • 65
  • How should I go about `// get a variable for condition2` if I don't want to execute that part on every iteration? – XtremeBaumer Jun 22 '18 at 07:42
  • @XtremeBaumer what about getting that variable before the iteration? if it doesn't depend on the elements from the iteration this may work the best – Lino Jun 22 '18 at 07:44
  • But it does depend on the elements, therefore I can't get it before – XtremeBaumer Jun 22 '18 at 07:45
  • @XtremeBaumer from the elements of the inner or outer loop? If its from the outer, you could define it before the inner loop. If it does depend on the inner, then you wont come around calling it on every iteration – Lino Jun 22 '18 at 07:46
  • @XtremeBaumer I am sorry to disappoint you, but best practice is still opinion based... – Lino Jun 22 '18 at 07:57
  • I was talking about the `EDIT` part – XtremeBaumer Jun 22 '18 at 07:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173604/discussion-between-lino-and-xtremebaumer). – Lino Jun 22 '18 at 07:58