Questions about scripting

So I have 2 questions.

What is the difference between:

if (number == 5) then

--and

if number == 5 then

The above I dont think there is a difference but what is the difference between this:

if debounce == false then
--do stuff
end

--and

if debounce == true then return end
--do stuff

I know what returning is, but is there a difference?

2 Likes

The first, as far as I know, I believe using parentheses gives some operator precedence, but I don’t know for sure, so don’t quote me. Someone please confirm this.

But it does make code look cleaner when your doing multiple checks in an if statemenr.

The second, both statements are equivalent.

if not debounce then
    — code
end 
if debounce then
    return
end

Are just two different ways of doing the same thing, there’s really no difference between them.

1 Like

Oh okay, I see alot of people do:

if debounce == true then return end

But I prefer to do:

if debounce == false then
--do stuff
end

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