Hi, I'm trying to understand how best to limit my risk with regards to local scope in functions. In my understanding my options for limiting the potential to pollute the Here scope from within a Function execution are to either declare my local variables or use Default Local. However, each of these has some problems:
Default Local:
- Referencing values may be context dependent, since names that haven't been defined locally fallback to Here then possibly (if Names Default To Here is off) Global then current table. As noted in the docs this can mean that behavior is less predictable as values are dependent on the context.
Explicit Declaration:
- Referencing values may be context dependent, since names still use this fallback behavior until they are declared if not declared in the local names
- Assignments can accidentally leak into the Here scope. That is, if a name that is intended to be local is omitted from the local name declarations then it will pollute the surrounding Here scope
- Generally this outward scope pollution is hard to track down.
- For example, if you call a function in a For where you use "i" as an iterator and that function also has a For that uses i and fails to declare it as local, this will affect your outer For loop. Granted this is obvious programmer error, but it is an easy one to make and is difficult to debug.
In my mind, the ideal option would be able to declare something like "Strict Local", which would always treat unqualified names (both when assigning values and referencing values) as local and surrounding scopes would only be accessible with qualified names. This would allow me to not risk polluting surrounding scopes as well as forcing me to declare when I am depending on those scopes, so limiting local pollution from surrounding scopes.