Step by step. NET code refactoring study note 3. Inline Method)

Source: Internet
Author: User

I. Inline Method

Overview

The method body of a function should be as clear and understandable as its name.

Motivation)

Express the action intention with a short function, which makes the code clearer and easier to read. but sometimes you may encounter some functions. its internal code and function names are as clear and easy to read. you may have reconstructed the function to make its content as clear as its name. if this is the case, you should remove this function and directly use the code in it, which may indirectly help, but the non-necessary indirect nature is always uncomfortable.

Mechanism)

1. Check the function to make sure it does not have polymorphism (is not ploymorphic)

If subclass inherits this function, do not convert it into line. Because subclass cannot overwrite (override) a function that does not exist at all.

2. Find all called points of the function.

3. replace all called points of the function with the function Ontology (CODE ).

4. Delete the definition of the function.

Example

View sourceprint? 01 public class Inline

02 {

03 public string GetUserInfo (int Age)

04 {

05 return MoreThenTen (Age )? "Spring yang": null;

06}

07 public bool MoreThenTen (int Age)

08 {

09 return Age> 10;

10}

11}

 

Changed:

View sourceprint? 1 public class Inline

2 {

3 public string GetUserInfo (int Age)

4 {

5 return Age> 10? "Spring yang": null;

6}

7}

 

Ii. Inline Temp

Overview

A temporary variable is assigned only once by a simple expression, which hinders other refactoring methods.

Motivation)

Inline Temp is mostly used as part of Replace Temp with Query. The only situation where Inline Temp is used independently is that you find that a temporary variable is assigned the return value of a function call. Generally, such a temporary variable does not cause any harm. You can leave it there with confidence. However, if this temporary variable hinders other refactoring methods, such as Extract Method, it should be made inline.

Mechanism)

1. If this temporary variable is not declared as final, declare it as final and compile it.

This checks whether the temporary variable is assigned only once.

2. Locate all reference points of the temporary variable and replace them with the expression on the right of the equals sign in the language name assigned to the temporary variable.

3. Compile and test each modification.

4. After modifying all the reference points, delete the declarative and value assignment languages of the temporary variable.

Example

View sourceprint? 1 public class Inline

2 {

3 public double GetUserSalary (string name)

4 {

5 double salary = UserSalary (name );

6 return salary * 5;

7}

8}

 

Changed:

View sourceprint? 1 public class Inline

2 {

3 public double GetUserSalary (string name)

4 {

5 return UserSalary (name) * 5;

6}

7}

 

Iii. Replace Temp with Query

Overview

The program uses a temporary variable (temp) to save the operation result of an expression. Extract this expression into an independent function (query. Replace all (referenced points) of this temporary variable with calls to the new function. New functions can be used by other functions.

Motivation)

The problem with temporary variables is that they are temporary and can only be used within the function. Temporary variables are visible only within the function, so they will drive you to write a longer function, because only friends can access the temporary variables you want to access. If you replace a temporary variable with a query method, all functions in the same class can obtain this information. This will greatly help you to write clearer code for this class.

Mechanism)

1. Find the temporary variable assigned only once.

If a Temporary Variable is assigned more than once, use Split Temporary Variable to Split it into multiple variables.

2. Declare the temporary variable as final.

3. compile.

This ensures that temporary variables are assigned only once.

4. Extract the right part of the equal sign of the language name assigned to the temporary variable to an independent function.

First, declare the function as private. In the future, you may find that more classes need to use it, so you can easily relax the protection for it.

Make sure that the extracted function has no joint impact, that is, the function does not modify any object content. If it has a joint effect, perform Separate Query from Modifier.

Example

View sourceprint? 1 public double GetPrice ()

2 {

3 int area = _ width * _ higth;

4 double discount;

5 if (area> 1000) discount = 0.9;

6 else discount = 1;

7 return area * discount;

8}

 

Changed:

View sourceprint? 01 public double GetPrice ()

02 {

03 return GetArea () * GetDiscount ();

04}

05

06 public double GetDiscount ()

07 {

08 double discount;

09 if (GetArea ()> 1000) discount = 0.9;

10 else discount = 1;

11 return discount;

12}

13 public int GetArea ()

14 {

15 return _ width * _ higth;

16}

 

Iv. Introduce Explaining Variable

Overview

Put the results of a complex expression into a temporary variable and use the variable name to explain the purpose of the expression.

Motivation)

Expressions may be complex and difficult to read. Temporary variables can help you break down expressions into easy-to-manage forms.

Mechanism)

1. Declare a final temporary variable and assign the calculation result of some actions in the complex expression to it.

2. Replace the calculation result in the expression with the above temporary variable.

If this part is repeated in the code, you can replace it one by one each time.

Example

View sourceprint? 1 double _ width, _ higth;

2 public double GetPrice ()

3 {

4 return _ width * _ higth-Math. Max (0, _ width-1000) * _ higth * 0.03 + Math. Min (_ width * _ higth * 0.1, 100 );

5}

 

Changed:

 

View sourceprint? 01 double _ width, _ higth;

02 public double GetPrice ()

03 {

04 return NormalPrice ()-QuantityDiscount () + Shipping ();

05}

06 public double NormalPrice ()

07 {

08 return _ width * _ higth ();

09}

10 public double QuantityDiscount ()

11 {

12 return Math. Max (0, _ width-1000) * _ higth * 0.03;

13}

14

15 public double Shipping ()

16 {

17 return Math. Min (NormalPrice () * 0.1, 100 );

18}

Summary

The complex expressions are divided into several methods that are clear and easy to read to make the program more readable.

 

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.