Page 1 of 1

is using local for same variable multiple times safe?

Posted: Fri Mar 31, 2023 9:52 am
by Bugala
I just noticed that having used copy-paste I have done something unnecessary, using "local" multiple times for same variable, and now I wonder if I need to fix this possibly, short imaginary example of the thing:

Code: Select all

local newitem = {x=1, y=2}
insertitem(mytable, newitem)
local newitem = {x=3, y=4}
insertitem(mytable, newitem)
local newitem = {x=5, y=6}
insertitem(mytable, newitem)
Just suddenly realised I have whole bunch of lines like this, where Local is not really necessary, could have just used it the first time and left it out from the rest.

Seems program is working just fine at least currently, but is there some potential problem this could cause, and therefore I should remove those unnecessary "local"s, or is it safe to leave them as they are?

Re: is using local for same variable multiple times safe?

Posted: Mon Apr 03, 2023 10:26 pm
by lazi
I don't know about the inner mechanics of Local, but I think it does not harm if you overuse it. However you should avoid any unneccesary strings in your script.

Anyway there is a great description of Local in the manual/guide. The examples contains an interesting statement, the Block- Endblock. I have not heard about it before. It is tightly connected to Local variables.
It was a nice find thanks to your question!

Re: is using local for same variable multiple times safe?

Posted: Tue Apr 04, 2023 9:20 am
by Bugala
@lazi Thanks for reminding me about that Block-End Block thing. I had noticed it at some point in the past but had completely forgotten about it, and it would have been handy a couple of times actually, so going to keep that in mind now.

Re: is using local for same variable multiple times safe?

Posted: Thu Apr 13, 2023 3:30 pm
by airsoftsoftwair
It's safe but definitely not a good style.