Reconstruction Techniques: reorganizing functions [4]. Restructuring manipulation functions

Source: Internet
Author: User

Reconstruction Techniques: reorganizing functions [4]. Restructuring manipulation functions

Total returned directory

Directory of this section

  • Split Temporary Variable (break down Temporary variables)
  • Remove Assignments to Parameters (Remove the value assigned to the parameter)

6 Split Temporary Variable (break down Temporary variables) Summary

A temporary variable in your program is assigned more than once. It is neither a cyclic variable nor used to collect computing results.

Creates an independent temporary variable for each assignment.

Motivation

Temporary variables have different purposes.

1. Cyclic variables;

2. Result collection variables;

3. Save the computation results of a lengthy piece of code for later use.

In the third case, the temporary variable should be assigned only once. If they are assigned more than once, they assume more than one responsibility in the function. If a temporary variable assumes multiple responsibilities, it should be divided into multiple temporary variables, and each variable assumes only one responsibility. The same temporary variable undertakes two different things, which will confuse the readers of the Code.

Example
double GetTotalCost(){    double result = 0;    double money = _chickMoney + _chipMoney;    result += money;    money = _cocoaMoney + _coffeeMoney;    result += money;    return result;}

In this example, the Temporary Variable money is assigned twice. In addition, it does not implement the cumulative result, and the cumulative result is given to the result. Therefore, we need to refactor to make the intention of this variable clearer.

So the first step is to find the place where the variable was declared for the first time, rename it, modify all reference points before the second value assignment, and re-declare it at the Second Value assignment:

double GetTotalCost(){    double result = 0;    double mealMoney = _chickMoney + _chipMoney;    result += mealMoney ;    double money = _cocoaMoney + _coffeeMoney;    result += money;    return result;}

Now, the new temporary variables only assume the first responsibility of the original money. In addition, we re-declare the money in the second assignment of the original money variable. Then, continue to process the second assignment of the money temporary variable.

double GetTotalCost(){    double result = 0;    double mealMoney = _chickMoney + _chipMoney;    result += mealMoney ;    double drinkMoney = _cocoaMoney + _coffeeMoney;    result += drinkMoney ;    return result;}

As we can see, after the reconstruction of the variables, the function is no longer confused about the money before the temporary variables, because the variable name itself makes the logic clearer.

If your code is still complicated here, you can use other refactoring methods as much as you like.

Summary

The focus of this refactoring method is that temporary variables are not used for loop variables and result collection, but are assigned more than twice, so that they are decomposed to assume only one responsibility at a time.

7 Remove Assignments to Parameters (Remove the value assigned to the parameter) Summary

The Code assigns a value to a parameter.

Replace this parameter with a temporary variable.

Motivation

First, we need to clarify the meaning of "assigning values to parameters" here. If you pass an object named foo as a parameter to a function, "assign a value to the parameter" means to change foo so that it can reference another object. If you perform any operation on the "passed object", it is okay. Java only uses the value-based transmission method, while C # is divided into value transfer and reference transfer. For the value transfer and reference transfer of C #, please read another article or Baidu.

int test(int a){    if (a > 50)    {        a = 1;    }    return a;}

This violates this principle, because the re-assignment of input parameters will lead to ambiguity in the code reader and reduce the Definition of the code. They don't even understand what your parameter represents, or even worry about the stability of your parameter.

When the value type is passed by value, any modification to the parameter will not affect the call end. This refactoring method is also used to transmit values by value.

Example
int GetDiscount(int inputVal, int quantity, int yearToDate){     if (inputVal > 50)     {         inputVal -= 2;     }     if (quantity > 100)     {         inputVal -= 1;     }     if (yearToDate > 1000)     {         inputVal -= 4;     }     return inputVal;}

Use temporary variables to replace the value assignment to parameters. The following code is obtained:

int GetDiscount(int inputVal, int quantity, int yearToDate){     int result=inputVal;     if (inputVal > 50)     {         result-= 2;     }     if (quantity > 100)     {         result-= 1;     }     if (yearToDate > 1000)     {         result-= 4;     }     return result;}
Summary

If the parameter only indicates "passed in", the code will be clear.

 

To Be Continued...

Related Article

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.