Java return the Object/modify the object (coding guidelines)

后端 未结 6 1031
野性不改
野性不改 2021-01-12 15:08

If a method populates/modifies an object, would it be preferable to return the object or to keep the return type as void and the method would modify the Object through its r

6条回答
  •  长发绾君心
    2021-01-12 16:07

    I depends on your style, but one advantage of returning: you could call populate(o).doSomethingElse();, i.e. you can chain method calls.

    Have a look at how StringBuilder does that for example, which allows things like this new StringBuilder().append("a").append("b")....

提交回复
热议问题