XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Method chaining -benefits?

Last post 9/28/2009 5:41 PM by Cygon4. 5 replies.
  • 9/27/2009 11:03 PM

    Method chaining -benefits?

    At work I've been porting some Java code over to ASP.NET, and it makes extensive use of method chaining. For example:

    SelectBoxBuilder.CreateSelectBox().setClass("xxx").setOption("something").setOtherBehavior("different").setSomethingDifferentAgain("xxx); 

    What's your concensus on this as a sensible technique? I'm familiar with the justification behind it (e.g. Martin Fowler et al), but personally I found it cumbersome and tedious for the following reasons:

    • It makes using properties in C# more difficult.
    • Every setter and getter has to return "this".
    • It just seems to trade source complexity in one area for another.

    /me waits to get burned
  • 9/27/2009 11:07 PM In reply to

    Re: Method chaining -benefits?

    It's all preference, but I'd probably have that written like this using an object initializer list and properties:

     

    SelectBox box = new SelectBox { Class = "xxx", Option = "something", OtherBehavior = "different", SomethingDifferentAgain = "xxx" }; 

    It just is cleaner and makes more sense to me. I'm not a big fan of method chaining for object creation like this because it doesn't make much logical sense to me. Why would a method that is supposed to set a value also returning the object I'm calling it on? That seems very odd and broken to me.

     

  • 9/27/2009 11:14 PM In reply to

    Re: Method chaining -benefits?

    I use it when it makes sense to, i.e.
    GetNewTile(x,y).SetFoo(bar); 
    but anything more involved seems like it would be cumbersome and confusing.
    "Software is never finished, it is in varying states of 'less broken'" because "If it ain't broke, it doesn't have enough features yet"

    In Playtest: Avatar Land | The MANLY Game for MANLY Men

    The signature that was too big for the 512 char limit
  • 9/27/2009 11:36 PM In reply to

    Re: Method chaining -benefits?

    In general it is bug prone. If any of the methods returns a null reference, for w/e reason, the next call in the chain will throw a NullReferenceException. For object initialization, the method Nick presented is far more preferable to me.
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 9/28/2009 12:56 AM In reply to

    Re: Method chaining -benefits?

    If you don't like it, don't use it. Let the people who like it, use it.

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 9/28/2009 5:41 PM In reply to

    Re: Method chaining -benefits?

    This technique (it's name is fluent interfaces) can save some space and provide more clarity if applied carefully. Carefully as in, not suddenly invoking a method that returns another object, so the last two lines would apply to an IBar whereas the first few lines configure an IFoo object ;)

    But it can turn a difficult configuration into a breeze. For example, in my unit tests NMock allows me to write:

    Expect.Once.On(mockedObject).Method("BeginSomething").WithAnyParameters(); 
    Expect.Once.On(mockedObject).Method("EndSomething").WithAnyParameters().Throw( 
      new MockException("Intentional failure for unit testing"
    ); 
     
    Foo(mockedObject); 
     
    mockery.VerifyAllExpectationsHaveBeenMet(); 

    Imagine what a mess that would make if I wanted to tell NMock through some traditional interfaces and methods how to behave and what to look for.

    Check out my website and blog for some interesting articles and useful utility classes!
    Nuclex Framework: threaded particles, skinnable GUI, vector fonts, texture atlasses and lots more.
    WiX XNA Installer: Professional-looking MSI installer template for XNA games.
Page 1 of 1 (6 items) Previous Next