Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Concerns with variables (and them having 2 meanings)?

Asked by 6 years ago

Here is an example line of code:

local table = {"Value1","Value2","Value3"}

The problem is, "table" is already a feature {table.insert(table,value)} Is there any disadvantage to using this type of variable, and if so, then what?

0
no.. greatneil80 2647 — 6y
0
So there is no disadvantage, and I can use my "table" variable without any issues? OptimisticSide 199 — 6y
0
i believe you're just overwriting it within the scope of the variable. so probably dont, but you can if you want? DinozCreates 1070 — 6y
0
ok OptimisticSide 199 — 6y
View all comments (3 more)
0
Don't do this. User#25115 0 — 6y
0
why cant u just name it something else? Gey4Jesus69 2705 — 6y
0
Just use a different variable. You're overwriting the pre-defined `table` variable. If you don't want to use a different variable then set the original `table` to something else such as, `tableUtility`. `local tableUtility = table; local table = {...}` EpicMetatableMoment 1444 — 6y

1 answer

Log in to vote
3
Answered by
aschepler 135
6 years ago

This is allowed as far as the Lua language and Roblox scripting is concerned. The drawback is that it's confusing.

So it's generally a bad idea to "hide" or "replace" a common name, because it's a violation of the Principle of Least Astonishment. (And software design principles apply even in a project you never intend to share with anyone else, because if you don't fully understand the technical details of an unusual pattern, and/or you set the project aside for a while and later return to it, it can easily be you that gets hit by the gotchas.)

A side note about this type of "hiding" local declaration: A Lua local declaration applies to a specific scope, up to the end of the smallest "block" that contains the declaration. (A block is a portion of code that starts with if, for, while, repeat, do, or function, and ends with the matching end.) After the end of that block, the local declaration no longer has any effect, and the name's previous meaning is visible again.

0
i like this DinozCreates 1070 — 6y
Ad

Answer this question