lua-users home
lua-l archive

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





On Wed, Jun 11, 2014 at 11:45 PM, Coroutines <coroutines@gmail.com> wrote:
Something I think other people undervalue is how easy code is to skim.

if v == 'value1' then
  do_something()
elseif v == 'value2' then
  do_something_else()
  goto whatever
elseif v == 'value3 then
  do_another_thing()
else
::whatever::
  do_whatever()
end

While this is readable, I feel like this is more comfortable:

switch condition do
  'value1' then
    do_something()
    break
  'value3' then
    do_another_thing()
    break
  'value2' then
    do_something_else()
  else
    do_whatever()
end

I'm not sure if this is the exact syntax I would want.  It's important
that the value being compared is top-level, and what it is being
compared against is on the same level of indentation -- also
fall-through is possible.  I find long elseif chains a bit skewing,
especially when using something like goto in the mix.  If I want to
understand code quickly, I expect "skim value" in how I can express
those comparisons.  Shrug it off as convenience for lazy programmers ~


In the mean time, perhaps a simple trick might provide some skim, when more is needed...

if v == 'value1' 
  then
  do_something()
elseif v == 'value2'
  then
  do_something_else()
  goto whatever
elseif v == 'value3
  then
  do_another_thing()
else
::whatever::
  do_whatever()
end

I find myself using multiple lines with Lua, very often. It's almost a style of programming, especially for obvious things, like passing a function literal as an argument.


-Andrew