Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Method Chaining


unknown
 Share

Recommended Posts

This is a simple but useful programming technique that I use a lot so I hope you enjoy it!

Method chaining can be applied to any language that supports object orientation. According to [Wikipedia](http://en.wikipedia.org/wiki/Method_chaining) method chaining is a technique for invoking multiple method calls. It can make code more readable and help provide a fluent interface. Method chaining is sometimes overused, and can make debugging difficult hence the term 'train wreck'. I find method chaining most useful when setting properties of an object.

Here's a simple Java example of what method chaining looks like and how to implement it

>! **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[[/color]] args) {
>! Person person = **new** Person();
>! _// Output: Hello, my name is Peter and I am 21 years old._
>! person.setName("Peter").setAge(21).introduce();
>! }
>! }
>! From the above example, this is method chaining.
>! ```
person.setName("Peter").setAge(21).introduce();
```
>! This is not method chaining
>! ```
>! person.setName("Peter");
>! person.setAge(21);
>! person.introduce();
>! ```
Link to comment
Share on other sites

Yeah, this looks like it could get you into a lot of trouble fairly quickly if your functions rely on arguments being returned from other functions. Otherwise, good to know if you want to save editor space, though.

FYI: This is not supported in VB.NET 2010 or VC# 2010.
Link to comment
Share on other sites

> Yeah, this looks like it could get you into a lot of trouble fairly quickly if your functions rely on arguments being returned from other functions. Otherwise, good to know if you want to save editor space, though.
>
> FYI: This is not supported in VB.NET 2010 or VC# 2010.

Method chaining works in any language that supports object orientation. [Here's an example in C#](http://stackoverflow.com/questions/293353/fluent-interfaces-method-chaining)

You guys are right, there are many instances where method chaining should be avoided. For example, if a chained method could return null or throw an exception it's a bad idea to use method chaining because it becomes difficult to read the stack trace. I really like to use method chaining when I have constructors with a ton of arguments.

Without method chaining

```

Person person = new Person(arg1, arg2, arg3, arg4);

```

With method chaining

```

Person person = new Person()

.setName(arg1)

.setAge(arg2)

.setHairColor(arg3)

.setLocation(arg4);

```

Note: I can't really tab it out the way I want on these forums. [Here's how I like it to look](http://i.imgur.com/58sXy.png) it's kinda similar to the with statement
Link to comment
Share on other sites

> Note: I can't really tab it out the way I want on these forums. [Here's how I like it to look](http://i.imgur.com/58sXy.png) it's kinda similar to the with statement

That's exactly what I was thinking. You're instantiating/constructing a new class.
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...