What are your opinions on method chaining? I feel that I’m overusing it; pretty much every method I’m creating now returns the current object. I’ve looked around on Google and found a few discussions on the topic though they don’t really have a decisive answer. My code looks something like this:
object1.doSomething().doSomethingElse().doAnotherThing();
The cons that I’ve found are:
- Can make code harder to read
- Makes code harder to debug
- Loss of a few bytes of memory for every return statement
To be honest, #3 is nothing major and #1 would only apply with something like this:
object1.doSomething().thisMethodReturnsAnotherObject().doSomethingWithReturnedObject();
Are there any more cons? Can method chaining be overused? Could it be considered bad practice?