tags:

views:

373

answers:

5

If you have a class with some plain get/set properties, is there any reason to use the getters within the class methods, or should you just use the private member variables? I think there could be more of an argument over setters (validation logic?), but I'm wondering just about getters.

For example (in Java) - is there any reason to use option 2?:

public class Something
{
    private int messageId;
    public int getMessageId() { return this.messageId; }
    public void setMessage(int messageId) { this.messageId = messageId; }

    public void doSomething()
    {
        // Option 1:
        doSomethingWithMessageId(messageId);

        // Option 2:
        doSomethingWithMessageId(getMessageId());
    }
}
+3  A: 

With getters you wont accidentally modify the variables :) Also, if you use both getters and the "raw" variable, your code can get confused.

Also, if you use inheritance and redefined the getter methods in child classes, getter-using methods will work properly, whereas those using the raw variables would not.

antti.huima
+2  A: 

If you use the getter method everywhere - and in the future perform a code-search on all calls of getMessageId() you will find all of them, whereas if you had used the private ones, you may miss some.

Also if there's ever logic to be introduced in the setter method, you wont have to worry about changing more than 1 location for it.

Nick Josevski
+2  A: 

Java programmers in general tend to be very consistent about using getter methods. I program multiple languages and I'm not that consistent about it ;)

I'd say as long as you don't make a getter it's ok to use the raw variable - for private variables. When you make a getter, you should be using only that. When I make a getter for a private field, my IDE suggests that it replace raw field accesses for me automatically when I introduce a getter. Switching to using a getter is only a few keystrokes away (and without any chance of introducing errors), so I tend to delay it until I need it.

Of course, if you want to stuff like getter-injection, some types of proxying and subclassing framworks like hibernate, you have to user getters!

krosenvold
A: 

If the value that you are assigning to the property is a known or verified value, you could safely use the private variable directly. (Except perhaps in some special situations, where it would be obvious why that would be bad.) Whether you do or not is more a matter of taste or style. It's not a performance issue either, as the getter or setter will be inlined by the compiler if it's simple enough.

If the value is unknown to the class, you should use the property to set it, so that you can protect the property from illegal values.

Here's an example (in C#):

public class Something {

   private string _value;

   public string Value {
      get {
         return _value;
      }
      set {
         if (value == null) throw new ArgumentNullException();
         _value = value;
      }
   }

   public Something() {
      // using a known value
      _value = "undefined";
   }

   public Something(string initValue) {
      // using an unknown value
      Value = initValue;
   }

}
Guffa
A: 

If you use the getter you're ensuring you'll get the value after any logic/decisions have been applied to it. This probably isn't your typical situation but when it is, you'll thank yourself for this.

Ian Suttle