Hi all,
do you have any wisdom/experience to share regarding Fowler's "Replace
Temp with Query" refactoring?
http://sourcemaking.com/refactoring/replace-temp-with-query.
Although the refactoring is often a vital step before "Extract
Method", that is not its only motivation.
In the first example of refactoring presented in Fowler's
"Refactoring: improving the design of existing code"
http://books.google.com/books/about/Refactoring.html?id=1MsETFPD3I0C
(the "Video Store" example) the "Replace Temp with Query" refactoring
is used no less than three times.
E.g. at some point in the example we have a statement method which has
temporary variable 'totalAmount':
https://gist.github.com/1059890.
Fowler applies "Replace Temp with Query" to 'totalAmount' and the
result is this:
https://gist.github.com/1059894. He then applies the
same refactoring to temporary variable 'frequentRenterPoints', and we
are left with this:
https://gist.github.com/1059899.
In both cases the refactoring was applied not because it was a vital
step before "Extract Method"
http://sourcemaking.com/refactoring/extract-method
but just for the benefits it brings. E.g. Fowler says:
###########################################
By replacing the temp with the query method, any method in the class
can get at the information. That helps a lot in coming up with cleaner
code for the class.
###########################################
He also says that one concern with the refactoring we have just seen
lies in performance:
###########################################
The old code executed the "while loop" once, the new code executes it
three times. A while loop that takes a long time might impair
performance. Many programmers would not do this refactoring simply for
this reason. But note the words _if_ and _might_. Until I profile I
cannot tell how much time is needed for the loop to calculate or
whether the loop is called often enough for it to affect the overall
performance of the system. Don't worry about this while refactoring.
When you optimize you will have to worry about it, but you will then
be in a much better position to do something about it, and you will
have more options to optimize efectively...
###########################################
The fact that the refactoring (especially this more radical variant
involving loop duplication) appears three times in this first example
suggests to me that the refactoring is significant, is commonly
applied, and is exemplary about what we do when we refactor, and why
we do it: what we gain by doing it. But is it? What do you normally
do? Do you always or typically perform this type of refactoring? Do
you typically wait for 'the first bullet'? Do you avoid this
refactoring as much as possible? Do you face objections from greener
programmers?
In "One Thing: Extract till you Drop"
http://blog.objectmentor.com/articles/2009/09/11/one-thing-extract-till-you-drop,
Unce Bob says (about another refactoring: Extract Method):
###########################################
Perhaps you think this is taking things too far. I used to think so
too. But after programming for over 40+ years, I’m beginning to come
to the conclusion that this level of extraction is not taking things
too far at all. In fact, to me, it looks just about right.
###########################################
Do you think the example of "Replace Temp with Query" at hand is
taking it too far? Do you think it would only happen when doing
'Refactor till you Drop'? Recently I watched Uncle Bob's "Clean Code
Episode III - Screencast 3: Video Store"
http://www.cleancoders.com/codecast/clean-code-episode-3-screencast3-videostore/show,
in which he shows us his own refactoring of the video store example.
He does not apply "Replace Temp with Query" to 'totalAmount' and
'totalFrequentrenterPoints', but that seems to be because he goes down
another route in which he makes the two variables member fields, and
their usage ends up in a very short helper method, in which their
presence is less conpicuous and their updating does not seem to make
the code unnecessarily harder to read/understand.
In Corey Haines' 'Taking it too far' v 'Taking too much time'
http://programmingtour.blogspot.com/2010/12/taking-it-too-far-v-taking-too-much.html
he says:
###########################################
As I've traveled around and had the opportunity to work with people of
widely varying experience and skill level, I've noticed that the big
difference is which corners get cut. For people with less experience,
say 1-5 years, the 'get it done' code is significantly different, less
clean, than the code written by those with a lot more experience, say
15-20 years, yet written in the same amount of time. This makes sense
and isn't a knock against beginners, it is just a good indication that
there is always more to learn. The gap between what we actually write
and what we think we would write given enough time is always there,
just at a different level. As we gain more experience, that gap simply
moves upward, so the code is cleaner given the same constraints.
###########################################
Do you, or people you have worked with object to "Replace Temp with
Query" because it is 'taking it too far', or because 'it takes too
much time'?
Taking Ron Jeffries' "Clean Code: Too Much of a Good Thing?"
http://xprogramming.com/articles/too-much-of-a-good-thing/ into
account: do you think we are we cutting corners when we don't apply
"Replace Temp with Query"?
At which of the Dreyfus levels do you think developers use "Replace
Temp with Query":
* Novice Programmer
* Advanced Beginner
* Competent
* Proficient
* Expert
Thanks,
Philip