Refactoring method 02: inline method (Inline Function)

Source: Internet
Author: User

The ontology and name of a function call are also clear and easy to understand.Insert the function body at the function call point and remove the function.

IntGetrating ()

{

ReturnMorethanfivelatedeliverise ()? 2: 1;

}

BoolMorethanfivelatedeliverise ()

{

Return_ Numberoflateliveries> 5;

}

 

 

 

int getrating ()

{

return _ numberoflateliveries> 5? 2: 1;

}

 

Motivation: Sometimes you may encounter some functions.CodeIt is as clear and easy to read as the function name. You may have reconstructed the function to make its content as clear as its name. If so, you should remove this function and use the Code directly. The indirect nature may help, but the non-necessary indirect nature is always uncomfortable.

Another method needs to be used.Inline Method(Inline Function) is: you have a group of unreasonable functions. You can inline them into a large function and then extract a reasonable small function. ImplementationReplace method with method object(Replace a function with a function object) previously, this can often achieve good results. You can inline all the function content of the called object of the function to the function object. Moving the entire large function as a whole is easier than moving a function and all other functions it calls.

If someone else uses too many indirect layers, so that all the functions in the system seem to be just a simple delegate to another function, resulting in confusion between these delegate actions, then useInline Method(Inline function ). Of course, the indirect layer has its value, but not all indirect layers have value. Try to use inline methods to find useful indirect layers and Remove useless indirect layers.

Practice:1Check the function to make sure it does not have polymorphism. If the subclass inherits this function, do not inline it, because the subclass overwrites a function that does not exist at all at lunch.

2Find all called points of this function.

3Replace all called points of the function with the function ontology.

4, Compilation, and testing.

5Delete the function definition.

inline method (Inline Function) seems simple. But this is often not the case. No access function is provided for a recursive call, multiple return points, or an inline join to another object. ...... you can write several pages in each case, this method should not be used.

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.