Java Conventions: use getters/setters WITHIN the class?

前端 未结 12 1795
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 02:32

My professor really emphasizes protecting against privacy leaks by always using accessors and mutators to access private instance variables; however, do I have to use the ge

相关标签:
12条回答
  • 2020-11-30 02:58

    You can use the accessors and mutators, but its a messy standard to follow.

    It clutters up your code and confuses anyone trying to read it thinking it might not be a part of your class.

    Basically, just access the variables directly from inside your class, and indirectly from anywhere else.

    0 讨论(0)
  • 2020-11-30 02:58

    If exists getters and setters, then should be used always even inside class. Of course if getter and setter do something more in method then it must be considered if it fullfils the needs.

    Should be used not means must be used. Property can be used directly ( only inside class if property is private - and should be ) but in good practice is to use getter and setter.

    So using property directly is possible, but I recommend use get and set because it give more flexibility and can do something more than tipical assign or get.

    For example if in first place setter only sets something, but after a while setter is changed to do something more.

     setName(name){
    
         this.name=name;
     }
    

    So in code setName("John"); is equal name="John". But imagine that after a while we want to set another property when name is set:

     setName(name){
    
         this.name=name;
         this.nameIsSet=true;
     }
    

    another example ( Listener pattern ):

    setName(name){
    
         this.name=name;
         this.listener.nameChanged(this.name); //we call listener that variable changed
     }
    

    Then programmer needs to find every assigment like name="John" in class and refactor it to new behavior. If only setName is used then no code change must be done.

    Of course everything depends from need and is possible that setting, getting property from outside do something more and different and it not fullfils our need inside class.

    0 讨论(0)
  • 2020-11-30 02:59

    I think we should use getters() and setters() instead of accessing directly. It is also makes debugging very easy; for example, if you need to assign a variable to multiple place in your class and later want to find out from how many places the variable is assigned to, then you need to find all the assignment and set the break point.

    However, if you use a setter you can simply put a break point inside the setter method and can see how many time the variable is assigned.

    0 讨论(0)
  • 2020-11-30 03:00

    In general, no. If your getter returns something other than the value of the field then you should use the method, but in that rare case your method should have a more descriptive name. For a bad example, if you have:

    public void setName(String name)
    {
      _name = name;
    }
    

    and your getter returned something else, like

    public String getName()
    {
      return _name.toUpperCase();
    }
    

    then yes, you should use the getter. It would be better, though, to have a more descriptive name for that getter:

    public String getNameAsUppercase()
    {
      return _name.toUpperCase();
    }
    
    0 讨论(0)
  • 2020-11-30 03:00

    On the flip side, consider it from a design standpoint. One of the motivations for getters/setters is that the underlying data storage can change and things that implement the class won't need to be changed since it is encapsulated.

    So, keeping that in mind, using getters/setters within the class makes future changes easier. Instead of having to find all the places that alter the member directly, you just have to change the getter/setter. Depending on the complexity of the class, this may significantly reduce the amount of work it takes to change the storage members.

    For example, let's assume you start out with the age variable in years. Then you decide later to store it as seconds for some reason. But you want to always print it in years anyway. So in your example, you could do the math in your toString() function (and anywhere else that wants years as the units) or you can just change the math in the getAge() routine to return years from the seconds and nothing else has to change.

    Obviously that example is a bit trivial. The more complicated the class, the more useful it is to use getters/setters within it.

    0 讨论(0)
  • 2020-11-30 03:01

    When I design a class I try to make a clear distinction between the inside (implementation details) and the outside (the interface exposed to the world). Getters and setters provide a convenient place to convert values between the form in which they are stored in the object’s instance members and the form in which the outside world sees them. Using getters and setters internally would muck that up, because they'd be getting used by both the inside and outside.

    If you find yourself wanting to hide part of a class from another part of the same class, consider breaking off the part you want to hide into its own class.

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