Friday 16 February 2007

RAII and closures in Java

[This blog has moved and this article can now be found at http://www.levelofindirection.com/journal/2009/9/24/raii-and-closures-in-java.html. It still appears here for archival purposes]

One of the biggest reasons I still prefer C++ over newer brethren such as Java and C# is its support for RAII (Resource Acquisition Is Initialisation). Unlike C++, Java and C# have the finally construct, to give you a place to do clean up of resources. The trouble is this doesn't scale very well. If you have several resources in a scope you may have to nest try-catch-finally blocks. A good article describing this problem from a Java perspective is here.

Of course, in C# there is the using keyword, along with the IDisposable interface, which gives you a little more C++-like scoped disposal. Even this is less clean and more awkward than the C++ model, and I believe also has scalability problems (caveat - I haven't really used this in real C# programs).

One way to work around the lack of deterministic destruction in such languages is to use the Execute-Around idiom (more detailed coverage in this pdf). This allows you to factor out the resource acquisition, initialisation and release code, from the code that uses the resource. It scales better than inline try-catch-finally, but can result in much more fragmented (using a named class) or messy (using an anonymous class) code. In some cases the fragmentation may be a good thing - after all Extract Method is a valuable refactoring tool. But other times it may be too much, especially in such languages with a more imperative bias.

In languages with decent support for Closures, Execute-Around can be a much more natural and pleasant experience with less fragmentation (although more because of the side-effect of decent language syntax, than because of the formal benefits of closures).

But while C# has closures, as of .Net 2.0, Java doesn't. That is, not at the time of this writing. There are serious proposals afoot to add them to the language - and from the sound of this blog entry from Neal Gafter (former Java co-designer, now at Google),they should have all the features that make them useful for techniques such as Execute-Around (as well as a whole load of other benefits, of course, but you can read about this elsewhere - such as in the cited article).

Perhaps the time will yet come when I can use Java seriously without too many grimaces!

Thursday 15 February 2007

ACCU conference

How better to kick off my technoblog than to mention the upcoming ACCU conference, and, of course, my involvement in it :-)

For anyone wondering what the ACCU is, it stands for the Association of C and C++ Users, but its membership covers a broader range of, mostly C-family, languages (inc. Java, C# and a number of dynamic languages). The group has a number of industry names in its fold - and a friendly, pub-like, atmosphere.
The conference is, according to many, the best programming related conference around, attracting top names such as Stroustrup, Sutter, Alexandrescu, Meyers and many others. At the same time many of the unlettered membership are given the opportunity to stand on the shoulders of giants, and this year I'll be one of them!

My presentation is on the subject of "Meta-Intelligent Programming", and is a continuation of my "Organic Programming" concept that I kicked off at the ACCU conference three years ago. I'll start writing more about Organic programming itself in my dedicated blog (http://organic-programming.blogspot.com/) - but suffice to say that it is not about programming, per se, but rather about how we use our minds (and bodies) more effectively in a software development environment. Think "NLP for software development" and you won't be far out. It mixes in a lot of Tony Buzan stuff too.

What's this blog about?

I'll be writing from my thoughts about technology in general or in specifics.
At least, that's the plan!