is using local for same variable multiple times safe?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

is using local for same variable multiple times safe?

Post 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?
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

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

Post 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!
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

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

Post 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.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

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

Post by airsoftsoftwair »

It's safe but definitely not a good style.
Post Reply