• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"replace temp with query" question

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried applying this suggested method to my code.
I ran into a problem in couple of instances.
What if the query method does something expensive?
w'd'nt i end up executing it more than once?

How to handle this issue.
thanks.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"replace query with temp" ?
 
krithika desai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Don Kiddick:
"replace query with temp" ?


yeah the way it was earlier. There was a temp that was storing the value so that it need not be calculated again. But my question is how do we address performace issues like in this case.
Does it mean that this refactoring in applicable only in those cases where there is no performance hit?
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the #1 motivation for "replace query with temp" is if the query is expensive. Executing it more than once may also be a motivation, but again this boils down to a performance consideration.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you may be missing the point of these refactorings if you expect to go direct to the right answer in one step. Refactorings are small steps, and most sensible changes will be comprised of several refactorings in sequence.
A typical sequence might be something like
  • replace temp with query
  • pull-up or push-down method etc.
  • extract method object
  • etc.


  • Which could end up with the same performance benefits, but with the cacheing of the result now nearer to the source - all calls to the "query" method benefit from the cacheing, and the cached value can be updated without causing invalid-temp problems with lots of calling code.
    Did that make sense? I'm sure I can come up with some code examples for the above if needed.
     
    There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic