Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I frequently use a pattern where I using method chaining to setup an object, similar to a Builder or Prototype pattern, but not creating new objects with each method call, instead modifying the original object.

Example:

new Menu().withItem("Eggs").withItem("Hash Browns").withStyle("Diner");

Just wondering if there is a name for this pattern and whether it is considered an anti-pattern, because although it can read more fluently, it can lead to long method chains.

share|improve this question
2  
A fluent interface is a matter of style. If you are writing an API provide alternative forms to it. –  Oded Mar 2 '12 at 14:55
    
For more discussion, take a look at this question: programmers.stackexchange.com/questions/69519/… –  Eric King Mar 2 '12 at 15:01
    
@Oded It is possible to use such an API without chaining by just making each call a separate statement, or did you have another idea for an alternative? –  Garrett Hall Mar 2 '12 at 15:04
    
@GarrettHall - Of course it is possible, but then you end up calling things like menu.withStyle("") without context. You need two APIs in such a case. –  Oded Mar 2 '12 at 15:08
2  
@GarrettHall The point of the 'fluent interface' is the method chain, which is meant to be read like a sentence. In this sense, the long method chain is not considered bad. But, and here I agree with Oded, it would be best to also provide the same functionality in a more conventional syntax. That way, the developer can choose which method to use. –  Eric King Mar 2 '12 at 15:11

3 Answers 3

up vote 23 down vote accepted

Fluent Interface

I've always heard of this method being called a 'fluent interface', as coined by Eric Evans (of Domain Driven Design fame) and Martin Fowler (of Agile Manifesto fame).

The main drawbacks are the readability (which some folks love and some hate), and the fact that it can be harder to debug in some cases because the entire chain of actions may be considered a single statement when stepping through it.

I certainly don't consider it an anti-pattern, although I've only used the technique a few times myself.

share|improve this answer
4  
Note that the proponents are pretty insistent that a fluent interface involves much more than just method chaining, although it is often unclear what else is meant by that term. –  Kilian Foth Mar 2 '12 at 15:12
    
Is it common for the entire chain to be considered a single statement? That sounds like a poorly designed debugger issue to me. –  blueberryfields Mar 2 '12 at 16:06
    
I've not come across a debugger that treats chains as a single statement, but it can still be a pain if you're only interested in one of the methods towards the end of the chain, in one particular chain. –  Baqueta Mar 2 '12 at 16:29
    
I work in C#, and the .NET debugger lets me step into the individual methods just fine. Other languages, I'm not so sure. –  Eric King Mar 2 '12 at 17:02
    
@KilianFoth As noted in the question's comments, the chained methods in a fluent interface should be readable almost as a sentence. Sometimes this means that the individual methods should have different names than is otherwise considered good practice –  Izkata Mar 2 '12 at 17:06

Method chaining like that is usually called a Fluent Interface when there is some kind of flow or discoverability in the chain. Alternatively, you can think of an api like jQuery that relies heavily on method chaining as not 'fluent' because there's not the same emphasis on discoverability -- it's more for convenience.

For your example (using withx, withy) you can consider this a variant of the Builder pattern because you've got a specialized class that, given some state (method calls) knows how to return a properly configured object.

This isn't an anti-pattern if used appropriately.

share|improve this answer

whether it is considered an anti-pattern,

definitely not an anti-pattern. jQuery is probably the most well used implementation of this.

because although it can read more fluently, it can lead to long method chains

Yes it can, but what is the alternative ? You can end up with almost a plain english sentence, with the api guiding you to what is available and appropriate.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.