Should I use continue?

Now that the continue keyword was added to Roblox, I see a lot of developers heavily relying on it.

For example, say I have this piece of code:

while wait() do
	loal user = ai:getTarget()
	local head = getHead(user)
	if not head then continue end

Would you consider this a valid use of continue?

because normally I would just do:

while wait() do
	loal user = ai:getTarget()
	local head = getHead(user)
	if head then
		-- Do code
	end

In the JavaScript ecosystem, ESLint even suggests to you that you don’t use continue at all.

that is perfectly fine, in some cases the only viable solutions is to use continue. The only reason i could see this being not preferred is because you wont be able to run any code under continue in the same scope, but it is pretty much the equivalent of doing:

function hi()
     local player = script.Parent.Test
     if not player then return end -- perfect
     --more code under here
end

which is perfectly fine to use - for continue, it would work like this:

while wait() do
     local player = script.Parent.Text
     if not player then continue end
end

now that i think of it, i seriously wonder how people dealt with coding before continue was a thing, lol

1 Like

It has to do with readability. Using continue in the example I provided is less clear than just saying if whatever then. You can find a lot of debate on this on Github and other sites.

1 Like

You can simply make it more visually prominent:

while wait() do
	local user = ai:getTarget()
	local head = getHead(user)

	if not head                then  continue
	end

	-- Stuff

	if not foo                 then  continue
	end

	-- Stuff

	if not bar                 then  continue
	end

	-- Stuff
end
1 Like