lua-users home
lua-l archive

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


On Wed, Jun 11, 2014 at 10:51 PM, Andrew Starks <andrew.starks@trms.com> wrote:

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

Before I say more I just want to say that I rarely need the
fallthrough behavior of a C-like switch.  I only need it when handling
exception-like code using pcall() -- I feel the handling is similar.

Anyway, I usually write those horrid elseif chains like so:

if     v == thing1 then do_something()
elseif v == thing2 then do_something_else()
elseif v == thing3 then do_another_thing()
else                    do_whatever()
end

(This will only look aligned for people viewing their emails with
fixed-width typefaces)

Basically I align the == after the if/elseif's and everything after
the then's/else.  Usually my chains have only one or 2 statements in
the "body" portion so I try to make each condition a single line.

While this looks concise and is easy to skim for me, it goes against
the style preferences of others I show my Lua with.  I wish a standard
switch would alleviate some of my guilt :(  I do think goto has its
place, it can make some things easier to express -- I think a switch
would be more appropriate for most cases.

The table-using switch works but doesn't look as clear..

Anyway, I'm off-topic.