In an if statement, if I had this:
if not enabled then return end
Does it differ in any way (such as performance wise) to doing it this way
if enabled then
-- do stuff
end
In an if statement, if I had this:
if not enabled then return end
Does it differ in any way (such as performance wise) to doing it this way
if enabled then
-- do stuff
end
It’s mostly based on preference/ case by case coding. The first instance has a return statement included, meaning the outer function will end - whereas in the second instance the rest of the function will continue as normal following the if statement.
i.e
function example()
if not enabled then return end
doSomething
somethingElse
end
function example2()
if enabled then
doSomething
end
somethingElse
end
somethingElse only runs if the condition is met in the first instance, whereas somethingElse will always run in the second instance.
No, there’s no difference besides the way it is written. They will both do the same exact thing, but I would prefer using the 2nd option for organization purposes.
When you have a long list of checks, doing
if not plr then
return "you are not real"
end
if not plr.Character then
return "you are in the void"
end
if not plr.Character:FindFirstChild("Humanoid") then
return "you are dead"
end
-- do stuff
is more readable than doing
if plr then
if plr.Character then
if plr.Character:FindFirstChild("Humanoid") then
-- do stuff
else
return "you are dead"
end
else
return "you are in the void"
end
else
return "you are not real"
end
(it becomes hard to tell which else belongs to which if (especially if you add more ifs and add more content in those ifs))
Performance wise they are probably the same