How many if statements are too many if statements?

Okay this is a very general question, and I kind of want (need) to know. How many if statements are too many if statements. All developers have stated lots of elseifs are a problem (Yandere Devs) but are singular if statements better? (For different things). How much is too much? And what are alternatives?

Thanks. : )

1 Like

As long as you donâ€™t have something completely unreasonable then you are completely fine.

(if statements arenâ€™t as catastrophic as you think as long as you donâ€™t overdo it, like with most things)

Well, you need to know that for the major part if and elseif statements wonâ€™t be absolutely destroying your game performance. Because of all the script happening at the blink of an eye, one singular if elseif elseif elseif elseif elseif else statement wonâ€™t be breaking your game, if it was run 6 times every 4 seconds you would still not see any trouble, even if it was 6 times every 1 second. The issue starts to rise when itâ€™s more than 7 in routine, so they do it every milisecond and they need the response in a milisecond.

What youâ€™re saying about yanderedev having his game being a broken mess because of elseif is kind of a misinformed statement that I shared sometime ago, his game did have a lot of huge elseif statements, but even by turning it into switches the game would still run the same, the issue was on the lack of graphic priority and tool details, which meant that a lot in the game had a lot of polygons, which many were useless, there even was a toothbrush with 500 polygons where you only needed 6!

If you, for some reason, wanted to make a script without using a lot of elseifs you would have to use switches, but because lua doesnâ€™t have switches you can also use dictionaries to make them do something on call, and insert a function to each word of the dictionary

so, basically, instead of doing this

``````if input == "a" then
do "a"
elseif input == "s" then
do "s"
elseif elseif elseif elseif
``````

you could do this table instead

``````keymovedon = {
[Enum.KeyCode.W] = function(key, input) if input == Enum.UserInputState.Begin then currenty2 = .5   else currenty2 = 0  end end,
[Enum.KeyCode.A] = function(key, input) if input == Enum.UserInputState.Begin then currentx1 = -.5   else currentx1 = 0  end end,
[Enum.KeyCode.S] = function(key, input) if input == Enum.UserInputState.Begin then currenty1 = -.5  else currenty1 = 0  end end,
[Enum.KeyCode.D] = function(key, input) if input == Enum.UserInputState.Begin then currentx2 = .5   else currentx2 = 0  end end
}
``````

Now, does it improve your performance by a long shot? not really, but it makes the code look much more comfortable at sight, and somewhat more formal.

10 Likes

Thank you for both of your answers @FranSauce @UnknownParabellum. This has slightly enlightened me (sounds kinda cringe but yk). The way people kind of stigmatize lots of elseif statements just kind of confused me on everything, making me post this. Thank you Fran for your in depth explanation.

1 Like

Hey, donâ€™t worry man, you will always flinch at your own actions, even those people just let slide.

Also, uhhhhh, I had done a similar thread to this one in '20 but because itâ€™s not that visible I will share it with you if anything about it can help you.

4 Likes