-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReplaceNestedConditionalWithGuardClauses.java
60 lines (56 loc) · 1.35 KB
/
ReplaceNestedConditionalWithGuardClauses.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package ch9;
/*
* A method has conditional behavior that does not make clear the normal path of execution
*
* Use guard clauses for all the special cases
*/
class ReplaceNestedConditionalWithGuardClauses {
public double getPayAmount() {
double result;
if (_isDead)
result = deadAmount();
else {
if (_isSeparated)
result = separatedAmount();
else {
if (_isRetired)
result = retiredAmount();
else
result = normalPayAmount();
}
}
return result;
}
}
class ReplaceNestedConditionalWithGuardClausesReversing {
public double getAdjustedCapital() {
double result = 0.0;
if (_capital > 0.0) {
if (_intRate > 0.0 && _duration > 0.0) {
result = (_income / _duration) * ADJ_FACTOR;
}
}
return result;
}
}
class ReplaceNestedConditionalWithGuardClausesRefactored {
public double getPayAmount() {
if (_isDead)
return deadAmount();
if (_isSeparated)
return separatedAmount();
if (_isRetired)
return retiredAmount();
return normalPayAmount();
;
}
}
class ReplaceNestedConditionalWithGuardClausesReversingRefactored {
public double getAdjustedCapital() {
if (_capital <= 0.0)
return 0.0;
if (_intRate <= 0.0 || _duration <= 0.0)
return 0.0;
return (_income / _duration) * ADJ_FACTOR;
}
}