• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic