Re: "Replace Temp with Query" use cases.

"MSZerg" <[email protected]>
Newsgroups gmane.comp.programming.refactoring
Message-ID <[email protected]>
Hi All,

  Sorry for long replies...

  The missing details here can be following:
0. doSomethingWithX is in global scope, implementing API functionality, so not much possible to move this to separate function of the same level. Move to X is a good idea, second this, and most probably I'd implement it that way.
1. parameters set to $y are regular ints\bools\strings, simple setters in other words.

  From my point of veiw I see following advanteges of the second way:
  a. you definitly see that you're working with _the same_ object
  b. less error prone, as it doesn't depend much on implementation of getY()... maybe someday one hero will make it returning new instance on each call?
  c. 2x speed (less function calls)
  d. 2x less code chars (maybe not very significant)

  The only argument for the first way is:
  a. it is more readable (IMO -- it is NOT, due to contrast with a. and b. of the previous advanteges...)

  So what do you think? How do you write it?

--
Thanks in advance,
  Serg Masyutin



--- In [email protected], "mszerg" <mszerg@...> wrote:
>
> Hi All,
> 
> First, I want to say that this is not a joke and kindly ask you to
> reply, suppose this will be simple and won't take much of your time :-)
> 
> Recently I faced the following situation. In our project we have 
> several functions with php code like this:
> 
> // some global function
> function doSomethingWithX(X $x) {
> $x->getY()->setA(..);
> $x->getY()->setB(..);
> $x->getY()->setC(..);
> ...
> $x->getY()->setZ(..);
> }
> 
> // this is how X looks like
> class X {
> private $y;
> // lazy getter of y
> function getY() {
> if (!$this->y) {
> $this->y = somehowCreateY();
> }
> return $this->y;
> }
> ...
> }
> 
> During code review I suggested the following refactoring:
> 
> function doSomethingWithX(X $x) {
> // store reference locally
> $y = $x->getY();
> // modify fields
> $y->setA(..);
> $y->setB(..);
> $y->setC(..);
> ...
> $y->setZ(..);
> }
> 
> In my opinion it is just simpler, not saying 2x faster. But the authors
> say that "...according to Fowler's "Replace Temp with Query" refactoring
> strategy our version is better". From my conversations with the other
> colleagues, this looks like common approach. I suppose that this can be
> misunderstanding of the strategy use cases, but I may be wrong. Please,
> explain me what is right and wrong in this situation.
> 
> --
> Thanks in advance,
>   Serg Masyutin
> 
> 
> [Non-text portions of this message have been removed]
>




------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/refactoring/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/refactoring/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.