Reputation: 21147
Was working with some code today that I did not write and noticed that in a class there was a private member m_privateMember
. The coder had also included a method called GetPrivateMember()
which contained only return m_privateMember
. It is worth mentioning that this method is heavily used within the class itself as opposed to simply using the private field that is accessible in this scope.
Semantically I see a lot of things wrong with this. A method vs. an actual getter/setter seems sloppy and I've always thought that if you are within class scope it is better form to actually use the private field rather than the getter. Needless to say, I am going to clean up the code purely for the reason that it does not meet coding standards in our workplace but I was curious to know if doing so would also improve performance in any way?
Does calling a method that simply returns a private field invoke more overhead?
Upvotes: 1
Views: 392
Reputation: 39625
To answer your question directly: It depends.
A property which is just a single return private field
is very likely to inlined by the compiler (when optimizations are enabled) for any calls inside that assembly. In this case, it is an identical operation to accessing the field directly.
For calls accessing the property from outside the assembly, the JIT compiler is likely to inline the call in the same way as the static compiler, but the static compiler is not allowed to do so, as it has to respect the interface provided by the assembly. This is so that you can replace a compiled assembly with a new version which has no public changes and not require a re-compile of the application source to use the new version.
As the property gets more complicated, the chances of being inlined by either the static compiler or JIT compiler become less and the property is invoked more like you would imagine.
All this said, the discussion of performance is really an academic one. In almost all practical examples, the difference between invoking the property versus reading the private field is trivial. The only time it will be measurable is if you're running on some very low-spec embedded system or doing billions of reads every second for each user of your system.
Most computers are so fast that you should be choosing the technique which makes your code more maintainable, not the one which gives you an extra 0.0000001 second decrease in execution time.
Upvotes: 2
Reputation: 45083
Does calling a method that simply returns a private attribute invoke more overhead?
There's no real noticeable difference but aesthetics and proper usage here, all else being equal, as a getter and setter method is generated per property per implementation of get and set - so all calls to return the value of a thing from a property is actually a method call, although there might be some implementation factors / optimisation going on that make using properties 'better' even in terms of speed, but with negligible improvements to comparable effect for the most part.
Upvotes: 1