Benefits and drawbacks of method chaining and a possibility to replace all void return parameters by the object itself

前端 未结 9 1830
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 06:58

I am mostly interested in Java, but I think it\'s a general question. Recently I\'ve been working with Arquillian framework (ShrinkWrap) that uses a lot of meth

9条回答
  •  青春惊慌失措
    2020-11-29 07:14

    The downside I have found of using method chaining is debugging the code when NullPointerException or any other Exception happens. Suppose you are having following code:

    String test = "TestMethodChain"; test.substring(0,10).charAt(11); //This is just an example

    Then you will get String index out of range: exception when executing above code. When you get into realtime situations and such things happen then you get at which line error has happened but not what part of chained method caused it. So it needs to be used judiciously where it known that data will always come or errors are handled properly.

    It has its advantages too like you need not write multiple lines of code and using multiple variables.

    Many frameworks/tools use this like Dozer and when I used to debug the code it I had to look through each part of chain to find what caused error.

    Hope this helps.

提交回复
热议问题