Reconstruction Method-Simplified function call [4], reconstruction method-Simplified Function
Total returned directory
Directory of this section
- Replace Parameter with Methods (Replace parameters with functions)
- Introduce Parameter Object (introducing Parameter objects)
- Remove Setting Method (Remove the Setting function)
8 Replace Parameter with Methods (Replace parameters with functions) Summary
The object calls a function and passes the result as a parameter to another function. The function that accepts parameters can also call the previous function.
Remove this parameter from the parameter receiver and directly call the previous function..
Motivation
If the function obtains the parameter value through other means, it should not obtain the value through the parameter. Too long parameter columns increase the reader's understanding difficulty. Therefore, we should try to shorten the length of parameter columns as much as possible.
One way to reduce the parameter column is to see if the parameter receiver can obtain the parameter value through the same calculation as the caller. If the caller calculates parameters through another function in the object to which the caller belongs, and "other parameters of the caller are not referenced" during the calculation ", you can transfer this calculation process to the called end to remove this parameter.
Example
Run the following code to calculate the order discount price.
class Price{ public int Quantity { get; set; } public int ItemPrice { get; set; } public double GetPrice() { int basePrice = Quantity * ItemPrice; int discountLevel = Quantity > 100 ? 2 : 1; double finalPrice = GetDiscountedPrice(basePrice, discountLevel); return finalPrice; } private double GetDiscountedPrice(int basePrice, int discountLevel) { if (discountLevel == 2) { return basePrice * 0.1; } return basePrice * 0.05; }}
First, extract the code for calculating the discount level into an independent function:
private int GetDiscountLevel(){ return Quantity > 100 ? 2 : 1;}
Next, replace all the reference points and Remove the parameters using Remove parameter:
class Price{ public int Quantity { get; set; } public int ItemPrice { get; set; } public double GetPrice() { int basePrice = Quantity * ItemPrice; double finalPrice = GetDiscountedPrice(basePrice); return finalPrice; } private int GetDiscountLevel() { return Quantity > 100 ? 2 : 1; } private double GetDiscountedPrice(int basePrice) { if (GetDiscountLevel() == 2) { return basePrice * 0.1; } return basePrice * 0.05; }}
Remove basePrice using the same method:
class Price{ public int Quantity { get; set; } public int ItemPrice { get; set; } public double GetPrice() { return GetDiscountedPrice(); } private int GetBasePrice() { return Quantity * ItemPrice; } private int GetDiscountLevel() { return Quantity > 100 ? 2 : 1; } private double GetDiscountedPrice() { if (GetDiscountLevel() == 2) { return GetBasePrice() * 0.1; } return GetBasePrice() * 0.05; }}
Finally, we can use the Inline Method for the GetDiscountedPrice () function:
class Price{ public int Quantity { get; set; } public int ItemPrice { get; set; } public double GetPrice() { if (GetDiscountLevel() == 2) { return GetBasePrice() * 0.1 } return GetBasePrice() * 0.05; } private int GetBasePrice() { return Quantity * ItemPrice; } private int GetDiscountLevel() { return Quantity > 100 ? 2 : 1; } }
Summary
Using This refactoring method can reduce the parameter columns, making it easier for the program to understand.
9 Introduce Parameter Object (introducing Parameter objects) Overview
Some parameters always appear at the same time.Replace these parameters with an object.
Motivation
A set of parameters may be used by several functions at the same time. These functions may belong to the same class or different classes. Such a set of parameters is the so-called Data Clump. We can use an object to wrap all the Data and replace them with objects. The value of this reconstruction is to shorten the parameter columns. In addition, the access functions defined by the new object can make the code more consistent, which further reduces the difficulty of understanding and modifying the code.
Example
/// <Summary> /// billing Item class /// </summary> class Entry {public DateTime ChargeDate {get; set;} public double Value {get; set ;} public Entry (double value, DateTime chargeDate) {Value = value; ChargeDate = chargeDate ;}} /// <summary> /// Account class /// </summary> class Account {private List <Entry> _ entries = new List <Entry> (); /// <summary> /// calculate the total number of billing items between two dates /// </summary> /// <param name = "start"> start date </param >/// <param name = "end"> end date </param> /// <returns> </returns> public double GetFlowBetween (DateTime start, dateTime end) {return _ entries. where (entry => entry. chargeDate> = start & entry. chargeDate <= end ). sum (entry => entry. value );}}
The following code may be used to call a client:
Account anAccount = new Account();anAccount.GetFlowBetween(DateTime.Now, DateTime.Now.AddMonths(1));
We will replace it with a "range object. First, create a simple data container in the Account class to indicate the range:
class DateRange{ public DateTime Start { get; } public DateTime End { get; } public DateRange(DateTime start, DateTime end) { Start = start; End = end; }}
Then modify the parameters of the GetFlowBetween () function:
/// <Summary> /// calculate the total number of items between two dates /// </summary> /// <param name = "range"> </param>/ // <returns> </returns> public double GetFlowBetween (DateRange range) {return _ entries. where (entry => entry. chargeDate> = range. start & entry. chargeDate <= range. end ). sum (entry => entry. value );}
The Code called by the client may become as follows:
Account anAccount = new Account();anAccount.GetFlowBetween(new DateRange(DateTime.Now, DateTime.Now.AddMonths(1)));
Summary
This reconstruction can also bring more benefits. After these parameters are organized together, you can quickly find that new classes can be transplanted. Normally, the functions that originally use those parameters have some common processing for this set of parameters. If these common behaviors are moved to the new object, repeated code can be reduced.
10 Remove Setting Method (Remove the set-value function) Summary
A field or attribute in the class should be set as a value when the object is created, and then it will not be changed.
Remove all set-value functions of this field or attribute.
Motivation
If you provide a value setting function for a field or attribute, this implies that the field or attribute value can be changed. If you do not want this field or attribute to change after the object is created, do not provide a value-setting function for it. In this way, your intention will be clearer and you can exclude the possibility of the value being modified.
Example
Let's look at a simple example:
class Account{ public string Id{ get; set; } public Account(string id) { this.Id ="ZZ"+id; }}
The above code can be modified:
class Account{ public string Id { get; } public Account(string id) { this.Id = "ZZ"+id; }}
To Be Continued ......