[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: In praise of globals
- From: Steve Litt <slitt@...>
- Date: Mon, 15 Apr 2013 15:42:53 -0400
On Mon, 15 Apr 2013 08:38:40 +0200
Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2013/4/15 Brian Kelley <brian.kelley@gmail.com>:
>
> > I look forward to a future Lua that abandons globals completely in
> > favor of manifest upvalues!
>
> That remark comes from another thread, and it's maybe not fair to pick
> on it, but it is an extreme example of something that has been
> bothering me for a long time.
>
> It's getting to be politically correct in the Lua community
> to regard globals as bad.
Globals suck. At least, globals intended to be or contain data. An
abdication of encapsulation.
>
> The attitude (the term 'prejudice' almost sneaked out, but I bit it
> back) is illustrated most clearly by the pejorative term "polluting
> the global namespace".
>
> You can tell the programs written by the supporters of this ideology.
> They start out something like this:
>
> local pairs, ipairs, print, tostring, getmetatable, setmetatable
> = pairs, ipairs, print, tostring, getmetatable, setmetatable
Yeah, that's going a little far. Everyone knows what ipairs is -- no
need to localize it.
>
> There are also plenty of forward declarations of local functions.
>
> Now, I don't want to knock this style of programming altogether. I've
> used it myself. But to preach that is the One Correct Way is going
> too far.
Within limits, you're right. The initial idea of banning globals is
stupid. What is EXPECIALLY stupid is banning global functions that
don't hold any static data -- what's the point unless you're writing a
library and are worried about clobbering somebody else's function name?
I started the preceding paragraph with "within limits". My point was
that I don't want to see Lua (or any other language) turn into Perl,
where "many ways to do it" morphs into "do your own thing, screw the
maintenance programmer!" Lua (and almost every other language) is
soooo far from that Perlistic "ideal", that it's not worth a second
thought.
So I think you're right that globals shouldn't be banned. On the other
hand, you're not going to see me using many global variables holding
data any time soon. To my way of thinking, it's a lot more encapsulated
to pass locals as function arguments. And of course, with upvalues and
the like, Lua gives all sorts of alternatives to globals that lesser
languages don't bestow.
Thanks,
SteveT
Steve Litt * http://www.troubleshooters.com/
Troubleshooting Training * Human Performance