unknown Posted September 26, 2012 Author Share Posted September 26, 2012 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 More sharing options...
JohnPony Posted September 26, 2012 Share Posted September 26, 2012 Thanks for the explanation!I may end up looking into this further, it would really help. Link to comment Share on other sites More sharing options...
Marsh Posted September 26, 2012 Share Posted September 26, 2012 Interesting, never seen that before. I would say that makes code less readable personally. But could be useful. Thanks. Always good to learn more tricks of a language. Link to comment Share on other sites More sharing options...
Mal Posted September 26, 2012 Share Posted September 26, 2012 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 More sharing options...
JeffSventora Posted September 26, 2012 Share Posted September 26, 2012 Whoa, never saw that before, very interesting indeed. Link to comment Share on other sites More sharing options...
unknown Posted September 26, 2012 Author Share Posted September 26, 2012 > 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 More sharing options...
Mal Posted September 27, 2012 Share Posted September 27, 2012 > 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 statementThat's exactly what I was thinking. You're instantiating/constructing a new class. Link to comment Share on other sites More sharing options...
JohnPony Posted November 25, 2012 Share Posted November 25, 2012 Just finished reading this and wow, very neat & very useful if used properly. Link to comment Share on other sites More sharing options...
Marsh Posted November 26, 2012 Share Posted November 26, 2012 > That's exactly what I was thinking. You're instantiating/constructing a new class.If you are doing something like that it would make more sense to just make a constructor that takes those arguments. Link to comment Share on other sites More sharing options...
Mal Posted November 26, 2012 Share Posted November 26, 2012 > If you are doing something like that it would make more sense to just make a constructor that takes those arguments.Yeah, I know; I wouldn't use the way he has. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now