The Artima Developer Community
Sponsored Link

Java Buzz Forum
The Same Code, Part 2 (or "why lambda pwns")

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brian McCallister

Posts: 1282
Nickname: frums
Registered: Sep, 2003

Brian McCallister is JustaProgrammer who thinks too much.
The Same Code, Part 2 (or "why lambda pwns") Posted: Nov 26, 2004 8:20 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Brian McCallister.
Original Post: The Same Code, Part 2 (or "why lambda pwns")
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
Latest Java Buzz Posts
Latest Java Buzz Posts by Brian McCallister
Latest Posts From Waste of Time

Advertisement

In my last entry I had several blocks of code doing the same thing with the same basic API (all derived from jDBI, or equivalents in similar languages (Scheme, Ruby)). It was for two reasons: the first is because I had been IM'ing people with "huh, look at this" with rafb pastes. That got annoying, the second is because the look of the code was interesting.

The look of the code is just watching the form of the source. The imperative code is very imperative looking; the (objectified) scheme-ish code is a slant; the functional style code (Java with callbacks, Ruby) with statement (as compared to expression) oriented languages was all sideways-V shaped (the scheme would be too except that the formatting tradition is different).

Darren Oakey added some C#:

using (Transaction.Called("Read stuff"))
    using (QueryDataSource data = dbi.open())
        foreach (DaraRow row in data.Query(CHANGED_DESCRIPTIIONS ))
            Console.Writeline( row.Name ); 

Which is quite nice, but misses the exception handling, which is important. (note: I didn't make the imperative Java as complicated and nasty looking as possible, I tried to capture the same behavior (see the ConnectionHandle#executeInternal implemenation for full exception boilerplate) as the more functional style.)

JDBC exception handling is bloody boilerplate. I suspect 50% of IDEA users have a live template for it, and the rest are slapping their foreheads right now. Libraries exist to get rid of boilerplate, but you cannot exactly say something like

beginExceptionHandlingBlock();
doJDBCStuff();
endExceptionHandlingBlock();

and have it magically work. So sad. The point is that you need to pass the innermost part of the exception handling boilerplate to the library function -- Java makes this possible, though verbose. Better C#, imho, would have been along the lines of:

dbi.Open(delegate(Handle handle) {
    handle.WithTransaction("Finding Changed Descriptions", delegate(Handle handle) {
        handle.Query(CHANGED_DESCRIPTIONS, delegate(Handle handle, Map row) {
            Console.Println(row.Get("name"));
        })
    })
});

That is pretty slick, and is why I (very much) regret that Java 1.5^H^H^H 5.0 copied the wrong things from C#.

The point is that a first class function, or in the Java case, an interface with one method (with varargs and interfaces in the language spec already, how hard would it have been to add lambda, er, I mean, delegates?), can absolutely be passed into the library, and that can be all wrapped and bundled as necesary to ensure 1) resources are closed properly, all the time, without extr 1000 a boilerplate, 2) exceptions are handled properly, all the time, without extra boilerplate.

Read: The Same Code, Part 2 (or "why lambda pwns")

Topic: Koders - a search engine for Code Previous Topic   Next Topic Topic: Continuations via JIT

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use