lua-users home
lua-l archive

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


On Wed, Dec 17, 2014 at 2:25 AM, Hao Wu <wuhao.wise@gmail.com> wrote:
> I think OP's point is return can be a function so it can be baked into the
> expression v.s. a single statement, for simplicity.


yes, i was just pointing a third option that is shorter, more readable
(IMHO) and currently valid.

when any function has some 'fail fast' condition(s), i like to add one
or more such 'guard lines' as close to the top as possible.  also, if
the "then" clause ends with a return, i don't put an "else" for the
rest of the function, avoiding useless indentation and too many 'ends'
at the end.

function division(x,y)
    if y==0 then return nil, "division by zero" end
    return x / y
end

instead of:

function division(x,y)
    if y==0 then
        return nil, "division by zero"
    else
        return x/y
    end
end

the OP proposal would be:

function division(x,y)
    if y!=0 or Return(nil, "division by zero") then
        return x/y
    end
end

which i think is much less readable than either of the others, and not
shorter than the first one.

-- 
Javier