Questions tagged [method-chaining]
37 questions
49
votes
5 answers
Purpose of `return self` from a class method?
I came across something like this in an open-source project. Methods that modify instance attributes return a reference to the instance. What is the purpose of this construct?
class Foo(object):
def __init__(self):
self.myattr = 0
def…

nate c
- 787
- 1
- 5
- 7
38
votes
8 answers
When using method chaining, do I reuse the object or create one?
When using method chaining like:
var car = new Car().OfBrand(Brand.Ford).OfModel(12345).PaintedIn(Color.Silver).Create();
there may be two approaches:
Reuse the same object, like this:
public Car PaintedIn(Color color)
{
this.Color = color;
…

Arseni Mourzenko
- 134,780
- 31
- 343
- 513
29
votes
3 answers
What is the name for the idiom using method chaining to build an object?
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…

Garrett Hall
- 2,182
- 1
- 15
- 16
27
votes
7 answers
Coding style issue: Should we have functions which take a parameter, modify it, and then RETURN that parameter?
I'm having a bit of a debate with my friend over whether these two practices are merely two sides of the same coin, or whether one is genuinely better.
We have a function which takes a parameter, fills out a member of it, and then returns it:
Item…

Squimmy
- 379
- 1
- 3
- 3
22
votes
11 answers
In a fluent interface with "with", is cloning expected?
In an object oriented language like Java or PHP (other perspectives welcome as well) if I use a fluent interface like this:
my_pizza = (new Pizza).withTopping("pineapple");
another_pizza = my_pizza.withGarlic(true);
would one expect withGarlic() to…

AndreKR
- 577
- 3
- 11
15
votes
7 answers
Function only returns unchanged parameter, useless?
I just found this function in the project I'm working at:
-- Just returns the text unchanged.
-- Note: may be nil, function must return nil in that case!
function Widget:wtr(text)
return text
end
Too sad, the coder does not…

jawo
- 270
- 2
- 11
15
votes
1 answer
Are chained methods that require only one parameter per method equivalent to currying?
I've been toying around with Ruby lately and I found myself wondering if in pure object oriented languages (and even those that are not pure) making methods that take only one parameter and then get chained together is equivalent to currying in…
user28988
14
votes
4 answers
Are there any actual drawbacks to self-referential method chaining?
I recently suggested a method of chaining be implemented for a certain class in a certain project so readability of the code could be improved. I got a "fluent interfaces should not be implemented just for convenience, but for semantics" answer and…

dukeofgaming
- 13,943
- 6
- 50
- 77
11
votes
4 answers
When should a method of a class return the same instance after modifying itself?
I have a class that has three methods A(), B() and C(). Those methods modify the own instance.
While the methods have to return an instance when the instance is a separate copy (just as Clone()), I got a free choice to return void or the same…

Martin Braun
- 283
- 1
- 3
- 13
8
votes
8 answers
Can "return this" pattern be optimized to no cost performance?
return this (or similar construct) allows method chaining. Lack of it is painful, because you have to write such code (C#):
var list = new List();
list.Add("hello");
list.Add("world");
instead of
list.Add("hello").Add("world");
Elixir…

greenoldman
- 1,506
- 1
- 14
- 27
7
votes
4 answers
Is the fluent interface pattern suitable in casual scenarios?
In a recent PR, a developer, whom I will call Alice, came across a lot of resistance by a coworker (Bob) because she wrote a utility code unit in a fluent style rather than in a classical style.
In short, Alice had a piece of business logic (BL)…

Daniele Repici
- 181
- 3
7
votes
1 answer
Is there a better term for "functional method chaining"?
I'm writing a C# style guide for my team and I'm trying to describe the benefits of side-effect-free functional-style methods. I want to include online references to back up the suggestions, but I can't seem to Google the kind of functional method…

Max
- 186
- 1
- 7
7
votes
6 answers
Coding style for chained function calls
A common thing you need to do is to take a value, do something with it by passing it to a function, and then do some more with the return value, in a chain. Whenever I run into this type of scenario, I get unsure about how best to write the code.
As…

last-child
- 189
- 1
- 6
5
votes
1 answer
nodejs chaining with async
I'm trying to chain a series of methods that are async. I have heard of promises and futures but what I'm looking for is:
obj.setup()
.do_something()
.do_another_thing()
.end()
and not:
obj.setup()
.then(...)
.then(....)
I have…

Daithí
- 151
- 1
- 5
4
votes
1 answer
Chain of responsibility for a single instance?
I have legacy code and it has a function called initialize and this function calls N amount of methods of same object and these methods are responsible for validating identity of personas.
At each method call, the function checks whether the said…

jeffbRTC
- 167
- 5