Tuesday, December 29, 2009

Syntax Hell

As anyone who has applied a more functional style to Java will realize, the Java syntax really gets in the way. I was using my usual guinea pig, JRDF, with "with" so you don't have to iterate but apply a function and so you don't have to close a ClosableIterator. The typical code to print out the graph is:
ClosableIterable<Triple> triples = graph.find(ANY_TRIPLE);
try {
   for (Triple triple : triples) {
     System.out.println("Graph: " + triple);
   }
 } finally {
   triples.iterator().close();
 }

Using "ClosableIterators.with" this becomes:
with(graph.find(ANY_TRIPLE), new Function<Void, ClosableIterable<Triple>>() {
   public Void apply(ClosableIterable<Triple> object) {
     for (Triple triple : object) {
       System.out.println("Graph: " + triple);
     }
     return null;
   }
});
It's typically one line less but that's not that much of an improvement.

1 comment:

Tony Morris said...

Though it's not the line count that is important; it's the compositional aspects of the code. You can pull the latter one apart and reason about the smaller parts independently.

Java gets in the way in terms of syntax and type system. The latter hindrance is much more catastrophic in terms of assessing Java for practical use, yielding a consistent conclusion.