lua-users home
lua-l archive

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


On Thu, Jul 1, 2010 at 5:18 PM, liam mail <liam.list@googlemail.com> wrote:
> If I were to be totally honest the thing which I dislike most about Lua is
> the keyword 'local', I dislike typing it over and over again and that the
> fact the default scope is global. Having edited third party code, it also
> seems to encourage the use of the globals although this is not to say that
> the same authors would have used scoped variables in other languages.

The "Huffman encoding" language design principle in Perl says that
common operations should have shorter syntax than less common
operations.  A slightly different formulation expressed in the Lua
community is that dangerous operations should look ugly [1].  In Lua,
we have "a=b" for re-assignment or, if coding policy permits [2],
global variable definition, and we then have the longer "local a=b"
for lexical variable definition.  In the interest of minimizing
complexity (number of plausible states), users should strive to use
lexical variable definitions and minimize use of variable
re-assignments and globals, but the syntax may encourage the opposite
here.  The same criticism could be said of other procedural languages,
but a number of these use the shorter keyword 'var' [4], and Perl has
an even shorter though curiously named 'my'.  The Go language also has
a short-hand: using "a := b" rather than "a = b" is analogous to
"local a = b" in Lua.  Regardless of syntax though, the linter [2]
could limit the use of '=', and one's text editor could use color and
font to make less desirable operations like mutating assignment and
global variable definition more prominent [3], or in a way more
"expensive" syntactically.

BTW, concerning the relationship between Lua syntax and user coding
practice, what I see regularly is poorly indented Lua code when Lua is
used embedded (and perhaps not by professional programmers).  Baring a
draconian approach like Python, the solution again may be the linter
and text editor offering assistance.

[1] http://lua-users.org/lists/lua-l/2010-05/msg00481.html
[2] http://lua-users.org/wiki/DetectingUndefinedVariables
[3] http://lua-users.org/wiki/LuaInspect
[4] JavaScript, Go, and to some extent C#