-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintro.js
682 lines (523 loc) · 13.5 KB
/
intro.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/**
*
* REFACTORING
*
*
*
* twitter: @jonskulski
* github: github.com/jskulski
*/
/**
* WHAT TO EXPECT:
*
* Planting a seed that REFACTORING is very a precise way of
* working.
*
* The general idea:
*
* "restructuring code confidently to make it easier to do what
* you want to do"
*/
/**
*
* NOT EXPECTED:
*
* 1. Change anything about the way you are working+learning
*
* 2. To understand fully or recite any of this.
*
*/
/***
* WHY:
*
* - Probably #1 most important discipline I've learned.
* - Gave me great confidence in tackling the worst of code bases.
*
*
* Any developer can work with good code.
* Any good developer can work with `bad` code.
* Great developers make bad code into good code, then work with it.
*
*/
/**
*
* WARNING
*
* The word `Refactoring` is used a lot. Often incorrectly.
*
* - must ensure no external behavior changes.
* - if you can't deploy a refactor, it's not a refactor.
* - not a synonym for Rewrite!
*
* - "Refactoring without test is just changing [stuff]"
*
*/
/**
* PROLOGUE
*/
/**
* You wake up to a new task at work. You have a register system
* that sends you integers that correspond with keys pressed.
*
* Your system generates strings to send to the kitchen to
* prepare.
*/
function takeOrder(drink, temperature) {
//...
}
// assert(takeOrder(0, 0) == 'CUSTOMER NEEDS A HOT COFFEE');
// assert(takeOrder(1, 1) == 'CUSTOMER NEEDS A COLD TEA');
// assert(takeOrder(2, 1) == 'CUSTOMER NEEDS A COLD MILK');
// ... and many more!
/**
* You look at your task, it's asking to you add a third option.
*
* People want sugar and cream with their drinks.
*
*
* So you'll be doing something like this:
*/
function takeOrder(drink, temperature, options) {
//...
}
/**
* And generating this:
*/
// assert(takeOrder(0, 0, 1) == 'CUSTOMER NEEDS CREAM AND A HOT COFFEE');
// assert(takeOrder(1, 1, 0) == 'CUSTOMER NEEDS A COLD TEA');
// assert(takeOrder(2, 1, 2) == 'CUSTOMER NEEDS SUGAR AND A COLD MILK');
// ... and more!
/**
* You crack your knuckles and open up the code...
*
* It's not great. It's not terrible.
*
* But it's not going to be easy for you to get what you need
* to do, done.
*/
var takeOrder = function(drink, temperature) {
var order;
if (drink == 0 && temperature == 0) {
order = 'CUSTOMER NEEDS A HOT COFFEE';
}
else if (drink == 0 && temperature == 1) {
order = 'CUSTOMER NEEDS A COLD COFFEE';
}
else if (drink == 1 && temperature == 0) {
order = 'CUSTOMER NEEDS A HOT TEA';
}
else if (drink == 1 && temperature == 1) {
order = 'CUSTOMER NEEDS A COLD TEA';
}
else if (drink == 2 && temperature == 0) {
order = 'CUSTOMER NEEDS A HOT MILK';
}
else if (drink == 2 && temperature == 1) {
order = 'CUSTOMER NEEDS A COLD MILK';
}
else {
throw "UNSUPPORTED ORDER"
}
return order;
}
assert(takeOrder(0, 0) == 'CUSTOMER NEEDS A HOT COFFEE');
assert(takeOrder(1, 1) == 'CUSTOMER NEEDS A COLD TEA');
assert(takeOrder(2, 1) == 'CUSTOMER NEEDS A COLD MILK');
/**
* You start to think.
*
*
* And then you start to sweat because if you just follow this pattern,
* the conditionals explode and you will have this:
*
* (And think of the next developer who has to add another drink type!)
*/
var takeOrder = function(drink, temperature, options) {
if (drink == 0 && temperature == 0 && options == 0) {
order = 'CUSTOMER NEEDS A HOT COFFEE'
}
else if (drink == 0 && temperature == 0 && options == 1) {
order = 'CUSTOMER NEEDS CREAM AND A HOT COFFEE'
}
if (drink == 0 && temperature == 0 && options == 2) {
order = 'CUSTOMER NEEDS SUGAR AND A HOT COFFEE'
}
else if (drink == 0 && temperature == 1 && options == 0) {
order = 'CUSTOMER NEEDS A COLD COFFEE'
}
else if (drink == 0 && temperature == 1 && options == 1) {
order = 'CUSTOMER NEEDS CREAM AND A COLD COFFEE'
}
else if (drink == 0 && temperature == 1 && options == 2) {
order = 'CUSTOMER NEEDS SUGAR AND A COLD COFFEE'
}
if (drink == 1 && temperature == 0 && options == 0) {
order = 'CUSTOMER NEEDS A HOT TEA'
}
else if (drink == 1 && temperature == 0 && options == 1) {
order = 'CUSTOMER NEEDS CREAM AND A HOT TEA'
}
if (drink == 1 && temperature == 0 && options == 2) {
order = 'CUSTOMER NEEDS SUGAR AND A HOT TEA'
}
else if (drink == 1 && temperature == 1 && options == 0) {
order = 'CUSTOMER NEEDS A COLD TEA'
}
else if (drink == 1 && temperature == 1 && options == 1) {
order = 'CUSTOMER NEEDS CREAM AND A COLD TEA'
}
else if (drink == 1 && temperature == 1 && options == 2) {
order = 'CUSTOMER NEEDS SUGAR AND A COLD TEA'
}
if (drink == 2 && temperature == 0 && options == 0) {
order = 'CUSTOMER NEEDS A HOT MILK'
}
else if (drink == 2 && temperature == 0 && options == 1) {
order = 'CUSTOMER NEEDS CREAM AND A HOT MILK'
}
if (drink == 2 && temperature == 0 && options == 2) {
order = 'CUSTOMER NEEDS SUGAR AND A HOT MILK'
}
else if (drink == 2 && temperature == 1 && options == 0) {
order = 'CUSTOMER NEEDS A COLD MILK'
}
else if (drink == 2 && temperature == 1 && options == 1) {
order = 'CUSTOMER NEEDS CREAM AND A COLD MILK'
}
else if (drink == 2 && temperature == 1 && options == 2) {
order = 'CUSTOMER NEEDS SUGAR AND A COLD MILK'
}
return order;
}
assert(takeOrder(0, 0, 1) == 'CUSTOMER NEEDS CREAM AND A HOT COFFEE');
assert(takeOrder(1, 1, 0) == 'CUSTOMER NEEDS A COLD TEA');
assert(takeOrder(2, 1, 2) == 'CUSTOMER NEEDS SUGAR AND A COLD MILK');
/***
*
*
* SO.... what do you do?
*
*
*
* Let's come back.
*
*
*/
/**
* MATH
*
* We know from MATH:
*
* y = (x - 1)^2
* y = (x - 1)(x - 1)
* y = x^2 - 2x + 1
*
*/
function f1(x) { return Math.pow(x - 1, 2); }
function f2(x) { return (x - 1) * (x - 1); }
function f3(x) { return Math.pow(x, 2) -2*x + 1; }
/**
*
* All with various qualities:
* f2 is more readable to me,
* f3 is the slowest,
*
* But since for ANY value of x we are pretty much guaranteed:
*
* f1(x) == f2(x) == f3(x)
*
* We can swap out the implementation.
*
* So we could have a test suite that looked something like:
*/
function f(x) {
// ANY OF THOSE IMPLEMENTATIONS
return f1(x) === f2(x) && f2(x) === f3(x) ? f1(x) : false
}
assert(f(2) == 1);
assert(f(-1) == 4);
assert(f(0) == 1);
//... and so on.
/**
* APPLY THIS TO PROGRAMS
*
* If our external behavior doesn't change,
* we can change choose the implementation that's easiest for us to work with.
*
* Given these functions and the guarantee that they're the same,
* we can choose the one that is easiest to work with.
*/
function easyToWorkWith() {
// ...
}
function hardToWorkWith() {
// ...
}
// assert(easyToWorkWith(x) == hardToWorkWith(x)); // For any x
/***
*
* And that's what REFACTORING is:
*
* 1. Identifying easier to work with structures
* 2. Guaranteeing no change in external behavior (through Tests)
* 3. Changing code to be easy to work with and
* then doing what you need to do.
*
*/
/**
* DEFINITIONS:
*
* "Code refactoring is the process of restructuring existing
* computer code without changing its external behavior."
* - Wikipedia and Boring Books
*
*
* "[Refactoring is] applying a series of small behavior-preserving
* transformations, each of which 'too small to be worth doing'"
* - MARTIN FOWLER
*
* The main point:
* - change the internal
* - preserve the external
*
* - do it for a purpose, to get something DONE.
*/
/**
* Let's return to our example.
*
* You think there must be an easier way to implement your options.
*/
var takeOrder = function(drink, temperature, options) {
//...
}
/**
* You go and take a walk around a lake and muse and think and when you come back
* you're rested and you realize that there's some duplication in the string.
*
* The first thing you'll want to do is create a nice test suite so
* you can guarantee there are no EXTERNAL CHANGES.
*
* So you do the tedious work of cataloguing current behavior:
*/
// assert(takeOrder(0, 0) == 'CUSTOMER NEEDS A HOT COFFEE');
// assert(takeOrder(0, 1) == 'CUSTOMER NEEDS A COLD COFFEE');
// assert(takeOrder(1, 0) == 'CUSTOMER NEEDS A HOT TEA');
// assert(takeOrder(1, 1) == 'CUSTOMER NEEDS A COLD TEA');
//... and so on for all combinations
/**
* Now that you are CONFIDENT that you aren't changing anything, you can begin to work.
*
* The strings all start with 'CUSTOMER NEEDS A'
*
* Huh.
*
*
* Let's extract it.
*
* Note that this is a very small thing, hardly worth doing.
* But we're also pretty confident (even w/o tests) that we will
* still have a working system.
*
* THIS PART IS CRUX
*/
var takeOrder = function(drink, temperature) {
var order = 'CUSTOMER NEEDS A ';
if (drink == 0 && temperature == 0) {
order += 'HOT COFFEE';
}
else if (drink == 0 && temperature == 1) {
order += 'COLD COFFEE';
}
else if (drink == 1 && temperature == 0) {
order += 'HOT TEA';
}
else if (drink == 1 && temperature == 1) {
order += 'COLD TEA';
}
else if (drink == 1 && temperature == 0) {
order += 'HOT MILK';
}
else if (drink == 1 && temperature == 1) {
order += 'COLD MILK';
}
else {
throw "UNSUPPORTED ORDER"
}
return order;
}
assert(takeOrder(0, 0) == 'CUSTOMER NEEDS A HOT COFFEE');
assert(takeOrder(0, 1) == 'CUSTOMER NEEDS A COLD COFFEE');
assert(takeOrder(1, 0) == 'CUSTOMER NEEDS A HOT TEA');
assert(takeOrder(1, 1) == 'CUSTOMER NEEDS A COLD TEA');
/**
* Now that we did this, even more duplication is presented.
*
* And we can start to see a path forward.
* Let's move temperature out of the main conditional.
*
* We can even keep the temperature checks in there to prove how 'not worth doing'
* these steps can be.
*/
var takeOrder = function(drink, temperature) {
var order = 'CUSTOMER NEEDS A ';
if (temperature == 0) {
order += 'HOT ';
}
else if (temperature == 1) {
order += 'COLD ';
}
if (drink == 0 && temperature == 0) {
order += 'COFFEE';
}
else if (drink == 0 && temperature == 1) {
order += 'COFFEE';
}
else if (drink == 1 && temperature == 0) {
order += 'TEA';
}
else if (drink == 1 && temperature == 1) {
order += 'TEA';
}
else if (drink == 2 && temperature == 0) {
order += 'MILK';
}
else if (drink == 2 && temperature == 1) {
order += 'MILK';
}
else {
throw "UNSUPPORTED ORDER"
}
return order;
}
assert(takeOrder(0, 0) == 'CUSTOMER NEEDS A HOT COFFEE');
assert(takeOrder(0, 1) == 'CUSTOMER NEEDS A COLD COFFEE');
assert(takeOrder(1, 0) == 'CUSTOMER NEEDS A HOT TEA');
assert(takeOrder(1, 1) == 'CUSTOMER NEEDS A COLD TEA');
/**
* Next, we can remove the next obvious duplication:
*
*/
var takeOrder = function(drink, temperature) {
var order = 'CUSTOMER NEEDS A ';
if (temperature == 0) {
order += 'HOT ';
}
else if (temperature == 1) {
order += 'COLD ';
}
if (drink == 0) {
order += 'COFFEE';
}
else if (drink == 1) {
order += 'TEA';
}
else if (drink == 2) {
order += 'MILK';
}
else {
throw "UNSUPPORTED ORDER"
}
return order;
}
assert(takeOrder(0, 0) == 'CUSTOMER NEEDS A HOT COFFEE');
assert(takeOrder(0, 1) == 'CUSTOMER NEEDS A COLD COFFEE');
assert(takeOrder(1, 0) == 'CUSTOMER NEEDS A HOT TEA');
assert(takeOrder(1, 1) == 'CUSTOMER NEEDS A COLD TEA');
/**
* NOW WE CAN IMPLEMENT OUR CHANGE!!!!!
*
* We can ship it with a lot of confidence.
* If needed we can even continue improving.
*/
var takeOrder = function(drink, temperature, options) {
var order = 'CUSTOMER NEEDS ';
if (options == 0) {
// No option
}
else if (options == 1) {
order += 'CREAM AND ';
}
else if (options == 2) {
order += 'SUGAR AND ';
}
if (temperature == 0) {
order += 'A HOT ';
}
else {
order += 'A COLD ';
}
if (drink == 0) {
order += 'COFFEE';
}
else if (drink == 1) {
order += 'TEA';
}
else if (drink == 2) {
order += 'MILK';
}
else {
throw "UNSUPPORTED ORDER"
}
return order;
}
assert(takeOrder(0, 0, 1) == 'CUSTOMER NEEDS CREAM AND A HOT COFFEE');
assert(takeOrder(1, 1, 0) == 'CUSTOMER NEEDS A COLD TEA');
assert(takeOrder(2, 1, 2) == 'CUSTOMER NEEDS SUGAR AND A COLD MILK');
/**
* A COUPLE TECHNIQUES FOCUSING ON IMPROVING READABILITY
*
* - Use refactor tools
*
* - Renaming
* - Extracting variable
* - Extracting method
*/
/**
* RESOURCES
*
* VIDEO: Jim Weirich - Decoupling Rails
* - https://www.youtube.com/watch?v=tg5RFeSfBM4
*
* * REAL WORLD REFACTORING!!
*
* VIDEO: Jim Weirich - Roman Numeral Kata
* - https://www.youtube.com/watch?v=WBJ3hdcM7G8
*
* * Live coding that explains Red, Green, Refactor like crazy!
*
* KATA: (exercise)
* - https://github.com/emilybache/GildedRose-Refactoring-Kata
*
* * Available in most languages
* * Great exercise to learn testing + refactoring
*
* BOOK: Refactoring by Martin Fowler
* - http://martinfowler.com/books/refactoring.html
*
* BOOK: Working With Legacy Code by Michael Feathers
* - http://programmers.stackexchange.com/questions/122014/what-are-the-key-points-of-working-effectively-with-legacy-code
*
* * I wish I read this a long time ago
*
* WIKI: C2 WIKI
* - http://c2.com/cgi/wiki?WhatIsRefactoring
*
*/
/**
*
* THANK YOU
*
*
* GITHUB + SLIDES: www.github.com/jskulski/refactoring_talk
* Twitter: @jonskulski
* Email: [email protected]
*
*/
///---- support
function assert(condition) {
if (!condition) {
throw "ASSERTION FAILED";
} else {
console.log('success');
}
}