Method chains can be a little dangerous. It all depends on the dependencies.
When using a chain of methods, it is useful to keep track of the objects returned by each part of the chain.
If they are not all the same, for example, they are not all lines, then itβs nice to break the chain into separate statements. Or perhaps select them in a separate function.
Say you have a chain, for example
somearray.pop().getATagWrapper().getTag().setSrc("http://www.stackoverflow.com")
Implicit chain dependencies: Array, Object1, TagWrapper, Tag, String
Now the function you wrote is now associated with all of these objects, and any change to them can potentially destroy the chaos with your code.
Where we look
someString.trim().substr(12).trim().concat("with awesome")
Everything deals only with the String object.
See Law of Demeter for more details.
source share