Consider the following:
if (x == 5 || x == 10 || x == 12) {
if (x == 5) {
doSomething()
} else if (x == 10) {
doSomethingElse()
} else {
doSomeOtherThing()
}
doMoreThings()
}
doThingsAfterThat()
The second set of conditions feel repetitive, as I have specified them in the original condition. I could separate this into two conditionals, but then I would be repeating `doSomeOtherThings()'.
What is the most elegant way to handle this?
Edit:
This is not a duplicate of Elegant ways to handle if(if else) else. The reasons being:
- In the potential duplicate question, the internal condition is testing a different value than is being tested in the original condition. In this question, the internal conditions being tested are the same as the original condition.
- In this question, one has to give consideration to multiple if, else if statements. The potential duplicate question does not consider this.