You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ℹ️ _This repository is part of my Refactoring catalog based on Fowler's book with the same title. Please see [kaiosilveira/refactoring](https://github.com/kaiosilveira/refactoring) for more details._
**Inverse of: [Replace Command with Function](https://github.com/kaiosilveira/replace-command-with-function-refactoring)**
60
54
61
-
**Refactoring introduction and motivation** dolore sunt deserunt proident enim excepteur et cillum duis velit dolor. Aute proident laborum officia velit culpa enim occaecat officia sunt aute labore id anim minim. Eu minim esse eiusmod enim nulla Lorem. Enim velit in minim anim anim ad duis aute ipsum voluptate do nulla. Ad tempor sint dolore et ullamco aute nulla irure sunt commodo nulla aliquip.
55
+
Nested functions are a useful tool in most cases but, sometimes, we need extra control over variables and readability. That's when this refactoring comes in handy.
62
56
63
57
## Working example
64
58
65
-
**Working example general explanation** proident reprehenderit mollit non voluptate ea aliquip ad ipsum anim veniam non nostrud. Cupidatat labore occaecat labore veniam incididunt pariatur elit officia. Aute nisi in nulla non dolor ullamco ut dolore do irure sit nulla incididunt enim. Cupidatat aliquip minim culpa enim. Fugiat occaecat qui nostrud nostrud eu exercitation Lorem pariatur fugiat ea consectetur pariatur irure. Officia dolore veniam duis duis eu eiusmod cupidatat laboris duis ad proident adipisicing. Minim veniam consectetur ut deserunt fugiat id incididunt reprehenderit.
59
+
Our working example consists of a program that calculates a health score for an insurance company. It goes like this:
if (scoringGuide.stateWithLowCertification(candidate.originState)) {
72
+
certificationGrade ='low';
73
+
result -=5;
74
+
}
75
+
// lots more code like this
76
+
result -=Math.max(healthLevel -5, 0);
77
+
return result;
78
+
}
79
+
```
80
+
81
+
Our goal here is to make the `score` function into a [command](https://github.com/kaiosilveira/design-patterns/tree/main/command), so we can isolate the processing into chunks, making the function more legible and probably more testable as well.
66
82
67
83
### Test suite
68
84
69
-
Occaecat et incididunt aliquip ex id dolore. Et excepteur et ea aute culpa fugiat consectetur veniam aliqua. Adipisicing amet reprehenderit elit qui.
85
+
The test suite covers all possible bifurcations of the aforementioned function, and it is somewhat extensive. To see the full implementation, please refer to [src/index.test.js](./src/index.test.js).
70
86
71
-
```javascript
72
-
describe('functionBeingRefactored', () => {
73
-
it('should work', () => {
74
-
expect(0).toEqual(1);
75
-
});
76
-
});
87
+
### Steps
88
+
89
+
We start by introducing a `Scorer` class:
90
+
91
+
```diff
92
+
+export class Scorer {
93
+
+ execute(candidate, medicalExam, scoringGuide) {
94
+
+ let result = 0;
95
+
+ let healthLevel = 0;
96
+
+ let highMedicalRiskFlag = false;
97
+
+
98
+
+ if (medicalExam.isSmoker) {
99
+
+ healthLevel += 10;
100
+
+ highMedicalRiskFlag = true;
101
+
+ }
102
+
+
103
+
+ let certificationGrade = 'regular';
104
+
+ if (scoringGuide.stateWithLowCertification(candidate.originState)) {
105
+
+ certificationGrade = 'low';
106
+
+ result -= 5;
107
+
+ }
108
+
+
109
+
+ // lots more code like this
110
+
+ result -= Math.max(healthLevel - 5, 0);
111
+
+
112
+
+ return result;
113
+
+ }
114
+
+}
77
115
```
78
116
79
-
Magna ut tempor et ut elit culpa id minim Lorem aliqua laboris aliqua dolor. Irure mollit ad in et enim consequat cillum voluptate et amet esse. Fugiat incididunt ea nulla cupidatat magna enim adipisicing consequat aliquip commodo elit et. Mollit aute irure consequat sunt. Dolor consequat elit voluptate aute duis qui eu do veniam laborum elit quis.
117
+
Then, we [inline](https://github.com/kaiosilveira/inline-function-refactoring)`Scorer.execute` at the body of `score`:
80
118
81
-
### Steps
119
+
```diff
120
+
export function score(candidate, medicalExam, scoringGuide) {
121
+
- let result = 0;
122
+
- let healthLevel = 0;
123
+
- let highMedicalRiskFlag = false;
124
+
-
125
+
- if (medicalExam.isSmoker) {
126
+
- healthLevel += 10;
127
+
- highMedicalRiskFlag = true;
128
+
- }
129
+
-
130
+
- let certificationGrade = 'regular';
131
+
- if (scoringGuide.stateWithLowCertification(candidate.originState)) {
132
+
- certificationGrade = 'low';
133
+
- result -= 5;
134
+
- }
135
+
-
136
+
- // lots more code like this
137
+
- result -= Math.max(healthLevel - 5, 0);
138
+
-
139
+
- return result;
140
+
+ return new Scorer().execute(candidate, medicalExam, scoringGuide);
141
+
}
142
+
```
143
+
144
+
Then, since the command's only raison d'être is to execute the scoring logic, it's somewhat more semantic to have the function's arguments as part of its constructor. We start by moving `candidate`:
145
+
146
+
```diff
147
+
+++ b/src/index.js
148
+
@@ -1,9 +1,13 @@
149
+
export function score(candidate, medicalExam, scoringGuide) {
150
+
- return new Scorer().execute(candidate, medicalExam, scoringGuide);
151
+
+ return new Scorer(candidate).execute(medicalExam, scoringGuide);
152
+
}
153
+
154
+
export class Scorer {
155
+
- execute(candidate, medicalExam, scoringGuide) {
156
+
+ constructor(candidate) {
157
+
+ this._candidate = candidate;
158
+
+ }
159
+
+
160
+
+ execute(medicalExam, scoringGuide) {
161
+
let result = 0;
162
+
let healthLevel = 0;
163
+
let highMedicalRiskFlag = false;
164
+
let certificationGrade = 'regular';
165
+
- if (scoringGuide.stateWithLowCertification(candidate.originState)) {
166
+
+ if (scoringGuide.stateWithLowCertification(this._candidate.originState)) {
167
+
certificationGrade = 'low';
168
+
result -= 5;
169
+
}
170
+
```
171
+
172
+
Then, we do the same for `medicalExam`:
173
+
174
+
```diff
175
+
export function score(candidate, medicalExam, scoringGuide) {
176
+
- return new Scorer(candidate).execute(medicalExam, scoringGuide);
177
+
+ return new Scorer(candidate, medicalExam).execute(scoringGuide);
178
+
}
179
+
export class Scorer {
180
+
- constructor(candidate) {
181
+
+ constructor(candidate, medicalExam) {
182
+
this._candidate = candidate;
183
+
+ this._medicalExam = medicalExam;
184
+
}
185
+
- execute(medicalExam, scoringGuide) {
186
+
+ execute(scoringGuide) {
187
+
let result = 0;
188
+
let healthLevel = 0;
189
+
let highMedicalRiskFlag = false;
190
+
- if (medicalExam.isSmoker) {
191
+
+ if (this._medicalExam.isSmoker) {
192
+
healthLevel += 10;
193
+
highMedicalRiskFlag = true;
194
+
}
195
+
```
196
+
197
+
And the last one is `scoringGuide`:
198
+
199
+
```diff
200
+
export function score(candidate, medicalExam, scoringGuide) {
201
+
- return new Scorer(candidate, medicalExam).execute(scoringGuide);
202
+
+ return new Scorer(candidate, medicalExam, scoringGuide).execute();
- if (scoringGuide.stateWithLowCertification(this._candidate.originState)) {
218
+
+ if (this._scoringGuide.stateWithLowCertification(this._candidate.originState)) {
219
+
certificationGrade = 'low';
220
+
result -= 5;
221
+
}
222
+
```
223
+
224
+
Now, on to the inner refactorings. Since our goal is to break the processing down into smaller chunks, we need to make the internal variables widely accessible, and we can accomplish this by turning them into class members. We start with `result`:
225
+
226
+
```diff
227
+
execute() {
228
+
- let result = 0;
229
+
+ this._result = 0;
230
+
let healthLevel = 0;
231
+
let highMedicalRiskFlag = false;
232
+
let certificationGrade = 'regular';
233
+
if (this._scoringGuide.stateWithLowCertification(this._candidate.originState)) {
**Step 1 description** mollit eu nulla mollit irure sint proident sint ipsum deserunt ad consectetur laborum incididunt aliqua. Officia occaecat deserunt in aute veniam sunt ad fugiat culpa sunt velit nulla. Pariatur anim sit minim sit duis mollit.
if (this._scoringGuide.stateWithLowCertification(this._candidate.originState)) {
289
+
- certificationGrade = 'low';
290
+
+ this._certificationGrade = 'low';
291
+
this._result -= 5;
292
+
}
90
293
```
91
294
92
-
**Step n description** mollit eu nulla mollit irure sint proident sint ipsum deserunt ad consectetur laborum incididunt aliqua. Officia occaecat deserunt in aute veniam sunt ad fugiat culpa sunt velit nulla. Pariatur anim sit minim sit duis mollit.
295
+
Now we're able to start the chunking. Our example is `scoreSmoking`:
|[3310680](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commit/3310680db031412cf0b87160b335976ea32b48db)| introduce `Scorer` class |
324
+
|[c982d35](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commit/c982d35f07364977eb0704b2679f721f191f2796)| call `Scorer.execute` at the body of `score`|
325
+
|[f8408b4](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commit/f8408b4690f690e568e5bfdc5ceafed9bab0e78e)| move `candidate` argument to `Score`'s constructor |
326
+
|[091533c](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commit/091533c15dc03a53c98bf1434cb1a4ee84b3fce4)| move `medicalExam` to `Score`'s constructor |
327
+
|[20b234e](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commit/20b234e39640ed5469eab550e329e783f635d2e2)| move `scoringGuide` to `Score`'s constructor |
328
+
|[10aa4f7](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commit/10aa4f7792523199c33d9aea33d8d991b722564e)| make `result` an instance variable at `Score`|
329
+
|[eda5940](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commit/eda594099ce1b4e4687189f3289822929e2a27c2)| make `healthLevel` an instance variable at `Score`|
330
+
|[d172cde](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commit/d172cde23d41c832739bfbe645d0c87e5b9a62f5)| make `highMedicalRiskFlag` an instance variable at `Score`|
331
+
|[b979164](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commit/b979164d07f04ae53012b3ca1157cc78c189c9c1)| make `certificationGrade` an instance variable at `Score`|
332
+
|[28a43a9](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commit/28a43a9654631562873fce636094c1b0d1f485a3)| extract `scoreSmoking` function at `Score`|
112
333
113
334
For the full commit history for this project, check the [Commit History tab](https://github.com/kaiosilveira/replace-function-with-command-refactoring/commits/main).
0 commit comments