Code refactoring and unit testing — refactoring the statement () method again with “replacing temporary variables with queries” (7)

Time:2022-5-18
 
 

7、 Reconstruction4: useReplace temporary variables with queriesAgain rightStatement() methodRefactoring

1. After the refactoring of the previous articles, the specific code of the statement () method in customer is shown in the following figure. When calculating the amount and points of each movie, we call the corresponding method of the object of the rental class. The method in the figure below is much simpler and easier to understand and maintain than the method in the original code of charging item billing project.

  

2. However, the above code still has room for reconstruction. For example, if we want to organize the results in HTML, we need to copy the above code and modify the text organization of the result variable. This may be what programmers often do Ctrl + C and Ctrl + V. many programmers modify the code in this way. This kind of modified code is fast and easy to realize the function, but such modified code will leave a lot of useless code to our system. Using Ctrl + C and Ctrl + V to modify the code will cause many temporary variables in the method to be copied, which is exactly the same, so it is easy to produce duplicate code. In this case, we need to use the refactoring technique of “replace temp with query” to extract the temporary variables in the red box shown in the above figure.

For each temporary variable shown in the red box in the figure above, we will extract a query method. The code shown in the figure below is the reconstructed statement () method using the “replace temp with query” rule and the extracted two query functions.

   

 

3. After the refactoring of the above steps, the specific code of customer class is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 

namespace LeasePowerBank
{

    /// 
      ///Customer class
    /// 
   public  class Customer
    {

        string Name;// User name
    public    List listRentals=new List();// User leased power bank
        public Customer(string name)
        {

           Name = name;
        }

        public void AddRental(Rental rental)
        {

            listRentals.Add(rental);
        }

   
        public string Statement()
        {

           
            String result = $"{name}" lease Bill: \ n ";
 
            Result + = $"total amount: {gettotalamount()} \ n";
            Result + = $"you have obtained: {gettotalfrequentrenterpoints()} points this time";
            return result;

        }

        /// 
        ///Calculate total amount
        /// 
        private decimal GetTotalAmount()
        {

            decimal totalAmount = 0M; // Total amount
            foreach (var item in listRentals)
            {

                totalAmount+= item. GetAmount();// Total price calculation            

            }

            return totalAmount;

        }

        /// 
        ///Calculate integral
        /// 
        private decimal GetTotalFrequentRenterPoints()
        {
            int frequentRenterPoints = 0; // User points

            foreach (var item in listRentals)
            {
                frequentRenterPoints += item. GetFrequentRenterPoints();// Calculate integral
            }

            return frequentRenterPoints;
        }
    }
}
 
4. Our test cases remain unchanged. After each refactoring, we need to call the above test cases to check whether the refactoring has side effects. At present, the dependencies between our classes have not changed much, but the methods in the corresponding classes have changed. Find all test items on the Run menu bar of visual studio — >. Or select “run all tests in view” button in “test Explorer” to run test cases. Monitor whether the reconstruction results are correct. As shown below.