Smalltalk cascades
Written on 11 Feb 2006
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);
Filed in: Smalltalk roots.

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.
I’ll second that: Ahem
Thanks guys.
I’ve got a little time coming up so, unless I get swamped with admin, maybe I can finally get something done.
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.
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©;
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.
Yes. The Java equivalent does the same, maybe because someone inherited an idea from C++ streams? It just saves so much noise in the code.
Yes. The Java equivalent does the same, maybe because someone inherited an idea from C++ streams? It just saves so much noise in the code.