How to avoid "if" chains?
You can use an &&
(logic AND):
if (executeStepA() && executeStepB() && executeStepC()){ ...}executeThisFunctionInAnyCase();
this will satisfy both of your requirements:
executeStep<X>()
should evaluate only if the previous one succeeded (this is called short circuit evaluation)executeThisFunctionInAnyCase()
will be executed in any case
Just use an additional function to get your second version to work:
void foo(){ bool conditionA = executeStepA(); if (!conditionA) return; bool conditionB = executeStepB(); if (!conditionB) return; bool conditionC = executeStepC(); if (!conditionC) return;}void bar(){ foo(); executeThisFunctionInAnyCase();}
Using either deeply nested ifs (your first variant) or the desire to break out of "part of a function" usually means you do need an extra function.
Old school C programmers use goto
in this case. It is the one usage of goto
that's actually encouraged by the Linux styleguide, it's called the centralized function exit:
int foo() { int result = /*some error code*/; if(!executeStepA()) goto cleanup; if(!executeStepB()) goto cleanup; if(!executeStepC()) goto cleanup; result = 0;cleanup: executeThisFunctionInAnyCase(); return result;}
Some people work around using goto
by wrapping the body into a loop and breaking from it, but effectively both approaches do the same thing. The goto
approach is better if you need some other cleanup only if executeStepA()
was successfull:
int foo() { int result = /*some error code*/; if(!executeStepA()) goto cleanupPart; if(!executeStepB()) goto cleanup; if(!executeStepC()) goto cleanup; result = 0;cleanup: innerCleanup();cleanupPart: executeThisFunctionInAnyCase(); return result;}
With the loop approach you would end up with two levels of loops in that case.