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

后端 未结 6 1004
野性不改
野性不改 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 15:48

    I would say the "first" option, to return the object. This has no disadvantages, and leave you "room" to make a change of implementation in the future (for example, returning a "deep copy" instead of the same object) without modifying the signature.

    In short, i see it more flexible.

提交回复
热议问题