Is it bad practice to have my getter method change the stored value?

前端 未结 14 530
既然无缘
既然无缘 2020-12-29 02:08

Is it bad practice to change my getter method like version 2 in my class.

Version 1:

 public String getMyValue(){
     return this.myValue
 }
         


        
相关标签:
14条回答
  • 2020-12-29 02:41

    State changes in getters should be a hanging offence. It means that client code must be careful about the order in which it accesses getters and setters and to do this it must have knowledge of the implementation. You should be able to call the getters in any order and still get the same results. A related problem occurs when the setter modifies the incoming value depending on the current state of the object.

    0 讨论(0)
  • 2020-12-29 02:44

    absolutely yes, it's a bad pratice.

    Imagine you communicate accross network with a third party (remoting, COM, ...), this will increase the round-trip and then hit application performance.

    0 讨论(0)
提交回复
热议问题