jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Calling an object's get functions multiple times in same code segement? Bad Practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling an object Watch "Calling an object New topic
Author

Calling an object's get functions multiple times in same code segement? Bad Practice?

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
Hi,

I'm tidying up my code and im considering creating a few extra fields to my code to save me repeatedly calling the same get function when the same value will be returned.

I have a method which sums up values but I also want to print these values in the same method like so..



Would it be better to store the value in a variable at the start of the method and save the repeated getValue() calls?

Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

If getValue() is a simple getter, then no, not really; any time that you'd save would be infinitessimal.

Let me give you some advice, though: having each method do One Thing and One Thing Only is a worthy goal to strive for. Methods that two something, and, oh, by the way, do this other thing too, lead to muddled designs and difficult code.


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling an object's get functions multiple times in same code segement? Bad Practice?