[erlang-questions] is_integer should always be used?

Richard A. O'Keefe <>
Thu Jul 3 02:50:46 CEST 2008


On 2 Jul 2008, at 8:38 pm, not norwegian swede wrote:
> when programming erlang, should i always use is_integer if a float  
> will make the function crash?

Think about WHY you want to use it.
Do you want to make the function crash?  But you've said it will anyway.
To make the crash report more helpful?  Could be useful.
To improve the results from the Dialyzer?  Are you using it?  What  
does it say?
To express your intentions?  Best of all.
>
>
> i havent used so much exception-prevention in erlang and it seems  
> the language needs a lot of extra code stuck it in ebcause of this.

Erlang doesn't need any more error detection code than other languages.
Some of what might have been type declarations in other languages might
show up as executable tests, but on the whole, Erlang tends to use if
anything less.
>
> if some one passes a float to a function that only takes integers,  
> should i catch that exception or should i let the program crash?

Do read Joe Armstrong's thesis.  It is plain language with clear ideas
and explains the Erlang design philosophy.  Part of that philosophy is
"let it crash".  To really understand that, read the thesis.  But here
I'll say that you should only catch an exception if there is something
useful you can DO about it.  If you can figure out what to do after an
exception, why didn't you write the code to do that in the first place?
Generally speaking, exceptions should be handled some upper level that
doesn't care too much about the details of what went wrong (except for
logging) but DOES know what it can do instead.





More information about the erlang-questions mailing list