Split temporary variable (anatomy of temporary variables)
Overview
A temporary variable in the program is assigned more than once. It is neither a cyclic variable nor a set with a temporary variable (collecting temporary variable)
Motivation)
Temporary variables have different purposes. Some of them are naturally transferred to temporary variables that are assigned multiple times. loop variables and (set with temporary variables) are two typical examples: loop Variables change with each running of the loop (for example, for (INT I = 0; I <10; I ++) in the statement I); The set uses a temporary variable (collecting temporary variable) to collect a value (through the entire function operation.
Mechanism)
1. In the declaration of the temporary variable (to be dissected) and its first value assignment, modify its name.
If the value assignment statement is an expression of I = J later, it means that this is a set with temporary variables, so do not dissect it. The Set uses temporary variables to accumulate, concatenate strings, write streams, or add elements to a collection.
2. Use the second value assignment of the temporary variable as the field. modify all the reference points of the temporary variable so that they can reference the new temporary variable.
3. Re-declare the original temporary variable at the second value assignment.
Example
View sourceprint?
1 |
public void GetArea( double _height, double _width) |
3 |
double temp = 2 * (_height + _width); |
4 |
Console.WriteLine(temp); |
5 |
temp = _width * _height; |
6 |
Console.WriteLine(temp); |
Changed:
View sourceprint?
1 |
public void GetArea( double _height, double _width) |
3 |
double temp = 2 * (_height + _width); |
4 |
Console.WriteLine(temp); |
5 |
double area = _width * _height; |
6 |
Console.WriteLine(area); |
Remove assignments to parameters
Overview
The Code assigns a value to a parameter.
Motivation)
First, we need to make sure that everyone knows the meaning of this statement (assigning values to parameters. If you pass an object named Foo as a parameter to a function, (assigning values to parameters) means changing Foo,
Make it reference another object.
Mechanism)
1. Create a temporary variable and assign the value of the parameter to be processed.
2. replace all the reference points of this parameter with (the reference action of this temporary variable ).
3. Modify the value assignment language to assign values to the newly created temporary variable.
Example
View sourceprint?
1 |
public int Discount( int inputVal, int quantity, int yeaarToDate) |
3 |
if (inputVal > 50) inputVal -= 2; |
4 |
if (quantity > 100) inputVal -= 1; |
5 |
if (yeaarToDate > 10000) inputVal -= 4; |
Changed:
View sourceprint?
1 |
public int Discount( int inputVal, int quantity, int yeaarToDate) |
4 |
if (inputVal > 50) result -= 2; |
5 |
if (quantity > 100) result -= 1; |
6 |
if (yeaarToDate > 10000) result -= 4; |
Summary
Try not to operate on the parameter and replace it with a temporary variable.
Split temporary variable (anatomy of temporary variables)
Overview
A temporary variable in the program is assigned more than once. It is neither a cyclic variable nor a set with a temporary variable (collecting temporary variable)
Motivation)
Temporary variables have different purposes. Some of them are naturally transferred to temporary variables that are assigned multiple times. loop variables and (set with temporary variables) are two typical examples: loop Variables change with each running of the loop (for example, for (INT I = 0; I <10; I ++) in the statement I); The set uses a temporary variable (collecting temporary variable) to collect a value (through the entire function operation.
Mechanism)
1. In the declaration of the temporary variable (to be dissected) and its first value assignment, modify its name.
If the value assignment statement is an expression of I = J later, it means that this is a set with temporary variables, so do not dissect it. The Set uses temporary variables to accumulate, concatenate strings, write streams, or add elements to a collection.
2. Use the second value assignment of the temporary variable as the field. modify all the reference points of the temporary variable so that they can reference the new temporary variable.
3. Re-declare the original temporary variable at the second value assignment.
Example
View sourceprint?
1 |
public void GetArea( double _height, double _width) |
3 |
double temp = 2 * (_height + _width); |
4 |
Console.WriteLine(temp); |
5 |
temp = _width * _height; |
6 |
Console.WriteLine(temp); |
Changed:
View sourceprint?
1 |
public void GetArea( double _height, double _width) |
3 |
double temp = 2 * (_height + _width); |
4 |
Console.WriteLine(temp); |
5 |
double area = _width * _height; |
6 |
Console.WriteLine(area); |
Remove assignments to parameters
Overview
The Code assigns a value to a parameter.
Motivation)
First, we need to make sure that everyone knows the meaning of this statement (assigning values to parameters. If you pass an object named Foo as a parameter to a function, (assigning values to parameters) means changing Foo,
Make it reference another object.
Mechanism)
1. Create a temporary variable and assign the value of the parameter to be processed.
2. replace all the reference points of this parameter with (the reference action of this temporary variable ).
3. Modify the value assignment language to assign values to the newly created temporary variable.
Example
View sourceprint?
1 |
public int Discount( int inputVal, int quantity, int yeaarToDate) |
3 |
if (inputVal > 50) inputVal -= 2; |
4 |
if (quantity > 100) inputVal -= 1; |
5 |
if (yeaarToDate > 10000) inputVal -= 4; |
Changed:
View sourceprint?
1 |
public int Discount( int inputVal, int quantity, int yeaarToDate) |
4 |
if (inputVal > 50) result -= 2; |
5 |
if (quantity > 100) result -= 1; |
6 |
if (yeaarToDate > 10000) result -= 4; |
Summary
Try not to operate on the parameter and replace it with a temporary variable.