HOME

TheInfoList



OR:

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in
object-oriented programming languages Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
. 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 declarations are syntactic sugar. 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 s ...
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 " is often referred to simply as "chaining". Both chaining and cascading come from the
Smalltalk Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by ...
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 In computer programming, a return code or an error code is a numeric or alphanumeric code that is used to determine the nature of an error and why it occurred. They are also commonly found in consumer electronics and devices when they attempt to ...
.


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 that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
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

* Fluent interface *
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 processes chained together by their standard streams, so that the output text of each process ('' s ...
* Nesting (computing) * Builder pattern * Pyramid of doom (programming)


References


External links


Creating DSLs in Java using method chaining concept

Method 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