Question About Debounce

So recently I’ve been having a troubling time deciding on how to add debounce to my functions. These two functions do the same thing; I’m just worried there might be something I’m missing, like: Is the second function just better practice? Can return mess something up?

local debounce = false

local function foo()
	if debounce then
		return
	end
	
	debounce = true
	-- code stuff
	debounce = false
end

-- somewhere else

local debounce = false

local function foo()
	if not debounce then
		debounce = true
		-- code stuff
		debounce = false
	end
end

This is kind of a stupid question, but it’s been itching at me lately. Any help is appreciated :+1:

1 Like

Both do the same thing and either is acceptable its just a matter of how you, the coder, likes to style your code. Some people like to put their conditions at the top of a function with a return included some like to put their code within the if condition. All up to you and no return won’t mess anything up.

3 Likes

Ultimately this comes down to preference. There are people who like nesting statements inside each other:

if condition then
    if condition then
        if condition then
            --Code here
        end
    end
end

And there are people who don’t like to nest code:

if not condition then return end
if not condition then return end
if not condition then return end

--Code here

It’s all your preference, and whatever you consider will make things more readable.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.