Refactoring notes-inline functions

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source: http://blog.csdn.net/pistolove/article/details/42261333


The refinement function is described in the previous article. This paper introduces the method of reconstructing the " inline function".

Let's learn the refactoring technique together.


straight to

Discovery: The body and the name of a function are equally legible.

WORKAROUND: Insert the function body at the function call point and remove the function.

Refactoring before public int getrating () {return (Morethansixlatedeliveries ())? 2:1;} Boolean morethansixlatedeliveries () {return _numberoflatedeliveries > 6;}
After refactoring public int getrating () {return (_numberoflatedeliveries > 6)? 2:1;}


Motive

The refactoring process often takes a short function to express the intent of the action, which makes the code clearer and easier to read. But sometimes you run into some functions whose internal code and function names are equally legible. You may have refactored the function so that its contents and its name become as clear as possible. If so, you should remove the function and use the code directly. Indirection may bring some help, but there is no need for indirection that is always uncomfortable.

Another situation is that you have a group of poorly organized functions on your hands. You can put them all inline into a large function, and then extract small, well-organized functions from them. It is easier to move the entire large function as a whole than to move a function and move all the other functions it calls.

If you find that too many layers of indirection are used in your code, so that all functions in the system seem to be simply delegates to another function, causing you to be disoriented by some of the delegate actions, then you usually use inline functions.


Practices(1) Check the function to determine that it is not polymorphic. (If the fruit class inherits this function, do not inline this function, because subclasses cannot replicate a function that does not exist at all). (2) Find out all the called points of this function. (3) Replace all the called points of this function with the function ontology. (4) compile, test. (5) When everything is OK, delete the definition of the function.

Inline functions may seem simple. But that's not always the case. For recursive calls, inline to another object, and the object does not provide an access function ... Every situation can be complicated. This is because if you encounter such a complex situation, you should not use this refactoring technique.



This paper mainly introduces the reconstruction technique--inline function, which is relatively simple to deal with, which is to eliminate a function and move the code into the place where the function is used. Although the method is relatively simple, but in the process of development will be often used to this method. I hope this article will be of some help to you. There are questions to leave a message, thank you. (PS: The next article will introduce refactoring notes--inline temporary variables)


Refactoring notes-inline functions

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.