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

前端 未结 14 671
既然无缘
既然无缘 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条回答
  •  梦毁少年i
    2020-12-29 02:32

    A setter could modify as part of validation, but a getter should return the value and let the validation be done by the caller. If you do validate, then how should be documented.

提交回复
热议问题