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

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