21

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.

  • 3
    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
  • 1
    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
37

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.

  • 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. – vaughandroid Mar 2 '12 at 16:29
  • 1
    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
  • 1
    @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
5

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.

  • What is discoverabilty? The ability to find the methods you need through the API's autocomplete? – Josiah Yoder Feb 2 '18 at 18:58
  • I am not familiar with jQuery. How are their method chains only for convenience, not discoverability? Because there are so many ways to chain it is overwhelming even with autocomplete? – Josiah Yoder Feb 2 '18 at 19:00
3

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.

  • ..and you get what you want in 1 line, instead of 8 - 10. Which is good if you are only writing a one liner in a terminal. Easier to recall 1 command than 10. Because it is fluent and the methods have descriptive names it is also easier to read. Sadly a lot of this in Python2 can no longer be done in Python3/Python4. – mckenzm Oct 8 '18 at 21:57
-3

It is called "train wreck" pattern.

(It is an anti-pattern and is against Clean Code)

The "train wreck" pattern violates the Law of Demeter.

  • 3
    This is not the same as the method chaining in the the article you link to, where the example is a.getB().getC().doSomething(). That example is bad because "Your method can call methods on its own fields directly (but not on the fields' fields) ". Here, there is only 1 object, which is being created, and "you can play with toys you've made yourself". – Nic Oct 14 '18 at 5:28

protected by gnat Jun 24 at 8:19

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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