Code refactoring and unit testing — refactoring of “extraction method” (3)

Time:2022-6-3
 
 

3、 Refactoring1Use the extract method refactoring on the code in the statement method

We observeCode refactoring and unit testing (1)In the articleIn the shared charging bank billing code of, it is found that the statement () method of the customer class needs to be reconstructed first. The biggest problem with the statement () method is that there are too many things in this method. Our first step is to split it. That is, use the “extract method” principle in refactoring to simplify and split the method. Extract the independent code in statement (). After careful analysis, we find that the code selected in the box below is a complete function module. The code in box 1 is used to calculate unit price, and the code in red box 2 is used to calculate integral. We can extract and package these two codes into a new method. When encapsulating a new method, you should give the new method an appropriate method name. See the meaning of the name.

   

1. let’s first extract the code in box 1. Select the code in box 1 in the code editor, right-click, and select quick operation and refactoring from the pop-up menu. See the following figure.

 

2. click “quick operation and refactoring” with the left mouse button, and the “extraction method” menu will appear in the code editor, and the preview code of our extraction method will appear. See the following figure.

3. we use the left mouse button to click “preview changes” in the above figure. As shown in the following figure, we will see the extraction method and the code changes. If there is no problem, click the “apply” button.

 

4. we have extracted this code into the new method. Change the “newmethod” method name to “getamount”. See the following figure.

 

5. the new method getamount() we extracted is as follows:

  public  decimal GetAmount(Rental item)
        {

            decimal amount = 0M;
            switch (item.Power.PriceCode)
            {

                case 0:
                    amount = item.RentedTime;
                    if (item.RentedTime > 12)
                    {
                        amount = 12;
                    }

                    break;

                case 1:
                    amount = item.RentedTime * 3;
                    if (item.RentedTime > 24)
                    {
                        amount = 24;
                    }

                    break;

                case 2:
                    amount = item.RentedTime * 5;
                    if (item.RentedTime > 50)
                    {

                        amount = 50;
                    }
                    break;
                default:
                    break;

            }
            return amount;
        }

 

      6. Repeat steps 1 to 4 above to extract the code in red box 2. When extracting, take the data that depends on the statement () method as the parameter of the new method. The encapsulated method is as follows. You can call the integral calculation method below at the corresponding place in the statement () method. The following is our encapsulated method for calculating integrals. The codes are as follows:

public  int GetFrequentRenterPoints(int frequentRenterPoints, Rental item, decimal amount)
        {
            //Calculate integral
            if (item.Power.PriceCode == PowerBank.HighTraffic && item.RentedTime > 4)

            {

                frequentRenterPoints += (int)Math.Ceiling(amount * 1.5M);
            }

            else
                frequentRenterPoints += (int)Math.Ceiling(amount);
            return frequentRenterPoints;
        }

 

 

 

Recommended Today

redis day2

persistence mechanism client redis[memory] —–> memory data-data persistence–>disk Redis officially provides two different persistence methods to store data in memory to the hard disk: Snapshot AOF (Append Only File) only appends log files   Snapshot Features:In this way, all data at a certain moment can be written to the hard disk, of course, this is […]