Method chaining is a common
syntax
In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituenc ...
for invoking multiple method calls in
object-oriented programming languages
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impleme ...
. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.
Rationale
Local variable
In computer science, a local variable is a variable that is given ''local scope''. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope. In programming languages with ...
declarations are
syntactic sugar
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
.
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.
A similar syntax is
method cascading, where after the method call the expression evaluates to the current object, not the
return value
In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address. The return address is sav ...
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 interface
In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric ...
s, and since chaining is widely implemented in object-oriented languages while cascading isn't, this form of "cascading-by-chaining by returning " is often referred to simply as "chaining". Both chaining and cascading come from the
Smalltalk
Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learni ...
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
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
uses the built-in methods of Array:
const interesting_products = products
.filter(x => x.count > 10)
.sort((a, b) => a.count - b.count)
.map(x => x.name)
Note that in JavaScript
filter
and
map
return a new shallow copy of the preceding array but
sort
operates in place. To get a similar behavior,
toSorted
may be used. But in this particular case,
sort
operates on the new array returned from
filter
and therefore does not change the original array.
See also
*
Fluent interface
In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric ...
*
Pipeline (Unix)
In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of process (computing), processes chained together by their standard streams, so that the output text of ...
*
Nesting (computing)
In computing science and informatics, nestinghttps://study.com/academy/lesson/nesting-loops-stan C Programming is where information is organized in layers, or where objects contain other similar objects. It almost always refers to self-similar ...
*
Builder pattern
The builder pattern is a design pattern that provides a flexible solution to various object creation problems in object-oriented programming. The builder pattern separates the construction of a complex object from its representation. It is one of ...
*
Pyramid of doom (programming)
References
External links
Creating DSLs in Java using method chaining conceptMethod Chaining in PHP
{{DEFAULTSORT:Method Chaining
Method (computer programming)
Articles with example C++ code
Articles with example Ruby code
Articles with example Java code
Articles with example PHP code