"Refactoring – Improving the design of existing code" reading notes----Replace Method with Method Object

Source: Internet
Author: User

Sometimes, when you encounter a large function, there are many temporary variables and parameters that make it impossible for you to extract method at all. Refactoring also strongly recommends the benefits of short functions, which bring about the explanatory, reusability of your benefits. But if you encounter a situation, you may be naïve to think that I just need to properly carry out the replace Temp with Query, you can resolve this phenomenon. But things tend to backfire and fail to reach your desired heights. This time you need to use the killer--replace method with method Object in the refactoring, which is from Kent Beck [Beck].

Simply put, this refactoring will change the parameters of all local variables within the source function into the field of method object. Then because it is a field, so there is no temporary variable this limit, you can easily extract the new object's new function of the original large function for easy disassembly.

Practice:

    • Create a new class that is a good name for this class, depending on the purpose of the function to be processed.
    • Create a const field in the new class to hold the object where the source function resides, and we call this function the source object . For each temporary variable and parameter of the original function, we add the corresponding field in the new class to save it.
    • Create a constructor in the new class where the parameters of the constructor are the original and the parameters of the original function (note: The parameters of the original function and the temporary variables need to be stored in the field, but the constructor requires only the parameters of the original function )
    • Add a compute () function to the new class
    • Copy the code of the original function into the compute () function of the new object, and then change the invocation of the original object in the original function to the field containing the original object in the new class.
    • Compile
    • Replace the function body of the old function with an object that creates the new class above, and then to the compute () function that uses it

Once this is done, you can make a drastic makeover of the compute () function in the new class.

Example:

class account... 

intGammaintInputval,intQuantityintyeartodate) { intImportantValue1 = (Inputval * quantity) +Delta (); intImportantValue2 = (Inputval * yeartodate) + -; if((yeartodate-importantvalue1) > -) ImportantValue2-= -; intImportantValue3 = ImportantValue2 *7; returnImportantValue3-2*importantValue1;}

This is our original function, and we can see that the parameters and the temporary variables are very many and very difficult to extract Method. So it's time to use our killer, first by "practice", we create a new class, and then change the function's arguments and temporary variables into his field.

class gamma{    private:        const Account *m_account;         int inputval;         int quantity;         int yeartodate;         int importantValue1;         int importantValue2;         int importantValue3;};

In order to guarantee the small step of the refactoring, here we do not rename the field variables, so there is where, when you move the implementation of the original function, you do not need to make any changes at the moment.

Next we make a declaration of the constructor, note: This time you just need to use the original function's object and the required parameters as a constructor parameter, do not need all the temporary variables.

     Public :         int int int yeartodatearg):            m_account (account),            inputval (inputvalarg),            quantity (Quantityarg),            YearToDate (Yeartodatearg)        {        }

Next, we declare the compute () function, and move the implementation of the original function, saying that the declaration is deleted, because it is already a field of the new class, and then replace the function call of the source object itself with a call to the field, the complete implementation of the following

classgamma{ Public: Gamma ( account*account,intInputvalarg,intQuantityarg,intyeartodatearg): M_account (account), Inputval (Inputvalarg), Quantity (Quantityarg), YearToDate (Yeartodatearg) {}intCompute () {importantValue1= (Inputval * quantity) + m_account->Delta (); ImportantValue2= (Inputval * yeartodate) + -; if((yeartodate-importantvalue1) > -) ImportantValue2-= -; ImportantValue3= ImportantValue2 *7; returnImportantValue3-2*importantValue1; }    Private:        ConstAccount *M_account; intInputval; intquantity; intyeartodate; intimportantValue1; intimportantValue2; intimportantValue3;};

Note that the delta () function, at this point, has become m_account->delta (), and the declaration of all variables has been deleted.

After finishing this step, we've actually finished this refactoring, but we're not done yet, and the reason we use the Replace method with method object is to make the extract method better, so next, We can easily refactor new classes of new functions, such as

     classGamma ...intCompute () {importantValue1= (Inputval * quantity) + m_account->Delta (); ImportantValue2= (Inputval * yeartodate) + -;            Importthing (); returnImportantValue3-2*importantValue1; }    Private:        voidimportthing () {if((yeartodate-importantvalue1) > -) ImportantValue2-= -; }

In this way, we can easily do the compute () function of the non-parametric extract Method, the entire implementation is more concise, do not need to worry about parameter transfer problems.

"Refactoring – Improving the design of existing code" reading notes----Replace Method with Method Object

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.