RajeshOne ChallaTwo wrote:
Is there any difference in execution time between calling a variable directly and calling its getter method.
RajeshOne ChallaTwo wrote:Both Snippet 1 and Snippet 2 do the same thing but is there any advantage of using one over the other in the same class in terms of performance. If it is a different class we can only use the getter method as the variable is private.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
RajeshOne ChallaTwo wrote:Both Snippet 1 and Snippet 2 do the same thing but is there any advantage of using one over the other in the same class in terms of performance. If it is a different class we can only use the getter method as the variable is private.
Well, since the processMessage() method is part of the class itself, it's not quite so bad to access the field directly, but I agree with Jeff: the difference is so small that you'll never notice it, and efficiency should NEVER take precedence over good design (see my quote below).
Suppose somebody changed the getter to disable messages on the 3rd Sunday of every month, viz:Now snippet 1 will no longer work correctly; but Snippet 2 will.
Correctness is always more important than speed.
Winston
RajeshOne ChallaTwo wrote:
If the reference to private variable directly has no notable performance gain over call to getter method as Jeff said, I think it's a best practice (like Raj said) to use the getter method even in the same class considering it's benefits.
RajeshOne ChallaTwo wrote:If the reference to private variable directly has no notable performance gain over call to getter method as Jeff said, I think it's a best practice (like Raj said) to use the getter method even in the same class considering it's benefits.
Winston Gutkowski wrote:
BTW, I wouldn't override getters; particularly if you're worried about performance. There may be rare corner cases for such a thing, but I can't think of a class I've written in 11 years whose getters weren't final.
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|