Boost logo

Boost Users :

From: Mark Storer (mstorer_at_[hidden])
Date: 2002-09-19 12:31:09


In general, I agree that taking personal issue with the use of some language
feature is a little silly.

In specific, I'm going to disagree with you both for exceptions and the
whole do{}while(0) thang.

I'm sitting next to my copy of Stroustrup's "The C++ Programming language"
(2nd edition). Chapter 9 covers exceptions. This chapter makes it clear
that the purpose of exceptions is to handle errors:

"
The fundamental idea is that a function that finds a problem that it cannot
cope with /throws/ an exception, hoping that its (direct or indirect) caller
can handle the problem.
"

Same thing with the do{}while(0) construct. Using a loop as an error
handler is quite similar. One could argue that a break is far more
efficient than a throw. A counter-argument is that the throw carries a
great deal more information about what went wrong. Once the excreta impacts
the air-pusher-thingy, speed becomes a secondary concern (far more often
than not).

If you want to abort a function due to an error then throw an exception. If
you want to return a value then do so.

And while using a language feature for something other than its original
intent is not criminal, there are usually 'proper' ways to do things already
built into the language, ways that often work better than subjecting some
poor unsuspecting feature to your vile, unnatural manipulations. <Dudly
Do-Right>You fiend!</Dudly Do-Right>

In the case that brought all this up (returning a value from a graph's
visitor to avoid iterating through the whole graph) the proper way to handle
it is to alter the algorithm to allow for early termination.

If Boost's source were a closed book, and the only option was to throw an
exception, then so be it. But that is not the case.

So do it right... as Chris Russell (the original poster) has decided to do.

--Mark Storer
  Software Engineer
  Cardiff Software

#include <disclaimer>
typedef std::disclaimer<Cardiff> Discard;

-----Original Message-----
From: Peter Dimov [mailto:pdimov_at_[hidden]]
Sent: Thursday, September 19, 2002 9:20 AM
To: Boost-Users_at_[hidden]
Subject: Re: [Boost-Users] Re: [BGL] Best way to abort an algorithm from
withi n a visitor?

From: "Mark Storer" <mstorer_at_[hidden]>
> Using exceptions in this way just bugs me. Religious reasons I guess. As
> the Esteemed Mr. Hylsop ;) mentioned earlier, exceptions are for "oops".
> This seems alot like the whole "do{}while(0)" with various 'break'
> statements sprinkled in to avoid multiple returns/nested ifs. Useful, but
> Just Not Right.

A common misconception. Many people try to project their own
ideas/expectations on language features, rather than simply using them as
tools where appropriate. There is nothing "oops"-y about exceptions. They
are a control flow mechanism; one that has interesting properties, but a
control flow mechanism nonetheless. If it works and leads to a better code,
it's foolish to avoid it without a technical reason.

A similar example is when people don't use "struct" for C++ classes because
it "doesn't feel right", or when trying to choose between "typename" and
"class" for template parameters. The language features simply do not carry
this kind of religious/emotional payload. It's all in your mind.

Info: <http://www.boost.org>
Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl>
Unsubscribe: <mailto:boost-users-unsubscribe_at_[hidden]>
 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net