I’m creating guard clauses to prevent my plugin code from running in incorrect circumstances but I’m not particularly sure about how three if statements would evaluate in comparison to three ors in one if statement or if the operations would be pretty much the same.
if condition or condition2 or condition3 then return end
if condition then return end
if condition2 then return end
if condition3 then return end
First one looks gross and unreadable for long conditions so I new lined the ors along with their condition and that looked even worse so I thought about using the second sample instead. I’m not particularly sure how the evaluations compare though.
It’s negligible and I shouldn’t care that much because it only ever runs one time but for the sake of knowledge and future scenarios where it doesn’t become negligible (e.g. needing to do this via a RunService signal or whatever) it’d help me greatly to know.
For those who might suggest I use os.clock to check, that doesn’t work for me. I’m moreso concerned about the language’s technical implementation not a speed check.
if condition or condition2 or condition3 then return end
If condition is true then condition2 and condition3 do not need to be evaluated since this is an or operation.
if condition then return end if condition2 then return end if condition3 then return end
if condition is true then condition2 and condition3 will still be evaluated.
The only time this could matter is if you’re trying to abuse how a condition is evaluated–perhaps when code golfing. _=condition or f(). over if not condition then f() end
local function a()
print("2")
end
local b = true
if b or a() then
print("1")
end
b = false
if b or a() then
print("3")
end
Fair point. Long condition checks just look so gross but nice of me to conveniently forget that the other ones wouldn’t need to be evaluated if one of them meets the criteria which would then go to the return.
Cheers, this is what I was looking for. Not particularly sure what is meant by the latter half about abusing how conditions are evaluated, if that’s an actual abuse of the conditional or if this is relevant information to hold on to, but I’ll take a note of it I guess.
Unmarked as solution because now I’m trying to read the comments and figure out what’s correct here. I think the confusion comes in what I’m trying to do: see my use case, I’m trying to make a guard clause. If the if statements pass they’re supposed to return and exit out of scope.
Are any of you familiar with the actual technical implementation of it in Lua? How would a three-or single if statement compare in evaluation to three if statements? I’m not sure if the if logic check is the exact same on all of them or if there’s at least one step that’d be cut.
For example: with three ors conditions would be checked until the one you want is hit and with three if statements then it’s looking at the if first and then the condition second three times rather than the if once and conditions three times.
I guess the first reply does make sense but are they just identical or are they exactly the same?
you are right. I missed the return call which exits the function. I was mentally replacing return with any given codeblock that might not exit the function.
The first time one of your guards returns true (so you want to quit early), the code will return and not check anymore guards. This is the case for both versions:
local function CheckSomething()
print("Checking something")
return true
end
local function CheckSomethingElse()
print("Checking something else")
return false
end
local function WithOrs()
if CheckSomething() or CheckSomethingElse() or CheckSomething() then return end
print("Done!")
end
WithOrs() --> prints "Checking something" and stops
local function WithIfs()
if CheckSomething() then return end
if CheckSomethingElse() then return end
if CheckSomething() then return end
print("Done!")
end
WithIfs() --> prints "Checking something" and stops