Re: "Replace Temp with Query" use cases.
Olof Bjarnason <[email protected]>
| Newsgroups | gmane.comp.programming.refactoring |
|---|---|
| Message-ID | <[email protected]> |
2009/11/7 mszerg <[email protected]> > > > 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. > There is nothing definite about Fowlers patterns, they are just a catalogue of "transformations" that may or may not add to the readability of a certain piece of code. As I read Fowlers book, he often usees "pattern pairs". That is one pattern goes in one direction, the other in the opposite direction. (so in you case you should look for a pattern "replace query with temp variable"?). When I look at your code, I get the feeling the function doSomethingWithX ought to be a method of the class X. The reason being so many getters/setters are used inside doSomethingWithX - that is a smell that the algorithm should really be inside the class, since it 'know' so much about X. > -- > Thanks in advance, > Serg Masyutin > > > [Non-text portions of this message have been removed] > > > -- twitter.com/olofb olofb.wordpress.com olofb.wordpress.com/tag/english [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/