Example & Tutorial understanding programming in easy ways.

What is meant by Method chaining?

Method Chaining, known by other name that is idiom. It is a common sysntax for invoking multiple calls in object-oriented programming language. Each method returns an object, that allowing calls to be chained together in a single statement. 

1. Chaining is syntactic sugar which eliminates the need for intermediate variables. A method chain is also known as 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.

2. Chaining line breaks are often added between methods.

                   When we use 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 implement using method chaining by having the method return the current object itself (this). 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 Small talk 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, and requires that the return value be mutable .

Read More →