Step by step. Net code refactoring Study Notes 4. Temporary variables (temporary variable)

Source: Internet
Author: User

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)
2 {
3     double temp = 2 * (_height + _width);
4     Console.WriteLine(temp);
5     temp = _width * _height;
6     Console.WriteLine(temp);
7 }

Changed:

View sourceprint?
1 public void GetArea(double _height, double _width)
2 {
3     double temp = 2 * (_height + _width);
4     Console.WriteLine(temp);
5     double area = _width * _height;
6     Console.WriteLine(area);
7 }

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)
2 {
3     if (inputVal > 50) inputVal -= 2;
4     if (quantity > 100) inputVal -= 1;
5     if (yeaarToDate > 10000) inputVal -= 4;
6     return inputVal;
7 }

 

Changed:

View sourceprint?
1 public int Discount(int inputVal, int quantity, int yeaarToDate)
2 {
3     int result = inputVal;
4     if (inputVal > 50) result -= 2;
5     if (quantity > 100) result -= 1;
6     if (yeaarToDate > 10000) result -= 4;
7     return result;
8 }

 

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)
2 {
3     double temp = 2 * (_height + _width);
4     Console.WriteLine(temp);
5     temp = _width * _height;
6     Console.WriteLine(temp);
7 }

Changed:

View sourceprint?
1 public void GetArea(double _height, double _width)
2 {
3     double temp = 2 * (_height + _width);
4     Console.WriteLine(temp);
5     double area = _width * _height;
6     Console.WriteLine(area);
7 }

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)
2 {
3     if (inputVal > 50) inputVal -= 2;
4     if (quantity > 100) inputVal -= 1;
5     if (yeaarToDate > 10000) inputVal -= 4;
6     return inputVal;
7 }

 

Changed:

View sourceprint?
1 public int Discount(int inputVal, int quantity, int yeaarToDate)
2 {
3     int result = inputVal;
4     if (inputVal > 50) result -= 2;
5     if (quantity > 100) result -= 1;
6     if (yeaarToDate > 10000) result -= 4;
7     return result;
8 }

 

Summary

Try not to operate on the parameter and replace it with a temporary variable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.