Forgetting try-finally

Posted by Anders Mon, 25 Aug 2008 19:06:00 GMT

or “Hot Potato Exception Handling”

This is a common use of try-catch:

public void foo() { 
    // Catch any exception so that the call to super is done anyway 
    try { 
        //... 
    } catch (Exception e) { 
        // Log
        // ...
    } 
    // Call super last 
    super.foo(); 
}

You could think that the purpose of the try-catch is to enable logging of the exception. But the first comment (taken from actual example code) suggests that the logging is just incidental. The purpose is to make sure that something is run no matter what. The logging is just a case of not knowing anything better to do with the exception once it’s caught.

Instead, why not use the simple try-finally:

public void foo() { 
    try { 
        //... 
    } finally {
        super.foo(); 
    }
} 

We’re not “handling” the exception, but that’s probably good. We are handling non-Exception throwables though. I think people just forget that you can have a try-finally without the catch.

Posted in ,  | 1 comment

Animal

Posted by Anders Thu, 07 Aug 2008 13:32:00 GMT

Animal

Posted in  | Tags  | no comments

"Street and Studio" @ Tate Modern

Posted by Anders Sun, 06 Jul 2008 13:21:00 GMT

Last week I saw the Street and Studio photo exhibition at Tate Modern in London. Lots of great photos from the 1800’s up to current time, including the 1900’s favorites I already knew, like Walker Evans, Diane Arbus and others. But it also served as an interesting introduction to later photographers like Martin Parr and Rineke Dijkstra, at least for someone like me who was stuck in the 20th century.

If you’re in London before 31 August 2008, go see it!

Posted in  | Tags  | no comments

toString() is Evil

Posted by Anders Tue, 24 Jun 2008 22:00:00 GMT

...or some other provocative title.

The good old toString() method, with us since Java 1.0, has at least two different meanings:

  • Displaying: How the object should appear to the user, in the GUI, on a web page, etc.
  • Inspection: How the object should appear in debug output, logs, debugger tools etc.

Both are in some way “a string representation of the object”. The default implementation in java.lang.Object suggests inspection, e.g. “java.lang.Object@c37f31”, but many APIs, like AWT/Swing, use it for displaying the object to the user.

Problems

  • It’s hard to tell which usage is intended when reading the code.
  • Debuggers will use toString(), which can cause confusing side-effects.
  • Since every object has a toString(), the IDE’s usage search becomes unusable.
  • It’s hard to tell if a toString() method is dead code or not.

In addition, a lot of code implements it when it has a more specific meaning. For instance, generating HTML is better done as toHTML() than as toString().

What Others Do

Ruby solves this differently than Java. There are two methods, inspect() and to_s(), where the default implementation of to_s() in Object uses inspect(). This separates the two intentions, but still has to_s() available on every object.

We can’t do much about java.lang.Object, but we still have options.

Suggestion

Use toString() only for logging and debug output.

If the method has a more specific meaning, communicate that instead, e.g. title() or name().

If the value to display has a specific format you can communicate that instead, e.g. toHTML() or asLeetSpeak().

If the value to display is nothing other than a string, still avoid toString(). Call it something like displayString(), or maybe even asString() to avoid problems.

Posted in ,  | 6 comments

BigDecimal performance notes

Posted by Anders Wed, 04 Jun 2008 17:47:00 GMT

Notes on java.math.BigDecimal’s performance (in Java 1.5):

Sorting

BigDecimal’s compareTo method relies on both of the BigDecimals being in the same internal form. Internally BigDecimal uses either a BigInteger or, when possible, a native integer to represent its value. To compare two BigDecimals they’re both normalized (“inflated”) to the BigInteger form. This means that simply sorting a list of BigDecimals can cause memory use to increase. Not what you’d expect.

Serialization

Serialization of BigDecimal is surprisingly slow. Not only do they inflate their internal representation, just like when comparing, but they also use a lot of CPU for some reason. When serializing large graphs of objects of a lot of different classes, the BigDecimals stood out like a sore thumb in the CPU profile. Dumping them as String representations instead was quicker and didn’t use as much memory.

Posted in ,  | Tags  | no comments

New job

Posted by Anders Sun, 25 May 2008 20:11:00 GMT

I’ll be starting a new job at Valtech this summer!

After three years at PriceRunner it felt like it was time to move on. I’ve done a lot of interesting projects and the people are great, but I need some new challenges. I’ll probably be doing a lot of the same things, but doing it in the role of a consultant will be new. I’m also hoping to do a bit more front-end web development, to balance all the back-end system work I’ve done lately.

But first: Summer vacation!

Tags  | no comments

Idly twittering away

Posted by Anders Wed, 30 Apr 2008 11:50:28 GMT

I’ve started to use Twitter. We’ll see how it works out.

1 comment

Canon Canonet 28

Posted by Anders Fri, 18 Apr 2008 17:54:00 GMT

For various reasons, among which was the need for a “lab rat” for camera repairs and the flawed awesomeness of the movie Pecker, I got myself a Canonet 28.

Canonet 28

It’s much like my fancier Canonet QL17 GIII, but without the manual control, the great lens and basically everything else that is good. The only thing the 28 still provides is a decent automatic mode, which also works with flash.

So far I’ve used it for practicing a small camera “repair”, i.e. cleaning the viewfinder. It took around 30 minutes to take it apart and clean it, excluding the time to find suitable tools for opening it (pliers and a rubber glove(!)). The same operation then took just 15 minutes on the QL17.

Canonet internals

Because of the automatic exposure the 28 is dependent on a working light meter. There is some small glitch in the circuits somewhere in it, so the meter doesn’t always work. Without fixing that it’s not reliable enough to use, but maybe I’ll be able to solve that too one of these days.

Posted in  | Tags , ,  | no comments

Lumix LX2

Posted by Anders Sun, 06 Apr 2008 12:57:00 GMT

Efter ett par år med bara en stor digitalkamera så kompletterade jag med en liten digitalkamera. Det blev en Panasonic Lumix LX2: Liten, skarp optik, bred vidvinkel, bildstabilisering, manuella kontroller, ... Det mesta man behöver. Den är så fånigt dyr att man får en Nikon D40 för samma pris, men i Bangkok gick den att köpa till ett rimligt pris.

Illustration: LX2

När jag väl upptäckt att jag kommer åt nästan alla funktioner via den lilla joysticken (tryck ner den en sekund) så blev den lätthanterad. Bilderna ser också bra ut, helt ok även uppåt ISO 400, även om de inte är i närheten av systemkamera-nivå. Under ideala omständigheter ger den fantastiskt bra bildkvalitet. Kombinerat med att den ryms i en jackficka så lär den här kameran följa med mig rätt mycket framöver.

Marievik

Blues

Det enda jag egentligen saknar är en optisk sökare. Det är lättare att fota med en kamera framför ansiktet, än att hålla den på utsträckta armar som en zombie.

Posted in  | Tags , ,  | no comments

Post-punk

Posted by Anders Sat, 05 Apr 2008 16:12:00 GMT

Today Last.fm Player almost exclusively plays me songs from bands tagged as “post-punk”. I had no idea I was into post-punk, but the statistics don’t lie.

Tags ,  | no comments

Older posts: 1 2 3 ... 6