Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

erlang phylosophy: Should I let users deal with wrong input or not?

Tags:

erlang

This is the case:

% print/1: Prints out the integers between 1 and N
print(0) ->   io:format("~w~n", [0]);
print(N) when is_integer(N) -> 
          io:format("~w~n", [N]),
          print(N - 1).

If the user inputs a non-integer, this happens:

11> effects:print('alfalfa').
** exception error: no function clause matching effects:print(alfalfa)

Is about phylosophy: Should I correct my program this way, in order to 'catch all' kinds of input?

% print/1: Prints out the integers between 1 and N
print(0) ->   io:format("~w~n", [0]);
print(N) when is_integer(N) -> 
          io:format("~w~n", [N]),
          print(N - 1).
% Last Line added:
print(_Other) -> false.

I'm new in erlang. Is there some convention for dealing with this?

Thanks!

like image 405
Herman Junge Avatar asked Nov 07 '11 22:11

Herman Junge


2 Answers

In Erlang you mostly would not catch such bad API usages. If no pattern matches the invocation, an exception of class exit with a rather verbose message would be thrown ({function_clause, CallStack}). Almost every standard library method throws. At the moment I fail to think of counterexamples.

Btw: You mostly would return {error, Msg}, not false, if there was some sort of error (mostly not usage error). In good cases ok or {ok, Datum} would be returned.

like image 92
kay Avatar answered Oct 03 '22 15:10

kay


This might be useful http://mazenharake.wordpress.com/2009/09/14/let-it-crash-the-right-way/

like image 20
niting112 Avatar answered Oct 03 '22 14:10

niting112