Content deleted Content added
(31 intermediate revisions by 26 users not shown) | |||
Line 1:
{{Short description|Programming syntax}}
{{refimprove|date=May 2008}}
'''Method chaining'''
|
|publisher = First Class Thoughts
|title = Applying Method Chaining
|quote = In order to simplify repeated object interactions on the same object the old trick ''Method Chaining'' originating the world of Smalltalk should be enforced. The idea is to let methods return<code> this </code>rather than<code> void</code>, thus affecting especially<code> set() </code>and<code> add() </code>methods. Method chaining arose during the designers of Smalltalk pursuit to minimize the number of keywords in the language, which lead to the discovery that<code> void </code>is an unnecessary keyword!.
|url = http://firstclassthoughts.co.uk/java/method_chaining.html
|url-status = dead
|archiveurl = https://web.archive.org/web/20110222112016/http://firstclassthoughts.co.uk/java/method_chaining.html
|archivedate = 2011-02-22
}}</ref>
== Rationale ==
[[Local variable]] declarations are [[syntactic sugar]].<ref>{{cite web|url=https://www.cs.umd.edu/class/spring2013/cmsc631/lectures/lambda.pdf|title=CMSC 631 – Program Analysis and Understanding|quote=• Syntactic sugar for local declarations - let x = e1 in e2 is short for (λx.e2) e1}}</ref>
Method chaining eliminates an extra variable for each intermediate step. The developer is saved from the cognitive burden of naming the variable and keeping the variable in mind.
Method chaining has been referred to as producing a "train wreck" due to the increase in the number of methods that come one after another in the same line that occurs as more methods are chained together.<ref>{{cite book
| last = Martin
| first = Robert Cecil
Line 14 ⟶ 26:
| publisher = [[Prentice Hall]]
| year = 2008
| isbn = 978-0-13-235088-
}}</ref>
A similar syntax is [[method cascading]], where after the method call the expression evaluates to the current object, not the [[return value]] of the method. Cascading can be implemented using method chaining by having the method return the [[this (computer programming)|current object itself]]. Cascading is a key technique in [[fluent interface]]s, and since chaining is widely implemented in object-oriented languages while cascading isn't, this form of "cascading-by-chaining by returning
While chaining is syntax, it has semantic consequences, namely that requires methods to return an object, and if implementing cascading via chaining, this must be the current object. This prevents the return value from being used for some other purpose, such as returning an [[error value]].
==
A common example is [[iostream]] in [[C++]], where for example <code><<</code> returns the left object, allowing chaining.
Compare:
<syntaxhighlight lang="cpp">
a << b << c;
</syntaxhighlight>
equivalent to:
<syntaxhighlight lang="cpp">
a << b;
a << c;
</syntaxhighlight>
Another example in [[JavaScript]] uses the built-in methods of Array:
<syntaxhighlight lang="javascript">
const interesting_products = products
.filter(x => x.count > 10)
.sort((a, b) => a.count - b.count)
.map(x => x.name)
</syntaxhighlight>
Note that in JavaScript <code>filter</code> and <code>map</code> return a new shallow copy of the preceding array but <code>sort</code> operates in place. To get a similar behavior, <code>toSorted</code> may be used. But in this particular case, <code>sort</code> operates on the new array returned from <code>filter</code> and therefore does not change the original array.
==See also==
Line 329 ⟶ 61:
* [[Nesting (computing)]]
* [[Builder pattern]]
* [[Pyramid of doom (programming)]]
==References==
Line 334 ⟶ 67:
==External links==
* [http://www.infoq.com/articles/internal-dsls-java Creating DSLs in Java using method chaining concept]
* [https://programmingdive.com/method-chaining-in-php/ Method Chaining in PHP]
{{Design patterns}}
{{DEFAULTSORT:Method Chaining}}
|