Split Temporary Variable (Interim Variable)
Summary:
Your program has a temporary variable to be assigned once, which is neither a loop variable, nor a collection of temporal variables (Collecting TempoRiable). For each assignment, create an independent, corresponding temporary variable.
TIPS:
If the temporary variable takes a number of responsibilities, he should be replaced (dissection) for multiple temporary variables, each variable only assumes a responsibility. The same temporary variable takes two different things, which will make the code reader confused.
Comments by Allen Lee:
When we add a new function, we have written it in and without considering what is normal, just a little bit of peacefulness. But when this function is increasingly expanded (constantly modifying and / or adding new code), we have to pay attention to our maintenance work excessively large and hindering our maintenance. Many times, we want to use Replace Temp with Query to share the data of local variables in Class in Class, but because the selected code is assigned multiple assignments for the temporary variable (mostly for convenience, a temporary variable Using) When you are unable to reconstruct, you can consider using split temporary variable to divide the variable's responsibility, and share the "joy" of the variable to other members of Class. Not long ago, when I was studying this retrieval principle, I had a question: Since the variable is only assigned once, why not declare it as a constant? With this question, I e-mail Martin Fowler, the following is our text:
Here is the email I sent to Martin:
Hi, Fowler. I'm Now Reading your book
Refactoring: Improving The Design of Existing Code, And When I Reached
Split Temporary Variable, a Question Come to Me. NOW First Take a Look at a Para you wrote in your book:
Many other temporaries are used to hold the result of a long-winded bit of code for easy reference later. These kinds of variables should be set only once. That they are set more than once is a sign that they have more than one responsibility within ...............................
So a variable that is only signed once is not just like a constant? And why should we use variable here? Can we use a constant here instead of variable due to the fact that the variable is only signed once and will not be changed In The Future? I am Looking Foward to Hearing from You!
Yours Sincerely,
Allen Lee
Wed., October 20, 2004
Martin gave me a simple reply, only two sentences, but pointed out that my blind spot:
constants are usually constant at compile time or at least constant for the length of the program execution. Local variables (in this form) are constant only for the a single execution of the block they are defined in.
It turned out that my understanding of constants stayed at this one-level level that was only assigned once. From Martin Reply We can know that the value of constant is usually determined when compiling. However, I void remembers that C # provides two ways of declared variables: Readonly and Const. One of the two important differences is that the value of the Const constant must be decided at compile; the value of the readonly constant can be determined at the time of compiling! This is the problem, is it necessary to use Readonly constants? However, the problem is far as simple, readonly can only be used to modify the class's field! (For further discussion of Readonly and Const, see "Readonly vs. const [c #]".) Actually, (in the function scope) local variable data should not be stored inside the variable, as data is determined at runtime. Although a variable is only used to store a data, it seems a bit waste, but makes a clear responsibility system that makes the code more flexible.