EDIT: I only want the finally
code to be executed if one of the if
or else if
statements were true.
Many many times, I run into the following situation:
if (condition1)
do stuff
do something
else if (conditon2)
do other stuff
do something
else if (condition3)
algorithm here
do something
If there were a finally
clause on if statements, I could reduce that into:
if (condition1)
do stuff
else if (conditon2)
do other stuff
else if (condition3)
algorithm here
finally
do something
I'm trying to find some solution where do something
only needs to be called once, without making an additional method, doing some weird out of place if statement, or making a flag like:
boolean special = false;
if (condition1)
do stuff
special = true;
else if (conditon2)
do other stuff
special = true;
else if (condition3)
algorithm here
special = true;
if (special) // <--- bad solution, doesn't simplify anything
do something
finally statement is just like another block of statements. You can do anything inside them. You can use for/while loops, if/else/elif statements and even another try-except-finally block. Yes.
Java For Testers It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.
The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the connection. The finally block executes whether exception rise or not and whether exception handled or not. A finally contains all the crucial statements regardless of the exception occurs or not.
Java has a syntax that allows to achieve this without a flag variable, i.e.
doFinalAction: {
if (condition1)
do stuff
else if (conditon2)
do other stuff
else if (condition3)
algorithm here
else
break doFinalAction;
do something
}
but you should think twice before using it. A lot of developers aren’t familiar with that and you’re not programming for you alone, most of the time. Some people think, it reminds them of the goto
statement too much.
A cleaner approach is just putting “do something” into a method, e.g. doSomething()
and call it explicitly:
if (condition1) {
do stuff
doSomething();
}
else if (conditon2) {
do other stuff
doSomething();
}
else if (condition3) {
algorithm here
doSomething();
}
The code duplication is acceptable if it’s just a single method invocation and the code even isn’t significantly bigger than the variant without.
By the way, sometimes developers go the route of being even more verbose, deliberately:
final boolean special;
if (condition1) {
do stuff
special = true;
}
else if (conditon2) {
do other stuff
special = true;
}
else if (condition3) {
algorithm here
special = true;
}
else special=false;
if (special)
do something
Declaring special
as a blank final
variable has the clear advantage that the compiler verifies that there is exactly one assignment in every code path that can reach the if(special) do something
statement, which ensures that no case can exhibit either behavior by accident, i.e. a forgotten assignment or the assignment being overwritten contradictionally later on. That’s especially important, when converting an if … else if …
chain to a switch
statement, where fall-through behavior is possible.
Although there is no special construct to do what you want to do, you can rewrite the last attempt with a single assignment in the final else
branch:
boolean doFinally = true;
if (condition1)
do stuff
else if (conditon2)
do other stuff
else if (condition3)
algorithm here
else
doFinally = false;
if (doFinally)
do something
This does simplify your code, because the repetition of setting special
to true
is now gone.
Your third example:
boolean special = false;
if (condition1) {
doStuff1();
special = true;
} else if (conditon2) {
doStuff2();
special = true;
} else if (condition3) {
algorithmHere();
special = true;
}
if (special) {
doSomething();
}
is actually a pretty common idiom, and there's nothing "bad" about it, as long as you give special
a descriptive name (in real life, that is; I don't expect a descriptive name here). However, this may be best:
if (condition1) {
doStuff1();
} else if (conditon2) {
doStuff2();
} else if (condition3) {
algorithmHere();
}
if (condition1 || condition2 || condition3) {
doSomething();
}
with the proviso that if the conditions are complex or could involve side effects, you should define boolean
variables (with descriptive names) before the if
to hold the values of the conditions, so that your last if
is as simple as possible. The choice between these two and your first example (repeating the doSomething() in every branch, which could be OK if it's very simple) is partly a matter of style, and I might pick different approaches in different situations depending on which one I thought was most appropriate in a particular case.
The idea of sharing code between different branches of if
statements is something I've long thought would be useful, but I've never seen a language that actually does it (and if there were, the syntax would probably be difficult to understand anyway). Just be glad that we no longer do the things programmers used to do when I first started, which would be to have two of the if
branches use a goto
to jump to the common code in the third branch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With