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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
1
00:00:00,070 --> 00:00:00,860
We're now going to talk about
2
00:00:00,860 --> 00:00:03,390
the consolidate conditional expression refactoring.
3
00:00:03,390 --> 00:00:05,730
A common situation in code is that you have a set
4
00:00:05,730 --> 00:00:08,530
of conditionals with the same result. What that means that
5
00:00:08,530 --> 00:00:12,090
sometimes the code contains a series of conditional checks in which
6
00:00:12,090 --> 00:00:14,760
each check is different, yet the resulting action is the
7
00:00:14,760 --> 00:00:18,440
same. In these cases, the code could be improved by combining
8
00:00:18,440 --> 00:00:22,100
the conditionals using, for example, and, and or, as connectors. So
9
00:00:22,100 --> 00:00:25,390
as to have a single conditional check, with a single result.
10
00:00:25,390 --> 00:00:28,500
At that point you can also extract those conditional into a
11
00:00:28,500 --> 00:00:32,310
method. And replace the conditional with a call, to debt matter consolidating
12
00:00:32,310 --> 00:00:35,110
the conditional code in this way can make the checks clearer
13
00:00:35,110 --> 00:00:38,170
by showing that you're really making a single check rather than multiple
14
00:00:38,170 --> 00:00:41,150
checks, and extracted that condition and having that matter instead of
15
00:00:41,150 --> 00:00:44,550
a condition can clarify your code by explaining why you're doing a
16
00:00:44,550 --> 00:00:47,350
given check, rather than how you're doing it. You can see an
17
00:00:47,350 --> 00:00:52,020
example of that situation in this code, which is the disabilityAmount method.
18
00:00:52,020 --> 00:00:54,580
As the name of the method says, the purpose of this code
19
00:00:54,580 --> 00:00:58,300
is to compute the disability amount for a given, for example, employee.
20
00:00:58,300 --> 00:01:01,250
And there is a set of initial checks in the methods whose
21
00:01:01,250 --> 00:01:05,090
goal is to decide whether there's this disabilityAmount should be instead zero.
22
00:01:05,090 --> 00:01:07,750
And as you can see, there's multiple conditions. For example, there's a
23
00:01:07,750 --> 00:01:10,630
check about the seniority level, and about the number of months that
24
00:01:10,630 --> 00:01:14,350
the employee's been disabled. So far, whether the employee is part time
25
00:01:14,350 --> 00:01:17,060
and the outcome of all these check is always the same. If they're
26
00:01:17,060 --> 00:01:19,740
true, if the check is satisfied then there is no disability
27
00:01:19,740 --> 00:01:22,564
amount. So the disabilityAmount is zero. So what I will do
28
00:01:22,564 --> 00:01:25,986
if I apply the consolidate conditional expression to this matter, is
29
00:01:25,986 --> 00:01:29,690
that I will take these three conditionals. I will put them together
30
00:01:29,690 --> 00:01:32,772
by saying basically that if seniority is less than 2 or
31
00:01:32,772 --> 00:01:36,524
monthsDisabled is greater than 12 or isPartTime is true then the
32
00:01:36,524 --> 00:01:40,170
return should be zero. And once I have this combined conditional,
33
00:01:40,170 --> 00:01:42,601
as I see here, I will just extract that into a method.
|