February 11, 2006

Smalltalk cascades

Jay Fields writes about using chaining for object initialisation.

One of the inspirations for the fluent interface style in jMock was a Smalltalk construct called cascade, which sends multiple message to the same object.

aThing one: 'one';
  two: 'two';
  other.

I just got fed up having to redeclare the same object

thing.setOne("one");
thing.setTwo("two);
Posted by stevef at February 11, 2006 11:29 AM
Comments

I like this. I've been trying to use a similar style in writing functional tests recently, to make them more readable (more like English sentences).

In the Martin Fowler article you link to, he says "I saw Steve Freeman and Nat Price give an excellent talk at JAOO2005 on the evolution of the JMock API, they promised to write up their experiences but haven't. Someone please kidnap their compilers until they do." *Ahem*.

Posted by: Robert Chatley at February 11, 2006 12:57 PM

I'll second that: *Ahem*

Posted by: Bill Caputo at February 12, 2006 2:43 AM

Thanks guys.

I've got a little time coming up so, unless I get swamped with admin, maybe I can finally get something done.

Posted by: Steve Freeman at February 12, 2006 4:31 PM

OK. We've written up a paper and submitted it to OOPLSA for this year. We'll post as much as we can as soon as they let us know.

Posted by: Steve Freeman at March 26, 2006 1:29 AM

The StringBuilder-class in .NET has some methods that return the instance itself, mimicking cascading:

StringBuilder sb = new StringBuilder();
sb
.Append(a)
.Append(b)
.Append(c);

Off course, this is pretty artificial, and only works on classes that are designed that way and when your methods don't return anything else.

Posted by: Tommy Carlier at April 10, 2006 5:50 PM