To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
Languages
Recent
Show all languages
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Method chaining

From Wikipedia, the free encyclopedia

Method chaining is a common syntax for invoking multiple method calls in 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.[1]

YouTube Encyclopedic

  • 1/3
    Views:
    44 456
    28 576
    9 686
  • jQuery method chaining
  • PHP Object Oriented Programming (OOP): Method Chaining (11/13)
  • Advance PHP in Hindi/Urdu (Method Chaining)

Transcription

Rationale

Local variable declarations are syntactic sugar.[2]

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.[3]

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 current object itself. Cascading is a key technique in fluent interfaces, and since chaining is widely implemented in object-oriented languages while cascading isn't, this form of "cascading-by-chaining by returning this" is often referred to simply as "chaining". Both chaining and cascading come from the Smalltalk language.

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.

Examples

A common example is iostream in C++, where for example << returns the left object, allowing chaining.

Compare:

a << b << c;

equivalent to:

a << b;
a << c;

Another example in JavaScript uses the built-in methods of Array:

somethings
  .filter(x => x.count > 10)
  .sort((a, b) => a.count - b.count)
  .map(x => x.name)

See also

References

  1. ^ "Applying Method Chaining". First Class Thoughts. Archived from the original on 2011-02-22. Retrieved 2011-04-13. 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 this rather than void, thus affecting especially set() and add() 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 void is an unnecessary keyword!.
  2. ^ "CMSC 631 – Program Analysis and Understanding" (PDF). • Syntactic sugar for local declarations - let x = e1 in e2 is short for (λx.e2) e1
  3. ^ Martin, Robert Cecil (2008). Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall. ISBN 0-13-235088-2.

External links

This page was last edited on 31 December 2023, at 14:32
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.