Re: A style question re "chaining" methods

Discussion in 'Java' started by Matt Humphrey, Jul 9, 2003.

  1. "Duane Morin" <> wrote in message
    > 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
    Matt Humphrey, Jul 9, 2003
    1. Advertisements

  2. Jon A. Cruz

    Jon A. Cruz Guest

    Matt Humphrey wrote:

    [BIG SNIP]

    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.
    Jon A. Cruz, Jul 10, 2003
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Bloggs
    Brock Allen
    May 11, 2005
  2. Jon A. Cruz
    Dale King
    Jul 15, 2003
  3. Dmytro Sheyko
    Dmytro Sheyko
    Jan 19, 2006
  4. Ken Varn
    Ken Varn
    Apr 26, 2004
  5. Kenneth McDonald
    Kenneth McDonald
    Sep 26, 2008

Share This Page