Introduce explanatory variables and explanatory variables
This article is in the study of the summary, welcome to reprint but please note the Source: http://blog.csdn.net/pistolove/article/details/42417535
In the previous article, we introduced "replacing temporary variables with queries". This article will introduce the reconstruction method of "Introducing explanatory variables.
Let's learn about This refactoring method.
Open door
We found that you have a complex expression.
Solution: Put the results of this complex expression (or part of it) into a temporary variable and use this variable name to explain the purpose of the expression.
// Pre-reconstruction if (platform. toUpperCase (). indexOf ("MAC")>-1) & (browser. toUpperCase (). indexOf ("IE")>-1) & wasInitialized () & resize> 0) {// do something}
// Final boolean isMacOs = platform after reconstruction. toUpperCase (). indexOf ("MAC")>-1; final boolean isIEBrowser = browser. toUpperCase (). indexOf ("IE")>-1; final boolean wasResize = resize> 0; if (isMacOs & isIEBrowser & wasInitialized () & wasResize) {// do something}
Motivation
In some cases, expressions may be so complex that they are hard to read. In this way, temporary variables can help you break down expressions into easy-to-manage forms.
In conditional logic, the introduction of explanatory variables is more valuable: You can use this refactoring to extract each clause and use a well-named temporary variable to explain the meaning of the corresponding conditional clause. Another possibility is that for long algorithms, temporary variables can be used to explain the meaning of each operation.
The refactoring method in this article is one of the most common methods, but its usage is not so much. Generally, we can use extract functions to explain the meaning of a piece of code. After all, a temporary variable is meaningful only in the function where it is located and has many limitations. A function can be useful throughout the entire lifecycle of an object and can be used by other objects. However, when it is difficult to use the extraction function for a local variable, you can try to use the introduced explanatory variable.
Method (1) declare a temporary final variable and assign the calculation result of some actions in the complex expression to it. (2) Replace the "operation result" part of the expression with the temporary variable. (If this part is repeated in the code, you can replace it one by one.) (3) Compile and test. (4) Repeat the above process to process other similar parts.
In this example, we start with a simple calculation:
// Double price () {// price = basePrice-quantity discount + shippingreturn _ quantity * _ itemPrice-Math. max (0, _ quantity-800) * _ itemPrice * 0.15 + Math. min (_ quantity * _ itemPrice * 0.25, 100 );}This code is still relatively simple, but it should be easier to understand now. First, we find that the base price is equal to the quantity multiplied by the unit price ). Therefore, you can put the calculation results in a temporary variable and replace the parameters in the Math. min () function with the same values.
Double price () {// price = basePrice-quantity discount + shippingfinal double basePrice = _ quantity * _ itemPrice; return basePrice-Math. max (0, _ quantity-800) * _ itemPrice * 0.15 + Math. min (basePrice x 0.25, 100 );}Then, extract the calculation of the wholesale discount (quantity discount) and assign the calculation result to the temporary variable.
Double price () {// price = basePrice-quantity discount + shippingfinal double basePrice = _ quantity * _ itemPrice; final double quantityDiscount = Math. max (0, _ quantity-800) * _ itemPrice * 0.15; return basePrice-quantityDiscount + Math. min (basePrice x 0.25, 100 );}Finally, extract the shipping calculation and assign the calculation result to the temporary variable.
// Double price () {// price = basePrice-quantity discount + shippingfinal double basePrice = _ quantity * _ itemPrice; final double quantityDiscount = Math. max (0, _ quantity-800) * _ itemPrice * 0.15; final double shipping = Math. min (basePrice * 0.25, 100); return basePrice-quantityDiscount + shipping ;}
The extract function is used to process the above Code. It generally prefers to use the extract function instead of interpreting its action intent with temporary variables. Let's start from scratch:
// Double price () {// price = basePrice-quantity discount + shippingreturn _ quantity * _ itemPrice-Math. max (0, _ quantity-800) * _ itemPrice * 0.15 + Math. min (_ quantity * _ itemPrice * 0.25, 100 );}Now we extract the base price calculation into an independent function.
Double price () {// price = basePrice-quantity discount + shippingreturn basePrice ()-Math. max (0, _ quantity-800) * _ itemPrice * 0.15 + Math. min (basePrice () * 0.25, 100);} private double basePrice () {return _ quantity * _ itemPrice ;}Continue to refine and extract a new function each time. The Code is as follows.
// Double price () {// price = basePrice-quantity discount + shippingreturn basePrice ()-quantityDiscount () + shipping ();} private double basePrice () {return _ quantity * _ itemPrice;} private double shipping () {return Math. min (basePrice () * 0.25, 100);} private double quantityDiscount () {return Math. max (0, _ quantity-800) * _ itemPrice * 0.15 ;}
This article mainly introduces the refactoring method-Introducing explanatory variables. This reconstruction method is mainly used when it takes more work to extract functions. For example, if you have an algorithm with a large number of local variables, it is not easy to use the extraction function. At this time, you can use the method described in this article to sort out the code and then consider what to do next. Once the code logic is clear, you can use a query to replace the temporary variables to remove the temporary variables introduced in the middle.
I think you would like to extract functions, because any part of the same object can use these extracted functions as needed. These new functions are declared as private at the beginning. If other objects need them, the access control of these functions can be easily released.
Finally, I hope this article will help you. If you have any questions, please leave a message. Thank you. (PS: the next article will introduce refactoring notes-breaking down temporary variables)
Rebuild note articles as follows
Rebuild notes-getting started
Rebuilding notes-bad taste of code (I)
Rebuilding notes-bad taste of Code (Part 2)
Rebuilding notes -- Building a test body
Rebuilding notes -- refining Functions
Rebuilding notes-inline functions
Refactoring notes-inline temporary variables
Rebuilding notes -- replacing temporary variables with queries