[erlang-questions] string:substr/2 gives confusing error message

Hynek Vychodil vychodil.hynek@REDACTED
Fri Dec 5 14:35:23 CET 2008


On Fri, Dec 5, 2008 at 2:11 PM, mats cronqvist <masse@REDACTED> wrote:

> "Hynek Vychodil" <vychodil.hynek@REDACTED> writes:
>
> > Because:
> >
> > Adding exception raise in this case is typical defensive programming
> > and it is not good practice in Erlang except pattern match with
> > tagging.
>
>   so, a function raising an exception when given bad data is not good
>  practice in Erlang?


Yes, absolutely. When you don't know what to do, die. see [1] As you wrote
below, now you think you know what to do, but in this case just do what to
do. Raise different exception is not solution. That's all.


> what gave you that impression? I'd say that the
>  opposite is true. "The erlang way", as i was taught it, is to fail
>  early and let the supervisor sort it out.
>
>  also, the way i interpret "defensive programming" is when a function
>  continues despite being given bad data. a typical example would be;
>
>  string:substr("a",2) -> []
>
>  The fact that
>
>  string:substr("a",2)
>
> and
>
>  string:substr("",2)
>
>  behaves differently is is just silly. the best would be is they both
>  failed with badarg. second best if they both returned [].
>
>  mats


You found matter of the problem. But I think both could return empty list.

[1] http://www.sics.se/~joe/thesis/armstrong_thesis_2003.pdf

-- 
--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081205/0dd54fbd/attachment.htm>


More information about the erlang-questions mailing list