Avoid using else / elseif - Make your code smaller and more efficient (For beginners)

are there any cases where elseif’s are needed?
i usually just use multiple if statements and I feel like else is more useful then elseif

I NEVER use elseif so if I remember right they still run if the past statement was false or true

Else and elseif aren’t needed unless you wanted to make your code long and unorganized.

I’m talking about elseif not else

also I’m just asking the difference between multiple if statements and elseif statements

That’s why you add comments throughout code to help others see what your code is doing and which parts are responsible for what. The only thing that isn’t quite beginner friendly is my addition of typechecking on the table but that’s fairly negligible. :slight_smile:

It’s good to teach best practice early on so newer developers get into good habits. You can consolidate your tables into data tables instead of having separate tables for each item.

1 Like

At this point you are villainizing if and elseif, there are absolutely valid use cases for them. What you are doing (dictionary dispatch) may use a bit more memory since depending on how many conditions you have you’ll have quite a large table, but probably not a big deal. Even then I think for organization purposes you should have a function that returns the “word” variant of a number.

7 Likes

Saying that the else and elseif statements is not efficient depends on the entirety of the code. A conditional statement itself is not inefficient, but rather what happens if that condition is true or if there are multiple conditions to be checked or what methods are used to test the condition.

It comes down to a per code or per structure basis. Not every else and elseif statement is inefficient. There are just the very specific occasions where not using it would save a bit of performance.

1 Like

It’s entirely possible to have shorter code that’s more inefficient that a longer block of code, so the argument that less if / else statements makes code more efficient is incorrect. Although having less statements will improve the readability and organization of code, it doesn’t have a large impact of code efficiency in most cases.

1 Like

Actually in the case you prescribed, elseifs would be faster (even in the worst case scenario where it has to check every branch before it gets to the end)
Here’s the test:

local status = "10"

wait(0.5)

local start = os.clock()

for i = 1,10000000 do
	local ae
	if status == "1" then
		ae = "One"
	elseif status == "2" then
		ae = "Two"
	elseif status == "3" then
		ae = "Three"
	elseif status == "4" then
		ae = "Four"
	elseif status == "5" then
		ae = "Five"
	elseif status == "6" then
		ae = "Six"
	elseif status == "7" then
		ae = "Seven"
	elseif status == "8" then
		ae = "Eight"
	elseif status == "9" then
		ae = "Nine"
	elseif status == "10" then
		ae = "Ten"
	end
end

print(os.clock() - start)

local statusMap = {
	["1"] = "One",
	["2"] = "Two",
	["3"] = "Three",
	["4"] = "Four",
	["5"] = "Five",
	["6"] = "Six",
	["7"] = "Seven",
	["8"] = "Eight",
	["9"] = "Nine",
	["10"] = "Ten",
}

wait(0.5)

local start = os.clock()

for i = 1,10000000 do
	local ae = statusMap[status]
end

print(os.clock() - start)

Here’s the result:
image

To be clear I’m not advocating for the use of lots of elseifs, the performance difference is negligible (unless you’re doing 100000000 of these, but then I think that points to a larger problem), and the readability costs get big

But saying that making all your if branches into tables will make it more efficient is false, you’re using more memory for the tables and as shown above will probably make it ever so slightly slower too (I haven’t tested how large an if branch needs to be to get slower than a dictionary, but you rarely need to use more than 10 if branches anyway).

2 Likes

Elseif is hugely important. There are two ways to avoid using elseif

if a then
else
    if b then
    else
        if c then
        else
        end
    end
end
if a then
end
if b then
end
if c then
end

Method two causes a performance hit since it needs to evaluate everything, even if the first one cleared the check. Don’t do that.
Method one is more proper, but it’s harder to read and (I believe) still results in an extra opcode. Use elseif.

1 Like

The title is pretty misleading and you never really explained why. You’re basically just saying shorter code = better code. This isn’t the case for what you’re talking about. In fact, there are even shorter ways to make even shorter code such as using the and, or, and not.

if 1 == 1 then
    return "1 = 1"
else
    return "1 not equal to 1"
end

--// Can be changed to

return 1 == 1 and "1 = 1" or "1 not equal to 1"

These 2 ways don’t have a difference when ran.


I believe you were probably trying to talk about code clarity and not code efficiency (as in performance). In which case, is totally up to preference.

1 Like
game.ReplicatedStorage.SendScore.OnClientEvent:Connect(function(Win)
    local Text = Win == "Win" and " " or " didn’t "
    local Id = Win == "Win" and "6808979425" or "6784305960"
    script.Parent.Text = "You"..Text.."beat your previous time"
    script.Parent.Parent.Music.SoundId = "rbxassetid://"..Id
    script.Parent.Parent.Visible = true
    script.Parent.Parent.Music:Play()
end)

not saying you should do this just saying it’s an other option

1 Like

ok that clears it up then, thanks

I didn’t care as much about that first option

2 Likes

I think more importantly to mention, as you said the difference of elseif vs. dictionary is negligible, however the flexibility + maintainability you gain by offloading it to a table is much better, not to mention I personally have an easier time reading a dictionary than elseif’s compacted in their standard form. I’m sure others may agree.

2 Likes

if you don’t mind me asking, what does the colon* do in a table? and do you have any resources for it/ explain it?

1 Like

you can use commas or semicolons to separate each value in a table

I meant Colon, my bad.":" this thing.

He’s just using them for type checks.

1 Like

TYSM, I was wondering because I saw it before, but never had the oppurtunity to ask. Have a great day!

1 Like

It might not be useful to always use tables in place of if/else(if) statements. I find if/else(if) statements to be more efficient, but for Something like this:

Tables are useful, and you could have shortened the table to a single line:

local NumberTable = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}

And as mentioned by someone above, villainizing if/else(if) statements to be portrayed as completely useless is absolutely wrong. Both ways have their use cases. And making code shorter does not always means its efficient (as mentioned by someone else above).

Other than that, It seems fine.

1 Like

Here’s my example on what I am trying to replace. I have a small elseif loop that goes through some conditions but for the last 2 I need it to check what the returned value is from table.find and if it equals another value it’ll replace it with the appropriate name. Someone told me it wasn’t very efficient so if anyone knows how to fix it, it would be appreciated!

		local Positions = {
			Left = {
			[1] = LOne.Position,
			[2] = LTwo.Position,
			[3] = LThree.Position,
			[4] = LFour.Position
			},
			Right = {
				[1] = ROne.Position,
				[2] = RTwo.Position,
				[3] = RThree.Position,
				[4] = RFour.Position
			}
		}

			if Team == "Team1" then
				Lc = T1.Position
			elseif Team == "Team2" then
				Lc = T2.Position
			elseif Team == "Team1Pad" then
				Lc = if table.find(Team1, Char) == 1 then LOne.Position elseif  table.find(Team1, Char) == 2 then LTwo.Position elseif table.find(Team1, Char) == 3 then LThree.Position else LFour.Position
			elseif Team == "Team2Pad" then
				Lc = if table.find(Team2, Char) == 1 then ROne.Position elseif  table.find(Team2, Char) == 2 then RTwo.Position elseif table.find(Team2, Char) == 3 then RThree.Position else RFour.Position
			end