views:

126

answers:

5

Encapsulation is obviously helpful and essential when accessing members from outside the class, but when referring to class variables internally, is it better to call their private members, or use their getters? If your getter simply returns the variable, is there any performance difference?

+8  A: 

There shouldn't be a significant performance difference, and the reason you stick to using the properties is because that's the whole point of encapsulation. It keeps all accesses of those private members consistent and controlled. So if you want to change the property getter/setter you don't have to think "do I need to duplicate the same functionality elsewhere in the places where I decided to access the private member directly?"

Daniel DiPaolo
+3  A: 

The benefit would be in the cases where there was validation to be done on the Get or Set. This would be in one place and always called.

As far as I know (from other questions asked on Stack Overflow) when the Getter simply returns the value the code generated is the same as for accessing the variable directly.

ChrisF
+6  A: 

Accessing the field directly or using the getter usually doesn't make a big difference, unless your getter performs some lazy initialization or other processing. So it depends on what the getter does, but my rule of thumb is to always use the getter except in specific cases.

To assign a value to the field, don't forget that setters often include validation code or raise an event. In that case you should always use the setter.

Thomas Levesque
+1: This is a good rule of thumb. There is a general tendency to use the private member.."I'm not doing anything in the getter and/or setter..it would be wasteful to call a method...I'll just do an assignment...cool." (guilty..I admit it) Then later you, or worse somebody else, adds some validation to the setter and ...bang... your the target at a who shot the bunny meeting. Like Thomas said..there is always a room for a special case exemption...by habit use the setter.
Rusty
Anyway, simple getters or setters are usually inlined by the JIT compiler, so it's not even an optimization to access the field directly...
Thomas Levesque
A: 

The performance difference is neglectable, in about 98% of the time.

You should always use properties even though your getter or setter just get or set your private member. This will allow you to bring changes as your application evolves. Doing so, you'll be able to bring some restrictions or some other encapsulation within your properties without breaking your code. Otherwise, once you decide to write properties because of a reason X, you'll find yourself obliged to refactor all of your code to get or set the property instead of your private member.

Will Marcouiller
A: 

I like to use internal properties for lazy initialization, where in the property you make sure the object is initialized. This ensures I am not using class level variables that have not yet been initialized.

TestClass1 _class1
internal TestClass1
{

get
{

    if (_class == null)
     _class = new TestClass1()

    return _class1

}

}
dretzlaff17