Using else if

I’ve seen it mentioned a few times on the forums that you should never use “else if” in code.

Now, I understand that it can be easily abused. If you have an if statement that has 20 “else if” statements within it, you’re probably doing something wrong (or, at least, inefficiently). However, do people really mean NEVER use “else if”? Or do they just mean to use it rarely?

I personally think they mean to use it rarely, or sparingly as it’s a poor “logic” choice. When running through a method or a program, if someone is using else if’s a lot, it’s likely that they’re not taking into account all the dynamic possibilities that they may run into.

I think it’s perfectly fine to have a string of else if’s if the logic is guaranteed to be within a set confines. Even if so, if it can be handled in a switch statement, it should be rather than an else if because then you’re forced to handle the step through logic, and ESPECIALLY you’re recommended to enforce the default case so that you’re wrapping the step through properly with the least amount of possible leaks.

The main reason people declare to never us it, is because it’s “more proper” to use a string of ifs and return out of the block of execution, which I don’t necessarily agree with. This way, you’re handling the step through logic properly, and you’re minimizing the comparison’s that you need to do, which is why a switch statement is seen as a much better option than a string of else if’s, because you’re using break to break out of the step through.

Even then, it’s not like it’s going to make a huge difference if your practices are sound. In the end, I would personally see it simply as a design choice. You either go one way, or you don’t.

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 :

  1. Either they have been working with someone’s work which was badly scripted and included it.

  2. 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 :

Dirty Formula

Should be your priority.

Only when you have fully finalized your work that it should looks like this :
Clean Formula

(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.

Ok…noob alert here…

From my point of view (I’ve been working in computer code for about 3 months), else if statements make sense for certain things. Basically, I can denote lines of code to only be called when the first statements are false. For example, if I want to create a simple set of statements to turn a boolean on or off, I need to use else if:

var test : boolean = false;

//when key is pressed

if(test == true)
{
     test = false;
}
if(test == false)
{
     test = true;
}

this variable will always be true, because it will fun the first if statement, and then immediately run the next if statement.

    var test : boolean = false;
    
    //when key is pressed
    
    if(test == true)
    {
         test = false;
    }
    else if(test == false)
    {
         test = true;
    }

However, in this statement, the variable will switch from true to false with each button press.

And I’m probably missing something, but this seems like a more simple way to do SOME THINGS. And if it isn’t I’d love to be directed to some more learning materials!

Thank you for your time!

Writen a blog related to full concept of condition statements the link is attached below: