RE: [Boost-Users] Re: [BGL] Best way to abort an algorithm from w ithi n a visitor?
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!
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@mmltd.net]
Sent: Thursday, September 19, 2002 9:20 AM
To: Boost-Users@yahoogroups.com
Subject: Re: [Boost-Users] Re: [BGL] Best way to abort an algorithm from
withi n a visitor?
From: "Mark Storer"
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@yahoogroups.com Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
From: "Mark Storer"
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!
This is all subjective. Original intents do not stay that way forever, as the language and our understanding of it evolves. What you think is "right" today will become "wrong" tomorrow. You can only decide what is right by using (somewhat) objective measures. Code clarity, maintainability, performance, correctness, that sort of thing.
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.
Possibly. But not for the reasons stated, that exceptions must not be used _that_ way because well I say so. And even Stroustrup kind of said so, so there. You are wrong! You fiend.
participants (2)
-
Mark Storer
-
Peter Dimov