Why we need try…finally, not just RAII

The claim has been made that because C++ supports RAII (resource acquisition is initialization), it doesn’t need try…finally. I think this is wrong. There is at least one use case for try…finally that’s very awkward to model with RAII, and one that’s so ugly it ought to be outlawed.

The awkward use case

What if what you want to do during a …finally block has nothing to do with freeing resources? For example, suppose you’re writing the next version of Guitar Hero, and you want to guarantee that when your avatar leaves the stage, the last thing she does is take a bow–even if the player interrupts the performance or an error occurs.

…finally, take a bow. Photo credit: gavinzac (Flickr)

Of course you can ensure this behavior with an RAII pattern, but it’s silly and artificial. Which of the following two snippets is cleaner and better expresses intent? Continue reading

All I Really Need To Know I Didn’t Learn In Compugarten

I’m glad newly minted software engineers are exposed to data structures, compilers, concurrency, graph theory, assembly language, and the other goodies that constitute a computer science curriculum. All that stuff is important.

But it’s not enough.

Not all classroom material for CS folks should be technical. Photo credit: uniinnsbruck (Flickr).

Since I’m half way to curmudgeon-hood, I frequently find myself lamenting educational blindspots in the young. I’ve even toyed with the idea of teaching at the nearest university, some day when I Have More Timeā„¢. If academia would take me, my lesson plans might cover some of the following topics:

What Should Be In The Next C++ Compiler?

Herb Sutter (champion of VC++ compiler evolution at Microsoft) posted an interesting poll today. He wants to know what the C++ developer community thinks about priorities for the next release.

Go vote.

Editorial comment: I’m not surprised that conformance is winning over performance or fancy features. MS’s compiler has always been easy to use in a vacuum, but a pain when interoperability and cross-platform matter. When Herb went to MS, things improved drastically, but my perception is that gcc leapfrogged VC++ a year or two back, as C++11 began to gel. (VS 2012 probably gets back to parity again; I haven’t poked into it deeply, yet.)