caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Radu Grigore <radugrigore@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] assertions or exceptions?
Date: Thu, 15 Jul 2004 11:03:24 +0300	[thread overview]
Message-ID: <7f8e92aa04071501035091cbe9@mail.gmail.com> (raw)

Hello,

First of all let me warn you that I am a beginner when it comes to
OCaml: I have experience in programming with other languages (C++), I
have read about OCaml, but I have only written about 1000 lines of
OCaml code, and only two "programs" were actually useful.

The purpose of this mail is to gather opinions on when should
assertions be used and when exceptions should be used.

I have noticed that the standard libraries use exceptions heavily.
This feels strange to me because I prefer to use assertions whenever
possible.

There are other libraries, notably Java API and .NET class library,
that use exceptions liberaly. But they don't bother me that much
because they try to give you the option of not relying on them
throwing exceptions. Whenever possible they provide functions that
check parameters and return a boolean. In [6] an example is given:
"whenever a FileNotFind exception might be thrown by method A, there
should be a method B that checks for this and returns a boolean".
However in OCaml I don't know how to tell if I've reached the end of a
file without using exceptions.

But why is a boolean better than an exception? Because it makes code
simpler. Exceptions can be left uncaught and this is a potential cause
for a combinatorial explosion of the number of possible "execution
paths" (tricky notion in a functional language, I know. For example
Herb Sutter in [5] identifies 23 execution paths in a 4-line C++
program because of exceptions! This kind of complexity makes it hard
to reason about the correctness of a program and is one of the reasons
that made Dijkstra say goto is generally a bad idea [2]. This doesn't
mean that it is _always_ a bad idea.

There is an OCaml programming guidelines document [3] that say that
_assertions_ (not exceptions -- my note) should be used to check
preconditions. There is also a tutorial on assertions from Sun [1]
that finally gives some hints on when to use assertions and when
exceptions: assertions should be used for preconditions of private
methods and other internal invariants (including postconditions),
while exceptions should be used only for checking preconditions of
non-private methods. But I think the reason given for using exceptions
is weak: we need to honor a contract that includes exceptions. So, we
need to use exceptions because we said at some point in time that we
will use exceptions. Not a very good argument. What's more Herb Sutter
argues here [4] that a contract that includes exceptions is actually
not that good in practice.

So, now a few questions:
1. Is my impression that OCaml standard library is abusing exceptions correct?
2. Is it safe to assume that exceptions complicate functional programs
as much as they complicate imperative ones?
3. Is it possible to avoid using exceptions and read a text file
line-by-line until EOF?
4. When do you use assertions and when do you use exceptions?

regards,
 radu

[1] http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html
[2] http://www.cs.utexas.edu/users/EWD/transcriptions/EWD01xx/EWD117.html
[3] http://pauillac.inria.fr/caml/FAQ/pgl-eng.html
[4] http://www.gotw.ca/gotw/082.htm
[5] http://www.gotw.ca/gotw/020.htm
[6] http://tinyurl.com/2opdo

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


             reply	other threads:[~2004-07-15  8:03 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-15  8:03 Radu Grigore [this message]
2004-07-15 10:18 ` Richard Jones
2004-07-15 10:28   ` Daniel Andor
2004-07-15 12:49   ` Radu Grigore
2004-07-15 13:33     ` Richard Jones
2004-07-15 13:58       ` Radu Grigore
2004-07-16 18:53         ` Aleksey Nogin
2004-07-17  2:55           ` John Prevost
2004-07-17 14:24             ` David MENTRE
2004-07-15 12:35 ` Jon Harrop
2004-07-15 13:45   ` Radu Grigore
2004-07-15 14:33     ` Jon Harrop
2004-07-15 15:05       ` Radu Grigore
2004-07-15 16:24     ` skaller
2004-07-15 15:38 ` [Caml-list] Unboxing options, was " Brian Hurt
2004-07-15 16:25   ` John Hughes
2004-07-15 17:00     ` Brian Hurt
2004-07-15 17:20   ` John Prevost
2004-07-15 19:14     ` Radu Grigore
2004-07-15 19:56     ` John Carr
2004-07-15 20:48       ` Brian Hurt
2004-07-15 20:49         ` John Carr
2004-07-15 21:15           ` John Prevost
2004-07-15 21:15           ` Karl Zilles
2004-07-15 21:26           ` Brian Hurt
2004-07-15 21:04       ` John Prevost
2004-07-15 21:17     ` skaller
2004-07-15 21:35       ` Brian Hurt
2004-07-15 21:51         ` skaller
2004-07-15 21:42       ` skaller
2004-07-16  0:35     ` Jacques GARRIGUE
2004-07-16  1:03       ` John Prevost
2004-07-16  2:00         ` Jacques GARRIGUE
2004-07-16 16:40         ` Xavier Leroy
2004-07-19  8:58           ` Damien Doligez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7f8e92aa04071501035091cbe9@mail.gmail.com \
    --to=radugrigore@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).