Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: A style question re "chaining" methods

Thread Tools

Re: A style question re "chaining" methods

Matt Humphrey
Posts: n/a

"Duane Morin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Every now and then I trip over a situation where I'm initializing alot
> of properties in a series of beans. Assuming the normal behavior of
> having setter methods return void I end up with something like this:
> Foo x = new Foo();
> x.setBar();
> x.setBaz();
> x.setQuux();
> and so on. But then I think, what if setters returned "this"? Then I
> could do this:
> x.setBar().setBaz().setQuux();
> I kinda like it. Feels very "functional" to me. Something about
> returning voids just feels like a big brick wall, like "Ok, stop flow
> of thought, begin new one." Not to mention it better mimics the way
> get() works, since nobody in their right mind does this:
> Bar b = getFoo().getBar();
> Baz c = b.getBaz();
> Quux q = c.getQuux();
> Instead they just getFoo().getBar().getBaz().
> When I've brought this up with my teammates in the past I was soundly
> thrashed.
> But I think that was more of a stylistic preference. is there a good
> solid reason why this style of doing setters would be a bad thing?

This kind of chaining is used in SmallTalk and uses the design style of
returning "this" However, I find that long chains are difficult to read and
somewhat misleading because normal chains expect to return a meaningful
object to which the next method applies, as in
Result r = MyObject.getXthPart().sortBy ("q").differentiate ();

The notion of returning "this" is redundant because the caller already knows
the object. The only reason for doing it is to make these chains possible.
In addition, it can be confused with cases where a real, meaningful object
is being returned. Also, the style is inconsistent because it cannot be used
where "this" cannot be returned. I'm not even sure it would be more
efficient, as the compiler should be able to figure out what you're doing

Although the style is used in Smalltalk, I don't think it occurs frequently
enough to outweigh the awkwardness of the design it requires.

Matt Humphrey Removed)

Reply With Quote
Jon A. Cruz
Posts: n/a
Matt Humphrey wrote:


Yes, it seems very bad in Java. Including all those things you just
pointed out.

> I'm not even sure it would be more
> efficient, as the compiler should be able to figure out what you're doing
> anyway.

Actually, I looked into this a little not too long ago.

You save a byte per call in the class file. But then you pay by having a
few extra bytes in each method to return a value, and at runtime pay the
overhead for the return.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Design question - methods calling methods Rhino Java 49 05-29-2010 03:16 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Top-Up Methods - Finarea (Voipcheap, internetcalls, etc.) et. al., VOIP Services Question - Top-up Methods News Reader UK VOIP 0 04-10-2006 02:41 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Re: A style question re "chaining" methods Jon A. Cruz Java 11 07-15-2003 03:13 PM