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

后端 未结 6 1044
野性不改
野性不改 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:08

    I would go with Command-Query separation generally.

    That is to say, if the method mutates the argument, it should not also return the argument.

    There are however (as noted in the wikipedia article above) situations where violating this general principle is appropriate.

提交回复
热议问题