Questions about scripting

You seem to be referring to a guard clause though in an incorrect usage. return is used to stop the function from continuing which a guard clause uses, it is mainly for readability.

I say incorrect since using return at the end of function isn’t really a guard clause because its at the end of the function which using return has no difference versus no return there.

It prevents nested code mainly
if condition then
    -- example
    if condition2 then
        -- stuff
        if condition3 or condition4 then
            -- stuff
        end
    else
        -- stuff
    end
else
    -- stuff
end

Becoming this instead:

if condition then
    -- do stuff
    return
end
if not condition then
    -- do stuff
    return
end
if condition2 and (condition3 or condition4) then
    -- do stuff
    return
end
if not condition2 then
    -- do stuff
end

There’s zero difference in the result, they will do the same thing. The difference is visually reading that code.

1 Like