Try/Cond
Joe Armstrong
joe@REDACTED
Thu Oct 23 16:53:12 CEST 2003
On Thu, 23 Oct 2003, Francesco Cesarini wrote:
> >
> >
> > What - you're nuts
> >
> It is pretty well known ;-)
It was a complement :-)
>
> >>With the introduction of OTP in 1996, I have never seen anyone use exit
> >>in their code. The few catches and throws I came across were easily
> >>rewritten into a more purely functional style.
> >>
... cut ...
On mature refelection there might be a case for being restrictive
with catches and throws but never with exits - let's suppose you write
last to compute the last element of a list.
You might write it like this:
last([_|T]) -> last(T);
last([H]) -> H.
This version works because you "know" that
1) It's dingo dongo stupid to ask for the last element of
an empty list
2) last([]) will fail with a pattern matching error and
3) any process that asks for the last element of an empty list
is wayo damaged beyond control and should be killed
But all of this is *implicit* knowledge.
When you read code like this my brain automatically wonders --
wonder why there is no base case for the recursion - I wonder if the programmer
forgot it (actually a type system will go though the same kind of
reasoning as well, so it's not a bad idea to be explicit).
If you write it like this:
last([_|T]) -> last(T);
last([H]) -> H;
last([]) -> exit(emptyListsDoNotHaveLastElementYourProgramIsNuts).
The you will at least know that the programmer had thought about it.
<afterthought>
You could do this with an appropriate comment instead
last([_|T]) -> last(T);
last([H]) -> H.
%% YIKYDHTTM
</afterthought>
>
> Got to go! The men in the green shirts are knocking on the door.
>
There you go -
/Joe
More information about the erlang-questions
mailing list