|Don't ask to ask, just ask|
Refactoring Perl #3 - Inline Tempby agianni (Hermit)
|on Jul 17, 2007 at 19:39 UTC||Need Help??|
Inline Temp is probably the most trivial refactoring pattern:
This causes occasional consternation in code reviews I attend, as some like to create temp variables to clarify their code and other find it to be unnecessary clutter. I find that programmers most often employ this technique when a verbose method name or deep data structure would otherwise exist multiple times in code:
I generally avoid getting into scuff-ups about such matters. Fowler suggests this refactoring for situations where the temp gets in the way of other refactoring, generally Replace Temp With Query (up next).
Fowler also suggests an interesting technique to aid in the refactoring:
Declare the temp as final if it isn't already and compile. This checks that the tem is really only assigned to once. (Fowler, p. 119)
While Perl lacks the means to turn a once dynamic variable into a read-only (does anyone know of one?) there are a number of ways to create read only variables in perl. My favorite is Readonly. This is a debugging technique that I hadn't considered before.