Method chaining

Share on Facebook Share on Twitter Email
Top

Method chaining is a common technique for invoking multiple method calls in object-oriented programming languages. Each method returns an object (possibly the current object itself), allowing the calls to be chained together in a single statement.[1] A method chain is also known as a train wreck due to an increasing amount of methods stacked after another in one line.[2]

Method chaining is not required. It only potentially improves readability and reduces the amount of source code. It is the core concept behind building a fluent interface.

Contents

Example

The following is an example in Java of how method chaining might be implemented and used:

class Person {
        private String name;
        private int age;
 
        // In addition to having the side-effect of setting the attributes in question,
        // the setters return "this" (the current Person object) to allow for further chained method calls.
 
        public Person setName(String name) {
                this.name = name;
                return this;
        }
 
        public Person setAge(int age) {
                this.age = age;
                return this;
        }
 
        public void introduce() {
                System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
        }
 
        // Usage:
        public static void main(String[] args) {
                Person person = new Person();
                // Output: Hello, my name is Peter and I am 21 years old.
                person.setName("Peter").setAge(21).introduce();
        }
}

By contrast, here is a non-chained equivalent:

class Person {
        private String name;
        private int age;
 
        // Per normal Java style, the setters return void.
 
        public void setName(String name) {
                this.name = name;
        }
 
        public void setAge(int age) {
                this.age = age;
        }
 
        public void introduce() {
                System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
        }
 
        // Usage:
        public static void main(String[] args) {
                Person person = new Person();
                // Not using chaining; longer than the chained version above.
                // Output: Hello, my name is Peter and I am 21 years old.
                person.setName("Peter");
                person.setAge(21);
                person.introduce();
        }
}

See also

References

  1. ^ "Applying Method Chaining". http://firstclassthoughts.co.uk/: First Class Thoughts. http://firstclassthoughts.co.uk/java/method_chaining.html. 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 persuit to minimize the number of keywords in the language, which lead to the discovery that void is an unnecesarry keyword!." 
  2. ^ Martin, Robert Cecil (2008). Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall. ISBN 0-13-235088-2. 

External links


Post a question - any question - to the WikiAnswers community:

Copyrights: