lua-users home
lua-l archive

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


On 2010-07-08, David Goehrig <dave@nexttolast.com> wrote:
> Why are people so afraid of globals?
>
> Maybe it is just my age, but this entire discussion sounds a lot like trying
> to do nasty things to the language to protect inexperienced people from
> shooting themselves in the foot.  Which is an activity bound to fail due to
> fools being so ingenious.
>
> A few months back I ported a rather large distributed application that was
> originally written in javascript to lua.  In it, we make extensive use of
> global name space objects, ( which are truly global as the objects's full
> name actually specifies which sever around the world it actually lives on).
>  Within each object, all state is represented within the properties of the
> object itself.  If you happen to  need a temp variable for a particular
> task, you create a property of the object itself to hold it.  Each object is
> encapsulated, and has its own internal event queue to manage scheduling.
>  Globals do not necessarily impede modularization and encapsulation, poor
> design, lack of style, and insufficient attention to detail definitely do.
>
> If you take a peek at how Self implements globals, and copy that you'd
> probably address 100% of the problems people actually have with globals.
>  But lua's OO features aren't quite up to Self's snuff.
>
> Dave
>
> --
> -=-=-=-=-=-=-=-=-=-=- http://blog.dloh.org/
>

It really isn't about preventing people from shooting themselves in
the foot, but just making it a little harder to do so by accident, and
helping prevent bugs in one module from breaking some completely
different part of your program due to variable name conflicts.
Globals are often said to be a bad thing in most languages, for
various reasons such as efficiency and namespace pollution/conflicts.
With Lua the conflict issue is more severe because there's no compiler
to tell you this name is already used elsewhere (and not even static
typing which might warn you right at the time of conflict if the types
happen to differ).
As some have pointed out, global-by-default (really,
anything-by-default) also makes for some scope ambiguity - assignment
to a global and declaration of a local are syntactically identical.
But this is more of an argument for explicit declaration than against
the use of globals or default scoping.

-- 
Sent from my toaster.