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

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

    Yes. It's a bad practice.

    Why?

    When the value is set (in a constructor or setter method), it should be validated, not when a getter method is called. Creating a private validate* method for this is also a good idea.

    private boolean validateThisValue(String a) {
         return this.myValue != null && !this.myValue.isEmpty();
    }
    
    public void setThisValue(String a) {
        if (validateThisValue(a)) {
            this.myValue = a;
        } 
        else {
            // do something else
            // in this example will be
            this.myValue = "N/A";
        }
    }
    

    And, in the getter method, never ever change the state of the object. I have worked on some projects, and the getter often must be made const: "this method cannot change internal state".

    At least, if you do not want to complicate things, in the getter method, you should return "N/A" rather than change internal state and set myValue to "N/A".

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

    You can use some value holder for this purpose. Like Optional class in guava library.

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

    This actually highly depends on the contract you want to enforce with your get()-method. According to design-by-contract conventions the caller has to make sure that the preconditions are met (which means doing a validation in a setter method often is actually bad design) and the callee (I do not know if that's the correct english term for that, i.e., the called one) makes sure that the post conditions are met.

    If you define your contract so that the get()-method is not allowed to change the object then you are breaking your own contract. Think about implementing a method like

    public isValid() {
        return (this.myvalue == null || this.myvalue.isEmpty());
    }
    

    Advantage of this approach is that you do not have to check wether the return of your get() is "N/A" or something else. This also can be called before calling set() to validate that you do not insert illegal values into your object.

    If you want to set a default value you should do that during initialization.

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

    I think it's better to initialize this.myValue = "N/A". And subsequent calls to setMyValue should modify the this.myValue according to your business conditions.
    The getMyValue shouldn't modify in any way this.myValue. If your needs are to return a certain value, you should return that value (like "N/A") and not alter this.myValue . Getters must not modify member's value.

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

    No. You're doing two things here. Getting and setting.

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

    Do what ever you like. After all getters and setters are just another public methods. You could use any other names.

    But if you use frameworks like Spring, you are bound to use those standard names and you should never put your custom codes inside them.

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