It’s ironic how the “Else If” statement is so bi-angular in the mind of the programmers. Most programmers “boycott” it do it for 2 reasons :
-
Either they have been working with someone’s work which was badly scripted and included it.
-
Or they learned it from someone else as a “no go” and keep thinking it.
It’s the same as if you’re learning to speak English in the U.K. or in the U.S. or French in France or in Quebec (Canada). It’s a matter of where you learned it and who’s teaching.
Personally, I think it’s okay to uses it as conditional statement if you got full control over the conditions. For example, if you make uses of a Random.Range(x,y) and want to apply some specific actions/statements ONCE with uneven range covering.
For example, in a function that is only called once when a chest is opened.
void ChestDrop()
{
int randomDropTemp = 1;
randomDropTemp += Random.Range(0,99);
if(randomDropTemp <= 60)
{
GenerateDrop(1);
}
else if(randomDropTemp >= 90)
{
GenerateDrop(3);
}
else
{
GenerateDrop(2);
}
}
In which GenerateDrop(); have its own switch statement that works with the 1, 2 or 3 integer as its case values. This is a situation in which else if can be useful as it allow temporary predetermined uneven conditional variables to be quickly read and analyzed once and forgotten in the next frame.
In the given example, a Switch statement would be difficult since it represent a chance of 60% to getting a drop of quality 1, 10% of getting a drop of quality 3 and the remaining possible 30% of getting a drop of quality 2. There’s also no chance of having the randomDropTemp value outside of the predetermined values. Even if for some unknown reason, randomDropTemp gets lower than 0 or higher than 100, it’s still covered.
Personally, I would advise people to never listen to words like “it’s ugly” or “it’s sinful”, “it’s inelegant”, etc. to judge the uses of any statement or function in programming (like in mathematics). Those are used only to put more pressure on the opinion without real logical meaning behind them. The ONLY reason why you should listen to that type of comment is if it has an impact on your future like having one hell of a really close-minded teacher that could make you fail just for it. Otherwise, it’s already good to listen to comments like “it won’t work with[…]” or “In this case, it can work […]” because they are logical reasons that “works” regardless of aesthetics.
Aesthetics is always the last of your problems. If you can include it, good. If not, don’t bother with it and practice so that, once day, you can include it. That’s the same with “ugly”, “sinful” and “inelegant” comments.
Things like this :

Should be your priority.
Only when you have fully finalized your work that it should looks like this :

(Note that those picture are not the same equations… it’s only for showing what I meant visually)
Damn even the example I have given, personally, for the sake of making it simple and as much forgettable as possible, I even tend to not format it as big as that… more like :
void ChestDrop()
{
int randomDropTemp = 1; //Temporary int for drop quality
randomDropTemp += Random.Range(0,99); //Will result between 1 and 100
//Get and send the random-based drop quality to GenerateDrop
if(randomDropTemp <= 60){GenerateDrop(1);}
else if(randomDropTemp >= 90){GenerateDrop(3);}
else{GenerateDrop(2);}
}
Well, that’s my opinion.