lua-users home
lua-l archive

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


2014-04-10 15:59 GMT+02:00  <stuart@testtrack4.com>:

> The idea is simple: allow a fixed character on the right-hand side of an
> assignment statement to refer to the left-hand variable(s). For example,
> if I have a variable like this:
>
>     local extremely_long_variable_name = 1
>
> and I want to increment it by 1, I have to type its name twice:
>
>     extremely_long_variable_name = extremely_long_variable_name + 1
>
> If there was a shorter way of describing the variable being assigned
> (let's use "@" as an example), this code could be much simpler:
>
>     extremely_long_variable_name = @ + 1

Why does one use very long names? Because they are self-documenting.
Personally, I find the practice somewhat Cobolic, but I have nevertheless
indulged in it from time to time.

They are ideal for global names, for field names in some database — but
not so obviously necessary for local variables. By definition, local variables
have a limited scope, and there can't be  many of them. In fact, you can't
simultaneously have even so much as half of a1,a2,...,a9,b1,b2,...z9.

Why not just:

do
   local s = extremely_long_variable_name    -- global or upvalue
   -- all the things you want to do to it, such as s=s+1
   extremely_long_variable_name = s
   -- not needed if it is a table, and you have just been modifying its contents
end

Like '@', there is only one letter in 's'. It is at least as easy to read,
and there can be 26 different ones at the same time.