lua-users home
lua-l archive

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


> tom7ca@yahoo.com wrote:

> 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.

No. In both versions failures are depending on
the situation. In both versions the failure can
be long hidden or found immediately.

In small programms global vars by default are welcome
because you don't have to worry about variable scopes.
Use them and its ok.

In big projects global variables are also welcome
because you don't need to declare it global again and
again in each function who need them. Instead you
declare the locals because you use them only here
and once.

Lua can do locals whenever you need. So locals are
independet from functions.

> Global-by-default is a very dangerous default.

You can program errors in each situation. It can't
not the job of the language to prevent errors. Sure,
it should help you, but not in the way to make things
complicated or to prevent features. You can simple
delete files with one command. You would never say
this is so dangerous let us make a complicate command
to prevent this.

> 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),

Almost all other languages aren't as good as Lua is.
Other languages are not reference. I hate this type
arguments: most do it so it most be done so (or: so
it must be good). Its possible that all do the right
but *others do it so* its not the argument.

> which means that programmers are
> particularly prone to making this error.  I think
> this really needs to get fixed one way or another.

Unfortunatly in the last weeks I programm only in php
and I think globals by default are a pain.

Kind regards



Markus