lua-users home
lua-l archive

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




On 14/08/15 08:37 PM, Mike Nelson wrote:

On 14/08/15 04:08 PM, Jonathan Goble wrote:

This can be written in stock Lua without any new features by using
nested if blocks:

local flag = false
if i == 1 or i == 2 then
   if i == 1 then
     flag = true
   end
   print(flag)
else
   print"Unknown!"
end

But now you check for the condition twice.

This is easily re-written in DRY style:

local flag = i==1
if flag or i==2 then
    print(flag)
else
    print"Unknown!"
end

Still very clear and reduces the wordiness almost as much as the orif proposal. Sometimes a good style is just as good as syntactic sugar.

You assume it's a simple flag change without side effects.

Add side effects and a more complicated change (a whole setup for example) and you'll easily see where my thing is better.

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.