Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.
1 the motive
Sometimes you find a string of condition checks where the conditions are different but the final behavior is the same.
It is time to combine them into a conditional expression using logical or and logical and:
- The merged condition code says, “There is really only one condition check, but there are multiple parallel conditions to check, making the purpose of this check clearer. Of course, the pre-merge code and the post-merge code have the same effect, but the message from the original code is “There are separate conditional tests, they just happen to happen at the same time.”
- This refactoring often sets the stage for the use of refining functions. Distilling the check condition into a separate function is very useful for clarifying the meaning of the code because it replaces the “what” statement with the “why” statement.
The reason to merge conditional statements also points out the reason not to merge: I wouldn’t use this refactoring if I thought that these checks were really independent of each other and should not be considered the same check.
2 practice
Make sure that none of these conditional expressions have side effects.
If a conditional expression has side effects, you can first use the separation of query function and modify function to deal with.
Combine two related conditional expressions into one, using the appropriate logical operators. Sequential conditional expressions are merged with logical or, and nested if statements are merged with logical and.
The test.
Repeat the previous merge process until all related conditional expressions are merged.
Consider implementing a refining function on the merged conditional expression.
3 cases
Case 1: Logic or
public int disabilityAmount(Employee anEmployee) {
if (anEmployee.seniority < 2) {
return 0;
}
if (anEmployee.monthsDisabled > 12) {
return 0;
}
if (anEmployee.isPartTime) {
return 0; // compute the disability amount}}Copy the code
There’s a bunch of conditional checks, all pointing to the same result. Since the results are the same, you should combine these condition checks into a single expression. Conditions executed in this order can be checked by logical or operational union.
public int disabilityAmount(Employee anEmployee) {
if ((anEmployee.seniority < 2) || (anEmployee.monthsDisabled > 12)) {
return 0;
}
if (anEmployee.isPartTime) {
return 0;
}
return 1;
}
Copy the code
Then incorporate the next condition check:
public int disabilityAmount(Employee anEmployee) {
if ((anEmployee.seniority < 2) || (anEmployee.monthsDisabled > 12) || (anEmployee.isPartTime)) {
return 0;
}
return 1;
}
Copy the code
After the merge is complete, use the extract function on this conditional expression.
public int disabilityAmount(Employee anEmployee) {
if (isNotEligableForDisability(anEmployee)) {
return 0;
}
return 1;
}
public boolean isNotEligableForDisability(Employee anEmployee) {
return ((anEmployee.seniority < 2) || (anEmployee.monthsDisabled > 12) || (anEmployee.isPartTime));
}
Copy the code
Case two: Logic and
For example, if statements are nested:
public double disabilityAmount(Employee anEmployee) {
if (anEmployee.onVacation) {
if (anEmployee.seniority > 10) {
return 1; }}return 0.5;
}
Copy the code
They can be combined with logic and operators:
public double disabilityAmount(Employee anEmployee) {
if ((anEmployee.onVacation) && (anEmployee.seniority > 10)) {
return 1;
}
return 0.5;
}
Copy the code
If the original conditional logic mixed both cases, I would also use a combination of logic and and logic or operators as needed. At this point, the code is likely to get messy, so I use refining functions frequently to make the code readable.