RAII
Joel's blurb on exceptions and some of the comments around here IMHO show one thing:
RAII (Resource Aquisition Is Initialization) is the single most misunderstood (or unheard of?) concept in modern programming languages and especially C++.
I'd even say that most of the criticism about C++ is based on that single misunderstanding. Whenever I see "C++ should have finally" or "all those try/catch constructs obfuscate" and most of the memory-management-problems people talk about *most of the time* it's obvious that they don't fully understand how to best use exceptions.
You DON'T need many try/catch-blocks when your classes are designed after RAII-idiom. There are cases where it's not possible (few) or practical to apply RAII and you can use whatever technique you want there. But: Exceptions are glorified gotos only when mis-used!
Sebastian Wagner
Tuesday, October 14, 2003
One comment, paraphrased, I remember from slashdot:
Any language where a person (Scott Meyers) can make a living pointing out all of the different ways you shouldn't use a language is overly complex.
Tuesday, October 14, 2003
That depends on your needs and taste.
"overly complex" or "complexity comes from expressiveness"
Sebastian Wagner
Tuesday, October 14, 2003
As Einstein would say: "Example isn't another way to teach, it is the only way to teach.". So please give an example.
Michael Bruckmeier
Tuesday, October 14, 2003
Expressiveness does not beget complexity: See Lisp or Scheme.
asdf
Tuesday, October 14, 2003
Here is an example of RAII:
#include <iostream>
class Test
{
public:
Test() {
std::cout << "Object constructed." << std::endl;
}
~Test() {
std::cout << "Object destroyed." << std::endl;
}
void Method() {
std::cout << "Method called" << std::endl;
}
};
class SmartPointer
{
private:
Test* m_ptr;
public:
SmartPointer(Test* ptr)
: m_ptr(ptr) {
}
~SmartPointer() {
delete m_ptr;
}
Test* operator->() {
return m_ptr;
}
};
int main(int argc, char* argv[])
{
try
{
SmartPointer ptr(new Test);
ptr->Method();
std::cout << "Throwing exception." << std::endl;
throw std::runtime_error("Exception");
}
catch(...)
{
std::cout << "Catching exception." << std::endl;
}
return 0;
}
The output is:
Object constructed.
Method called
Throwing exception.
Object destroyed.
Catching exception.
kmo
Tuesday, October 14, 2003
Sebastian, yours is the first sane comment I've seen in the last 2 days. Between this and a SlashDot post about a latest Strostroup interview, I have only one thing to say: there are A LOT of mediocre programmers around, and Joel Spolski is one of them.
For me mediocrity isn't simply the inability of writing great code. But most important it is the inability of LEARNING how to write great code. It is the lack of flexibility of the mind. The desire to cling on the well known techniques and the fear of the new things.
This "exceptions are bad" post is pretty funny coming from a person claiming that he only wants to hire "superstar programmers"!!! I am a superstar and I sure know that I'd never work for this guy...
Nick
Nick
Tuesday, October 14, 2003
Just to clarify: I don't want to imply that Joel is a mediocre programmer.
Sebastian Wagner
Tuesday, October 14, 2003
As for resources (hehe) on RAII: I think the well-known authors (Stroustrup, Sutter, Meyers etc.) have published about this, but you can also try
http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/
This article has a nice overview on how to write exception safe code. It gives an IMHO nice and quick example of what RAII means. After that it goes down a different route (one that you can debate about but is interesting anyway), that is meant to be more practical in real life ... whatever you think about that...
Sebastian Wagner
Tuesday, October 14, 2003
"I have only one thing to say: there are A LOT of mediocre programmers around, and Joel Spolski is one of them."
Whereas you can't even be bothered to spell Joel's name correctly!
Tuesday, October 14, 2003
Sebastian, great posts! You've said it better than I could have!
Joel, speaking straight from the Microsoft School of Programming! Return codes to indicate error status -- is this April 1??? The C++ exception model is superior to the C#/Java/VB models, specifically due to the automatic cleanup RAII model. C++ client programmers do not generally need to be concerned about what exceptions are thrown, or even generally where, when using properly written exception-based code, only whether an exception may or may not actually be thrown in any given place (certain functions are formally defined as never throwing exceptions, which allows other guarantees to be built on top of this). This is defined as the "exception safety guarantee model", and is fully implemented in the c++ model, but is completely lacking in C#/Java. To anyone who is not familiar with C++ exception safety guarantees, please read up at www.gotw.ca or any of the articles written by David Abrahams (such as http://www.boost.org/more/error_handling.html ), one of the top C++ exception gurus.
PS, to the other writer, "catch (...)" *IS* actually an evil glorified goto, and actually should NOT be used (especially not the way you did it with a cout in your example). See the Abrahams paper listed above for more info on that.
Hillel Y. Sims
Tuesday, October 14, 2003
I'm increasingly viewing C++ as a professionally-acceptable sibling to Lisp-like languages (CLisp, Scheme, ML) in many ways.
Between templates, the different lambda libraries, Spirit (Parser generator using template metaprogramming), and other such things, you can use an astonishingly large number of different programming paradigms other than just the usual object-oriented paradigm.
Flamebait Sr.
Tuesday, October 14, 2003
I wrote <http://www.hackcraft.net/raii/> when, like Sebastian, I was beginning to feel that RAII wasn't well-enough known about and understood. In hindsight it's a bit long for something where part of the value is in its simplicity but somebody might find it useful.
Jon Hanna
Thursday, October 23, 2003
http://www.hackcraft.net/raii/ even. The comment boxes don't take <> delimited URIs á la RFC 2396 :(
Jon Hanna
Thursday, October 23, 2003
Recent Topics
Fog Creek Home
|