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
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 =)