The Artima Developer Community
Sponsored Link

Java Buzz Forum
Leo's Blogging Again!

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.
Leo's Blogging Again! Posted: Aug 26, 2005 11:32 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Brian McCallister.
Original Post: Leo's Blogging Again!
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

I'm glad Leo has returned from his "escape geekery" sabbatical! In particular I'd like to point out a post on changing how we write jarvar =)

Leo gives us some code:

iterate(
      over( pairs(coll) ) )
    .skipIf(  keyIsInstanceOf(UnprocessableObject.class)  )
    .returning(  arrayOf(Pair.class)  )
    .exec(
      new Object(){Object process(Pair pair){
        print(  "%s -> %s", pair.getKey(), pair.getValue()  );
      }}  );  

What caught my eye is that it looks like a bunch of stuff I've been doing lately when building API's:

handle.inTransaction(new TransactionCallback() {
    public void inTransaction(Handle handle) throws Exception {
        handle.prepareBatch("insert into something (id, name) values (:id, :name)")
            .add(new Something(1, "one"))
            .add(new Something(2, "two"))
            .add(new Something(3, "three"))
            .add(Args.with("id", new Long(4)).and("name", "four"))
            .add(Args.with("id", new Long(5)).and("name", "five"))
            .execute();
    }
});
assertEquals(new Integer("5"),
             handle.first("select count(*) num from something")
                   .get("num"));

or

AsyncHelper.tryUntilNotInterrupted(new AsyncHelper.Helper() {
    public void cycle() throws InterruptedException {
        pendingWriteFrames.put(new FrameBuilder(Stomp.Responses.ERROR)
                        .addHeader(Stomp.Headers.Error.MESSAGE, message)
                        .toFrame());
    }});
}

which a popular couple libraries with reasonably well respected client api tends to do

return (Thing) getHibernateTemplate().execute(new HibernateCallback() {
    public Object doInHibernate(Session session) throws HibernateException {
        return session.createQuery("select s from Thing s where " +
                                   "s.wombat = :wombat and " +
                                   "s.mouse = :mouse")
                .setString("wombat", wombat)
                .setString("mouse", mouse)
                .uniqueResult();
    }
});

All of these examples use a common idiom from Ruby, Objective-C, and (I am told) Smalltalk -- message chaining. It leads to some really natural feeling and expressive code (which can look awfully funny at first, but you get used to it really fast, and then get frustrated when you cannot use it.

Next time you write a method with no return (a void method) consider having it return this instead. It breaks JavaBeans, sadly, which explicitely say a setter returns void, which limits its usability as the JavaBean naming convention is one of Java's serious strong points. It works nicely for chaining action style things though!

I blame Ruby. Leo blames Python. Gavin has blamed Smalltalk (he doesn't look old enough to be a Smalltalker, who'd have thunk). I don't know who gets blamed for the callbacks and templates in Spring, or to what they attribute the practice =)

Read: Leo's Blogging Again!

Topic: Terrorists Behind Bangladesh Blast Ridicules Democracy, Calls for Islamic Rule, States Bush is... Previous Topic   Next Topic Topic: Joel doesn’t “get” XP

Sponsored Links



Google
  Web Artima.com   

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