[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Making vars local by default
- From: Tom _ <tom7ca@...>
- Date: Wed, 30 May 2001 06:55:33 -0700 (PDT)
|No, i absolutely disagree. At first it may appear
|to you that life would be easier with default
|local variables, but in the end it's not. It's
|all about scope and when you have local variables
|per default you either loose some scope or have
|clumsy commands.
The issue isn't convenience, it's safety. People
don't usually accidentally add declarations,
but they forget to add necessary declarations
all the time. What are the consequences?
If the default is global and you forget to
declare a variable "local", your program seems
to work most of the time, but it actually has a
serious bug: that variable, which you assume is
local, can get clobbered by any function you call,
and it won't be preserved during recursive
calls. What is worse, that bug usually only
shows up a long time later when you change
something completely unrelated and your program
mysteriously fails.
If the default is local and you forget to
declare a variable that should have been declared
"global", you'll find out very quickly because
a value that you expected to be set to something
will be uninitialized: failure is immediate and
obvious.
Global-by-default is a very dangerous default.
The situation is aggravated by the fact that Lua
is unusual among scripting and programming
languages with this default these days (almost
all other languages that had this default have
abandoned it), which means that programmers are
particularly prone to making this error. I think
this really needs to get fixed one way or another.
I think a relatively easy and painless way of fixing
it is to introduce a new assignment operator that
automatically declares the variable being assigned
to "local". That way, safety-conscious programmers
can make it a habit to use ":=" by default.
Thomas.
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year! http://personal.mail.yahoo.com/