Archive for the ‘programming’ Category

Good APIs

Monday, September 3rd, 2007

At least in the Java world[1], good APIs are very rare. The vast majority of the APIs I’ve used in Java are at best mediocre, and I’ve run into some real stinkers (No fingerpointing here, but a lot of them are even in the standard library!).

Part of this is the fault of the language. Java code… tends to be ugly. It’s not a language which lends itself to really flexible syntax, and so you have to work really hard to produce powerful abstractions which are actually nice to use.

The only APIs I’ve encountered so far which have really made me sit up and go “Wow, that’s well designed” are Google Guice and Joda time. They have well thought out class hierarchies, good object oriented style[2], sensible use of fluent interfaces / method chaining and just generally well thought out interfaces and names.

They’re not perfect by any means, but they show promise that it really is possible to write good APIs for Java.

Anyone else know of other similarly well designed libraries?

[1] And, unfortunately, I lack much in the way of non-trivially large development in other languages. The Parsec API is nice, ‘though it gives me a headache sometimes. Haskell libraries in general look rather pretty, though I suspect that’s more a function of the language than the API design. I’m not really experienced enough in it to judge good API design.

[2] I’m not an OO fanatic. It has advantages and disadvantages, and sometimes you just want to use a different approach (I rather like FP for example). But one thing I’ve observed is that if you do it right, OO can have the effect of producing some astonishingly readable code. I don’t know either well/at all really, but this style of design seems much more common in Ruby, smalltalk, etc.

Looking through other peoples’ code is fun

Sunday, September 2nd, 2007

I’m playing with the google guice code at the moment. It’s very interesting, and contains some quite neat tricks (although is rather short on documentation so I’m getting very confused). But that’s not what this post is about. I just wanted to share the following fun snippet. :-)


// TODO(kevinb): gee, ya think we might want to remove this?
private static boolean allowNullsBadBadBad() {
return "I'm a bad hack".equals(
System.getProperty("guice.allow.nulls.bad.bad.bad"));
}

Birds of a Feather and AMQP

Friday, August 24th, 2007

Minor announcement in case anyone who lives in London actually reads this thing. :-)

Alexis mentioned to me at wednesday’s London HUG that he’s giving a Birds of a Feather talk on AMQP at No Fluff Just Stuff next week. It looks quite interesting.

If you don’t know, AMQP is a newish protocol for application level messaging. One of the big implementations out there (which is the one Alexis is involved with) is RabbitMQ, which is written in Erlang (which is how I got interested in the subject in the first place). By the sounds of it, they’ve been doing some quite fun and useful things with it. This should definitely be an interesting talk and I highly recommend coming if you’re in the area.

The world has gone mad

Wednesday, August 15th, 2007

Here is the proof:

vv.getRenderContext().setVertexLabelTransformer(MapTransformer.<Number,String>getInstance(
LazyMap.<Number,String>decorate(new HashMap<Number,String>(), new ToStringLabeller<Number>())));

Update: I’ve found a better example.

Data structures in Java

Friday, August 3rd, 2007

I’ve been playing with implementing some simple data structures in Java, for later use in the lazy strings project. The results are… a bit depressing, frankly. Clearly I need to learn to optimise better, as right now I’m creating specialised data structures for certain types which turn out to perform worse than the general purpose implementations in the standard library. :-)

In particular, I threw together a simple implementation of a Trie, and it performs significantly worse than just using a TreeMap. As the number of collisions increases the relative performance of the Trie improves somewhat, eventually beating the TreeMap, but still in most situations the performance is pretty poor.

It also doesn’t help that the Trie ends up bloating the keys significantly, as you end up having to create far too many objects per character. I can definitely cut back on the object creation, but even one object per character may prove to be too much to get an efficient implementation in Java.