<br><br><div class="gmail_quote">On Mon, Mar 23, 2009 at 1:39 PM, Adam Lindberg <span dir="ltr"><<a href="mailto:adam@erlang-consulting.com">adam@erlang-consulting.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">----- "Hynek Vychodil" <<a href="mailto:vychodil.hynek@gmail.com">vychodil.hynek@gmail.com</a>> wrote:<br>
<br>
> One of Joe's suggestion: program success case code separated from<br>
> error<br>
> handling. You can make it in this way<br>
><br>
> create_user(Email, UserName, Password) -><br>
> try<br>
> ok = new_email(Email),<br>
> ok = valid_user_name(UserName),<br>
> ok = new_user(UserName),<br>
> ok = strong_password(Password),<br>
> ...<br>
> _create_user(Email, UserName, Password)<br>
> catch<br>
> error:{badmatch, email_in_use} -> do_something();<br>
> error:{badmatch, invalid_user_name} -> do_something();<br>
> error:{badmatch, user_exists} -> do_something();<br>
> error:{badmatch, weak_password} -> do_something();<br>
> ...<br>
> end.<br>
<br>
</div>Why use badmatch?<br>
<div class="im"><br>
create_user(Email, UserName, Password) -><br>
try<br>
</div> is_new_email(Email),<br>
is_valid_user_name(UserName),<br>
is_new_user(UserName),<br>
is_strong_password(Password),<br>
<div class="im"> ...<br>
_create_user(Email, UserName, Password)<br>
catch<br>
</div><div class="im"> email_in_use -> do_something();<br>
</div><div class="im"> invalid_user_name -> do_something();<br>
</div> user_exists -> do_something();<br>
<div class="im"> weak_password -> do_something();<br>
...<br>
end.<br>
<br>
<br>
</div>Validation functions can use throw:<br>
<br>
is_new_email(Email) -><br>
case mnesia:dirty_read(email, Email) of<br>
[] -> ok;<br>
_Else -> throw(email_in_use)<br>
end.<br>
<br>
Cheers,<br>
<font color="#888888">Adam<br>
</font></blockquote></div><br>If I have function like <br clear="all"><br>
is_new_email(Email) -><br>
case mnesia:dirty_read(email, Email) of<br>
[] -> ok;<br>
_Else -> email_in_use<br>
end.<br>
<br>I can choose if use it as<br><br>case is_new_email(Email) of<br> ok -> do_something();<br> email_in_use -> do_something_other()<br>end<br><br>or as<br><br>ok = is_new_email(Email);<br>
<br>When You trow exception, I can't choose.<br><h3><a style="font-weight: normal;" name="HDR3">Don't make assumptions about what the caller will do with the results of a function</a><span style="font-weight: normal;"> (<a href="http://www.erlang.se/doc/programming_rules.shtml#HDR3">http://www.erlang.se/doc/programming_rules.shtml#HDR3</a>)</span><br>
</h3><br>-- <br>--Hynek (Pichi) Vychodil<br><br>Analyze your data in minutes. Share your insights instantly. Thrill your boss. Be a data hero!<br>Try Good Data now for free: <a href="http://www.gooddata.com">www.gooddata.com</a><br>