lua-users home
lua-l archive

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

On 13 March 2017 at 13:59, Coda Highland <> wrote:
> I think there's a reasonable argument for
> local a, b, c, d =
>       e, f, g, h
> This gives you some level of alignment between value and variable that
> one-lining it doesn't, while being more compact than a series of
> one-variable assignments.

I think this a case where the over-simplified example is misleading.
It looks pretty with `a, b, c` but in the real world with significant
names, this becomes a pain to maintain, and when we stuff too much in
a single line, diffs are harder to read.

Things always look good in tiny examples with single-letter variables.
It's why I think it's so disengenious to defend `t[#t+1] = v` as the
preferred idiom for appending to an array. It looks good with a
single-letter variable and five-line tutorial examples, but in the
real world we use nested tables. In the end,
`table.insert(my.nested[data], v)` is both more readable and avoids
repetition (i.e. it's one thing less to have a typo on, one thing less
to change, one thing less to get out of sync). Note how in a realistic
example the variable name dominates the statement.

Do I think `table.insert` is too long? Yes I do, I wouldn't mind
having a shorter idiom (many were proposed here over the years, most
of them were fine, but I'm not getting into them because we risk
delving into syntactic bikeshedding again, so let's avoid that).

Do I think it's worth it to add `local tinsert = table.insert` to
every program? No, because I hate having to guess which abbreviation
the module author used to write a shorter `table.insert` in their code
(I've even seen `local append = table.insert` in the wild!). And then
again, the abbreviation doesn't gain us much: being comfortable to
read is more important than being comfortable to write, but being easy
to maintain is just as important if not more. (And before someone
mentions, the performance gains for localizing such variables are
overstated; done at the top of modules they become upvalues and not
true locals; and most of the modules I've seen doing this are far from
being aimed at performance-intensive tasks that would warrant this
kind of micro-optimization).

Back to the original topic of assignment style, I'm 100% with
Daurnimator in his assessment of Dirk's original post.

-- Hisham