Method chaining: Difference between revisions

Content deleted Content added
Reverted 1 edit by 103.83.21.100 (talk) to last revision by Anthrópinos81638
 
(9 intermediate revisions by 9 users not shown)
Line 1:
{{Short description|Programming syntax}}
{{refimprove|date=May 2008}}
 
'''Method chaining''', also known as '''named parameter idiom''', is a common [[Syntax (programming languages)|syntax]] for invoking multiple method calls in [[Object-oriented programming|object-oriented programming languages]]. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.<ref>{{cite web
|accessdate = 2011-04-13
|___location = http://firstclassthoughts.co.uk/
|publisher = First Class Thoughts
|title = Applying Method Chaining
Line 26:
| publisher = [[Prentice Hall]]
| year = 2008
| isbn = 978-0-13-235088-24
}}</ref>
 
Line 37:
 
Compare:
<syntaxhighlight lang="cpp">
a << b << c;
</syntaxhighlight>
equivalent to:
<syntaxhighlight lang="cpp">
a << b;
a << c;
Line 47:
 
Another example in [[JavaScript]] uses the built-in methods of Array:
<syntaxhighlight lang="javascript">
const interesting_products = products
somethings
.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 62 ⟶ 64:
 
==References==
<ref>[https://programmingdive.com/method-chaining-in-php/ Method Chaining in PHP]</ref>
{{Reflist}}
 
==External links==
* [http://www.infoq.com/articles/internal-dsls-java Creating DSLs in Java using method chaining concept]
<ref>* [https://programmingdive.com/method-chaining-in-php/ Method Chaining in PHP]</ref>
 
{{Design patterns}}
 
{{DEFAULTSORT:Method Chaining}}