Reconstruction Method-Simplified conditional expression [2], reconstruction expression
Total returned directory
Directory of this section
- Merge lidate Duplicate Conditional Fragments (merge Duplicate condition Fragments)
- Remove Control Flag)
3. Merge lidate Duplicate Conditional Fragments (merge Duplicate condition Fragments) Summary
Each branch of the conditional expression has the same piece of code.
Move this duplicate code out of the conditional expression.
Motivation
If all branches of a set of conditional expressions execute the same code segment, move the code outside the conditional expression. In this way, we can more clearly show what changes with conditions and what remains unchanged.
Example
Assume that the following code is available:
class Deal{ public double Price { get; set; } private bool IsSpecialDeal() { //your code here return true; } private void Send() { //your code here } public double GetTotalPrice() { double total; if (IsSpecialDeal()) { total = Price * 0.95; Send(); } else { total = Price * 0.98; Send(); } return total; }}
Since both branches of the conditional expression execute the Send () function, move it to the periphery of the conditional expression:
class Deal{ public double Price { get; set; } private bool IsSpecialDeal() { //your code here return true; } private void Send() { //your code here } public double GetTotalPrice() { double total; if (IsSpecialDeal()) { total = Price * 0.95; } else { total = Price * 0.98; } Send(); return total; }}
This refactoring method can also avoid repeated code.
Summary
We also do this when dealing with exceptions. If both the try block and the catch block repeatedly execute the same piece of code, you can move it to the finally block.
4 Remove Control Flag (Remove Control Flag) Overview
In a series of boolean expressions, a variable carries a control flag.
Replace the control mark with a break or return statement.
Motivation
In a series of condition expressions, we often see the control mark used to determine when to stop the condition check:
Set done to false
While not done
If (condition)
Do something
Set done to true
Next step of loop
This control mark greatly reduces the readability of conditional expressions. Replacing the control mark with a break or return statement will bring great convenience.
Example: replace a simple control flag with a break
The following functions are used to check whether two suspicious characters are in a series of names:
class Person{ public void CheckSecurity(string[] people) { bool found = false; foreach (var person in people) { if (!found) { if (person == "Don") { SendAlert(); found = true; } if (person == "John") { SendAlert(); found = true; } } } } private void SendAlert() { }}
In this case, it is easy to find the control Tag: When the variable found is granted true, the search ends. In this way, we can introduce the break statement to replace the statement that assigns values to the found variable, and delete the reference of the control mark after the replacement is complete:
class Person{ public void CheckSecurity(string[] people) { foreach (var person in people) { if (person == "Don") { SendAlert(); break; } if (person == "John") { SendAlert(); break; } } } private void SendAlert() { }}
Example: return control flag
We will slightly change the above example:
class Person{ public void CheckSecurity(string[] people) { string found = string.Empty; foreach (var person in people) { if (found == string.Empty) { if (person == "Don") { SendAlert(); found = "Don"; } if (person == "John") { SendAlert(); found = "John"; } } } OtherMethod(found); } private void SendAlert() { } private void OtherMethod(string found) { }}
Here, the variable found does two things: Control Mark and calculation result. In this case, the code for calculating the found variable is first extracted into an independent function:
class Person{ public void CheckSecurity(string[] people) { string found = FoundMiscreant(people); OtherMethod(found); } private string FoundMiscreant(string[] people) { string found = string.Empty; foreach (var person in people) { if (person == "Don") { SendAlert(); found = "Don"; } if (person == "John") { SendAlert(); found = "John"; } } return found; } private void SendAlert() { } private void OtherMethod(string found) { }}
Then replace the control statement with the return statement, and completely remove the control mark:
class Person{ public void CheckSecurity(string[] people) { string found = FoundMiscreant(people); OtherMethod(found); } private string FoundMiscreant(string[] people) { foreach (var person in people) { if (person == "Don") { SendAlert(); return "Don"; } if (person == "John") { SendAlert(); return "John"; } } return string.Empty; } private void SendAlert() { } private void OtherMethod(string found) { }}
If the return value is void, you can replace the control mark with the return statement, but it is an empty return.
Summary
If this method is used to process functions with side effects, you must first separate the query function from the modification function.
To Be Continued ......