Avoiding Bad Scripting Practices - Clarification

  1. I’ve read the documentation regarding the spawn() function and how using it too much would cause performance issues. The function I spawn is an infinite loop until a condition is satisfied (while a == false do). When the condition is not satisfied, the loop would stop, but will the thread that the spawn function created be destroyed?

  2. I’ve read posts saying that variables are better off local than in places like _G or ThisVariable=true. Local variables are said to save some milliseconds when they are called, but what’s really the difference with
    ThisVariable=true and local ThisVariable=true? Which one is a bad practice?

1 Like

When the code inside the spawn is finished, the thread will die.

The perf benefit of local variables is irrelevant. Local variables (local x = 1 and local function x()) are always favored over global variables due to their more intuitive and less side effect causing lexicology.

for i = 1, 3 do
	x = i
end

print(x) -- 3

vs.

for i = 1, 3 do
	local x = i
end

print(x) -- nil

Local variables are kept within their scope. This is not only more intuitive, but is also easier to work with and read. You don’t have to worry about overriding variables other scopes might use and can more easily figure out where a variable is initially defined.

7 Likes