[erlang-questions] How do I elegantly check many conditions?
Hynek Vychodil
vychodil.hynek@REDACTED
Tue Mar 24 23:06:18 CET 2009
On Tue, Mar 24, 2009 at 9:24 PM, mats cronqvist <masse@REDACTED> wrote:
> Hynek Vychodil <vychodil.hynek@REDACTED> writes:
>
> > code
> >
> > ok = is_new_email1(Email).
> >
> > doesn't looks like same amount of code
> >
> > try is_new_email2(Email) catch throw:email_in_use -> email_in_use end.
> >
> > First one turns return value to exception, second one turns exception to
> > return value. Second one seems more code, does not?
>
> but the exception generated in example 1 is a worthless 'badmatch'.
>
I can flexible choose which worth-full 'badmatch' will be generated just
using {ok, Email} = {is_new_email1(Email), Email}. It all up to me. It is
meaning of word 'flexible'.
> > Even code
> >
> > is_new_email1(Email) ->
> > case mnesia:dirty_read(email, Email) of
> > [] -> ok;
> > _ -> email_in_use
> > end.
> >
> > seems less than
> >
> > is_new_email2(Email) ->
> > case mnesia:dirty_read(email, Email) of
> > [] -> ok;
> > _ -> throw(email_in_use)
> > end.
>
> I think it should be written like this;
>
> is_new_email2(Email) ->
> try [] = mnesia:dirty_read(email, Email)
> catch _:_ -> throw({email_in_use,Email})
> end.
>
> This is how your example 1 above has to be written too, if it is to
> give a helpful error message.
>
Yes, but what prevent me
is_new_email1(Email) ->
case mnesia:dirty_read(email, Email) of
[] -> ok;
_ -> {email_in_use,Email}
end.
and again, less code and more flexible. Each variant with throw can be
written without throw in far flexible way.
> mats
>
--
--Hynek (Pichi) Vychodil
Analyze your data in minutes. Share your insights instantly. Thrill your
boss. Be a data hero!
Try Good Data now for free: www.gooddata.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090324/b20e0f15/attachment.htm>
More information about the erlang-questions
mailing list