|Come for the quick hacks, stay for the epiphanies.
Refactoring Perl #5 - Introduce Explaining Variableby agianni (Hermit)
|on Aug 02, 2007 at 18:21 UTC
This refactoring pattern proves that context is the key refactoring decisions as it presents the exact opposite of the technique suggested in Inline Temp. Here's what it looks like using List::Util qw( min max ):
Interestingly enough, Fowler immediately follows this example with the following disclaimer:
For this example, I usually wouldn't have done the explaining temps; I would prefer to do that with Extract Method (p. 126)
and he goes on to show the refactoring using that technique. Fowler suggests that the appropriate time to use this technique is when "Extract Method is more effort" (p. 127). Obviously it's up to the programmer to decide what "more effort" is, but in my mind it'll be much of the time. In other words, this will generally be a better option than Extract Method unless the temps would be useful if shared through a method.
Another nice side effect, if not one of the primary intended effects of this technique is that it makes your code self-documenting. The comment at the top of the original code is unnecessary in the refactored version, as the last line essentially says the same thing explicitly in Perl.