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

前端 未结 14 531
既然无缘
既然无缘 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:29

    In my opinion, unless you are doing lazy-loading (which you are not in that case), getters should not change the value. So I would either:

    Put the change in the setter

    public void setMyValue(String value) {
        if(value == null || value.isEmpty()){
            this.myValue = "N/A";
        } else {
            this.myValue = value;
        }
    }
    

    Or make the getter return a default value if value not set properly:

    public String getMyValue() {
        if(this.myvalue == null || this.myvalue.isEmpty()){
            return "N/A";
        }    
        return this.myValue;
    }
    

    In the case of lazy-loading, where I would say that changing your members in a getter is fine, you would do something like:

    public String getMyValue() {
        if (this.myvalue == null) {
            this.myvalue = loadMyValue();
        }    
        return this.myValue;
    }
    
    0 讨论(0)
  • 2020-12-29 02:29

    I usually define a specific getter.

    Never alter original getter:

     public String getMyValue(){
         return this.myValue
     }
    

    And create a specific getter:

    public String getMyValueFormatted(){
    
        if(this.myvalue == null || this.myvalue.isEmpty()){
           return "N/A";
        }else{
           return this.myValue;
        }
     }
    
    0 讨论(0)
  • 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.

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

    I think it is actually quite a bad practice if your getter methods change the internal state of the object.

    To achieve the same I would suggest just returning the "N/A".

    • Generally speaking this internal field might be used in other places (internally) for which you don't need to use the getter method. So in the end, the call to foo.getMyValue() could actually change the behaviour of foo.

    Alternatively, the translation from null to "N/A" could be done in the setter, i.e. the internal value could be set to "N/A" if null is passed.


    A general remark:
    I would only add states such as "N/A" if they are expected by some API or other instance relying on your code. If that is not the case you should rely on the standard null types that are available to you in your programming language.

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

    I would change better the setter method so, if the value is null or empty, the N/A is assigned to the attribute. So, if you use the attribute in other methods inside the class (v.g. toString()) you will have the intended value there.

    Alternatively, change the setter method to launch an exception when the value being set is not right, so the programmer is forced to improve its handling prior to setting the value.

    Other than that, it is ok.

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

    I do feel this is a bad practice unless and until you explain the reason why it is so necessary for you modify the object inside the getter method instead of doing it inside the setter method.
    Do you feel this cannot be done for some reason? Could you please elaborate?

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