lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 8 July 2010 12:45, Mark Hamburg <mark@grubmah.com> wrote:
> As someone I think pointed out, the reason the globals issue seems to keep coming up is that globals are viewed as "bad" and Lua makes them easy to create. In addition, that ease of creation can also hide errors caused by typos.
>
> Why are globals "bad"?

Actually, the main reason I think globals are bad is that it's too
easy to use them by accident. Two situations I've hit are

1) I make a typing mistake and end up using an unused global instead
of the local I meant. The only symptom is that one use of the variable
seems to return nil.
2) I forget to declare a variable local, so that it ends up being
global instead. Everything works fine until the function ends up being
called re-entrantly, resulting in a tricky to find bug.

That being said, I love how useful they are for making closure-based
objects. Using setfenv I set it up so that globals are public fields,
and locals are private fields, but both using the same syntax for
accessing/setting.

    henk