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

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