I'm trying to follow Uncle Bob's clean code suggestions and specifically to keep methods short.
I find myself unable to shorten this logic though:
if (checkCondition()) {addAlert(1);}
else if (checkCondition2()) {addAlert(2);}
else if (checkCondition3()) {addAlert(3);}
else if (checkCondition4()) {addAlert(4);}
I cannot remove the elses and thus separate the whole thing into smaller bits, cause the "else" in the "else if" helps performance - evaluating those conditions is expensive and if I can avoid evaluating the conditions below, cause one of the first ones is true, I want to avoid them.
Even semantically speaking, evaluating the next condition if the previous was met does not make sense from the business point of view.
edit: This question was identified as a possible duplicate of Elegant ways to handle if(if else) else.
I believe this is a different question (you can see that also by comparing answers of those questions).
- My question is checking for the first accepting condition to end quickly.
- The linked question is trying to have all conditions to be accepting in order to do something. (better seen in this answer to that question: https://softwareengineering.stackexchange.com/a/122625/96955)