[erlang-questions] How do I elegantly check many conditions?

Paul Fisher pfisher@REDACTED
Fri Mar 20 18:36:21 CET 2009


Gordon Guthrie wrote:
> 
> 
> On Fri, Mar 20, 2009 at 2:34 PM, Paul Fisher <pfisher@REDACTED 
> <mailto:pfisher@REDACTED>> wrote:
> 
>     Hynek Vychodil wrote:
>      > One of Joe's suggestion: program success case code separated from
>     error
>      > handling. You can make it in this way
>      >
>      > create_user(Email, UserName, Password) ->
>      >   try
>      >     ok = new_email(Email),
>      >     ok = valid_user_name(UserName),
>      >     ok = new_user(UserName),
>      >     ok = strong_password(Password),
>      >     ...
>      >     _create_user(Email, UserName, Password)
>      >   catch
>      >     error:{badmatch, email_in_use} -> do_something();
>      >     error:{badmatch, invalid_user_name} -> do_something();
>      >     error:{badmatch, user_exists} -> do_something();
>      >     error:{badmatch, weak_password} -> do_something();
>      >     ...
>      >   end.
> 
>     +1 Bravo! 
> 
> 
> 
> This solution is not good for the user though...
> 
> It throws an error on first failure, and if (say on a web form) the user 
> has made many errors they get caught in a fix/fail cycle which drives 
> them (<--- read me here) mad!

Then do this with failures returning the same atom values as above:

create_user(Email, UserName, Password) ->
     case lists:usort( [new_email(Email),
                        valid_user_name(UserName),
                        new_user(UserName),
                        strong_password(Password)] ) of
         [ok] ->
             _create_user(Email, UserName, Password);
         Failures ->
             _notify_failures( Failures )
     end.



--
paul



More information about the erlang-questions mailing list