lua-users home
lua-l archive

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




Am 16.08.2017 um 11:49 schrieb Dirk Laurie:
Suppose your program has some very short actions in an if.

if condition then call_my_routine() else return end

Do you indent it as:

if condition then
   call_my_routine()
else
   return
end

or as

if condition
   then call_my_routine()
   else return
end
if condition
   then call_my_routine()
   else return
end

or not at all?
First option.

Additionally, I tend to put the shorter execution into the then part; so it may read as :

if not condition then
    return
else
    call_my_routine()
    and some more
    lenghty
    stuff
end

As the return is very short, I tend to put it in one line:

if condition then return end

remaining
lengthy
process


Here a real-world example that tests conditions one by one and
immediately pops out on first matching:

function Analysis.ShortRingRate( rule, cdr, params )
    if not cdr.ddi or cdr.answered then return "n/a" end
    if cdr.ended - cdr.started >= params.max then return "too long" end
if increment( CallsToDDI, cdr.ddi, params.wndw ) >1 then return "same DDI" end if increment( rule, "count", params.wndw ) < params.warn then return "need more" end

email.send{ body="WarnShortRingRate", cdr=cdr, rule=rule, params=params }
    return true
end



--
Oliver