One of Joe's suggestion: program success case code separated from error 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>note that you can do all errors catches out of create_user function which is better.<br>
<br>create_user(Email, UserName, Password) -><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><br>main() -><br> try<br> ...<br> some_function_where_create_user_is_called(),<br> ...<br> catch<br> ...<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 class="gmail_quote">On Fri, Mar 20, 2009 at 2:20 PM, ryeguy <span dir="ltr"><<a href="mailto:ryeguy1@gmail.com">ryeguy1@gmail.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;">
So when a user sends a request to register an account, they send their<br>
username, password, email, and other info. The registration function<br>
must verify all of their data. An example would be:<br>
<br>
- verify email not in use<br>
- verify username not in use<br>
- verify username is alphanumeric<br>
- verify all fields are above X characters long<br>
- verify all fields are less than Y characters long<br>
<br>
Now I don't want to have a 5 level deep if or case statement, but what<br>
other options do I have? Splitting it into separate functions sounds<br>
like a good idea, but then I just have to check the return value of<br>
the functions in some sort of conditional and it's back to the<br>
original problem.<br>
<br>
I could separate them into functions and then call an if statement<br>
with all of the conditionals OR'd together, but that wouldn't give me<br>
what I want because I need to be able to tell the user the specific<br>
error if there was one.<br>
<br>
How does one handle this kind of situation in erlang?<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br><br clear="all"><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>