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!