forked from 738/clean-code-typescript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.md
2890 lines (2114 loc) ยท 76 KB
/
README.md
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# clean-code-typescript [![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=ํด๋ฆฐ์ฝ๋%20ํ์
์คํฌ๋ฆฝํธ&url=https://github.com/738/clean-code-typescript)
> ๋ฒ์ญ ์์
์งํ์ค์
๋๋ค.
ํ์
์คํฌ๋ฆฝํธ๋ฅผ ์ํ ํด๋ฆฐ์ฝ๋.
[clean-code-javascript](https://github.com/ryanmcdermott/clean-code-javascript)์์ ์๊ฐ์ ๋ฐ์์ต๋๋ค.
## ๋ชฉ์ฐจ
1. [์๊ฐ(Introduction)](#์๊ฐintroduction)
2. [๋ณ์(Variables)](#๋ณ์variables)
3. [ํจ์(Functions)](#ํจ์functions)
4. [Objects and Data Structures](#objects-and-data-structures)
5. [Classes](#classes)
6. [SOLID](#solid)
7. [Testing](#testing)
8. [Concurrency](#concurrency)
9. [Error Handling](#error-handling)
10. [์์(Formatting)](#์์formatting)
11. [์ฃผ์(Comments)](#์ฃผ์comments)
12. [๋ฒ์ญ(Translations)](#๋ฒ์ญtranslations)
## ์๊ฐ(Introduction)
![Humorous image of software quality estimation as a count of how many expletives
you shout when reading code](https://www.osnews.com/images/comics/wtfm.jpg)
Robert C. Martin์ ์ฑ
์ธ [*ํด๋ฆฐ ์ฝ๋*](http://www.yes24.com/Product/Goods/11681152)์ ์๋ ์ํํธ์จ์ด ๊ณตํ ๋ฐฉ๋ฒ๋ก ์ ํ์
์คํฌ๋ฆฝํธ์ ์ ์ฉํ ๊ธ์
๋๋ค. ์ด ๊ธ์ ์คํ์ผ ๊ฐ์ด๋๊ฐ ์๋๋๋ค. ์ด ๊ธ์ ํ์
์คํฌ๋ฆฝํธ์์ [์ฝ๊ธฐ ์ฝ๊ณ , ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉฐ, ๋ฆฌํฉํ ๋ง ๊ฐ๋ฅํ](https://github.com/ryanmcdermott/3rs-of-software-architecture) ์ํํธ์จ์ด๋ฅผ ์์ฑํ๊ธฐ ์ํ ๊ฐ์ด๋์
๋๋ค.
์ฌ๊ธฐ ์๋ ๋ชจ๋ ๊ท์น์ ์๊ฒฉํ๊ฒ ๋ฐ๋ฅผ ํ์๋ ์์ผ๋ฉฐ, ๋ณดํธ์ ์ผ๋ก ํต์ฉ๋๋ ๊ท์น์ ์๋๋๋ค. ์ด ๊ธ์ ํ๋์ ์ง์นจ์ผ ๋ฟ์ด๋ฉฐ, *ํด๋ฆฐ ์ฝ๋*์ ์ ์๊ฐ ์๋
๊ฐ ๊ฒฝํํ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์ ๋ฆฌํ ๊ฒ์
๋๋ค.
์ํํธ์จ์ด ๊ณตํ ๊ธฐ์ ์ ์ญ์ฌ๋ 50๋
์ด ์กฐ๊ธ ๋์๊ณ , ๋ฐฐ์์ผ ํ ๊ฒ์ด ์ฌ์ ํ ๋ง์ต๋๋ค. ์ํํธ์จ์ด ์ค๊ณ๊ฐ ๊ฑด์ถ ์ค๊ณ๋งํผ ์ค๋๋์์ ๋๋ ์๋ง๋ ์๋ ๊ท์น๋ค๋ณด๋ค ์๊ฒฉํ ๊ท์น์ ๋ฐ๋ผ์ผ ํ ๊ฒ์
๋๋ค. ํ์ง๋ง ์ง๊ธ์ ์ด ์ง์นจ์ ๋น์ ๊ณผ ๋น์ ํ์ด ์์ฑํ๋ ํ์
์คํฌ๋ฆฝํธ ์ฝ๋์ ํ์ง์ ํ๊ฐํ๋ ๊ธฐ์ค์ผ๋ก ์ผ์ผ์ธ์.
ํ ๊ฐ์ง ๋ ๋ง๋ถ์ด์๋ฉด, ์ด ๊ท์น๋ค์ ์๊ฒ ๋๋ค ํด์ ๋น์ฅ ๋ ๋์ ๊ฐ๋ฐ์๊ฐ ๋๋ ๊ฒ์ ์๋๋ฉฐ ์ฝ๋๋ฅผ ์์ฑํ ๋ ์ค์๋ฅผ ํ์ง ์๊ฒ ํด์ฃผ๋ ๊ฒ์ ์๋๋๋ค. ํ๋ฅญํ ๋์๊ธฐ๋ค์ด ์ฒ์์ ๋ง๋ํ ์ ํ ๋ถํฐ ์์ํ๋ฏ์ด ๋ชจ๋ ์ฝ๋๋ฅผ ์ฒ์๋ถํฐ ์๋ฒฝํ ์ ์์ต๋๋ค. ํ์ง๋ง ๋น์ ์ ํ์๋ค๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ๋ฆฌ๋ทฐํ๋ฉฐ ์ ์ ์๋ฒฝํ๊ฒ ๋ง๋ค์ด ๋๊ฐ์ผ ํฉ๋๋ค. ๋น์ ์ด ์ฒ์ ์์ฑํ ์ฝ๋๋ฅผ ๊ณ ์น ๋ ์ ๋๋ก ์์ ์ ์งํํ์ง ๋ง์ธ์. ๋์ ์ฝ๋๋ฅผ ๋ถ์๊ณ ๋ ๋์ ์ฝ๋๋ฅผ ๋ง๋์ธ์!
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
## ๋ณ์(Variables)
### ์๋ฏธ์๋ ๋ณ์ ์ด๋ฆ์ ์ฌ์ฉํ์ธ์
์ฝ๋ ์ฌ๋์ผ๋ก ํ์ฌ๊ธ ๋ณ์๋ง๋ค ์ด๋ค ์ ์ด ๋ค๋ฅธ์ง ์ ์ ์๋๋ก ์ด๋ฆ์ ๊ตฌ๋ณํ์ธ์.
**Bad:**
```ts
function between<T>(a1: T, a2: T, a3: T): boolean {
return a2 <= a1 && a1 <= a3;
}
```
**Good:**
```ts
function between<T>(value: T, left: T, right: T): boolean {
return left <= value && value <= right;
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ๋ฐ์ํ ์ ์๋ ๋ณ์ ์ด๋ฆ์ ์ฌ์ฉํ์ธ์
๋ฐ์ํ ์ ์๋ ์ด๋ฆ์ ๊ทธ ๋ณ์์ ๋ํด์ ๋ฐ๋ณด ๊ฐ์ด ์๋ฆฌ๋ด์ด ํ ๋ก ํ ์ ๋ฐ์ ์์ต๋๋ค.
**Bad:**
```ts
type DtaRcrd102 = {
genymdhms: Date;
modymdhms: Date;
pszqint: number;
}
```
**Good:**
```ts
type Customer = {
generationTimestamp: Date;
modificationTimestamp: Date;
recordId: number;
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ๋์ผํ ์ ํ์ ๋ณ์๋ ๋์ผํ ๋จ์ด๋ฅผ ์ฌ์ฉํ์ธ์
**Bad:**
```ts
function getUserInfo(): User;
function getUserDetails(): User;
function getUserData(): User;
```
**Good:**
```ts
function getUser(): User;
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ๊ฒ์ํ ์ ์๋ ์ด๋ฆ์ ์ฌ์ฉํ์ธ์
์ฝ๋๋ฅผ ์ธ ๋๋ณด๋ค ์ฝ์ ๋๊ฐ ๋ ๋ง๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๊ฐ ์ฐ๋ ์ฝ๋๋ ์ฝ์ ์ ์๊ณ ๊ฒ์์ด ๊ฐ๋ฅํด์ผ ํฉ๋๋ค. ํ๋ก๊ทธ๋จ์ ์ดํดํ ๋ ์๋ฏธ์๋ ๋ณ์ ์ด๋ฆ์ ์ง์ง ์์ผ๋ฉด ์ฝ๋ ์ฌ๋์ผ๋ก ํ์ฌ๊ธ ์ด๋ ค์์ ์ค ์ ์์ต๋๋ค. ๊ฒ์ ๊ฐ๋ฅํ ์ด๋ฆ์ ์ง์ผ์ธ์. [TSLint](https://palantir.github.io/tslint/rules/no-magic-numbers/)์ ๊ฐ์ ๋๊ตฌ๋ ์ด๋ฆ์ด ์๋ ์์๋ฅผ ์๋ณํ ์ ์๋๋ก ๋์์ค๋๋ค.
**Bad:**
```ts
// 86400000์ด ๋๋์ฒด ๋ญ์ง?
setTimeout(restart, 86400000);
```
**Good:**
```ts
// ๋๋ฌธ์๋ก ์ด๋ฃจ์ด์ง ์์๋ก ์ ์ธํ์ธ์.
const MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000;
setTimeout(restart, MILLISECONDS_IN_A_DAY);
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ์๋๋ฅผ ๋ํ๋ด๋ ๋ณ์๋ฅผ ์ฌ์ฉํ์ธ์
**Bad:**
```ts
declare const users: Map<string, User>;
for (const keyValue of users) {
// users ๋งต์ ์ํ
}
```
**Good:**
```ts
declare const users: Map<string, User>;
for (const [id, user] of users) {
// users ๋งต์ ์ํ
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ์์ํ๋ ์ด๋ฆ์ ์ฌ์ฉํ์ง ๋ง์ธ์
๋ช
์์ ์ธ ๊ฒ์ด ์์์ ์ธ ๊ฒ๋ณด๋ค ์ข์ต๋๋ค.
*๋ช
๋ฃํจ์ ์ต๊ณ ์
๋๋ค.*
**Bad:**
```ts
const u = getUser();
const s = getSubscription();
const t = charge(u, s);
```
**Good:**
```ts
const user = getUser();
const subscription = getSubscription();
const transaction = charge(user, subscription);
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ๋ถํ์ํ ๋ฌธ๋งฅ์ ์ถ๊ฐํ์ง ๋ง์ธ์
ํด๋์ค/ํ์
/๊ฐ์ฒด์ ์ด๋ฆ์ ์๋ฏธ๊ฐ ๋ด๊ฒจ์๋ค๋ฉด, ๋ณ์ ์ด๋ฆ์์ ๋ฐ๋ณตํ์ง ๋ง์ธ์.
**Bad:**
```ts
type Car = {
carMake: string;
carModel: string;
carColor: string;
}
function print(car: Car): void {
console.log(`${car.carMake} ${car.carModel} (${car.carColor})`);
}
```
**Good:**
```ts
type Car = {
make: string;
model: string;
color: string;
}
function print(car: Car): void {
console.log(`${car.make} ${car.model} (${car.color})`);
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### short circuiting์ด๋ ์กฐ๊ฑด๋ฌธ ๋์ ๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ธ์
๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์๋ short circuiting๋ณด๋ค ๋ณดํต ๋ช
๋ฃํฉ๋๋ค.
**Bad:**
```ts
function loadPages(count?: number) {
const loadCount = count !== undefined ? count : 10;
// ...
}
```
**Good:**
```ts
function loadPages(count: number = 10) {
// ...
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ์๋๋ฅผ ์๋ ค์ฃผ๊ธฐ ์ํด `enum`์ ์ฌ์ฉํ์ธ์
์๋ฅผ ๋ค์ด ๊ทธ๊ฒ๋ค์ ๊ฐ ์์ฒด๋ณด๋ค ๊ฐ์ด ๊ตฌ๋ณ๋์ด์ผ ํ ๋์ ๊ฐ์ด ์ฝ๋์ ์๋๋ฅผ ์๋ ค์ฃผ๋๋ฐ์ `enum`์ ๋์์ ์ค ์ ์์ต๋๋ค.
**Bad:**
```ts
const GENRE = {
ROMANTIC: 'romantic',
DRAMA: 'drama',
COMEDY: 'comedy',
DOCUMENTARY: 'documentary',
}
projector.configureFilm(GENRE.COMEDY);
class Projector {
// Projector์ ์ ์ธ
configureFilm(genre) {
switch (genre) {
case GENRE.ROMANTIC:
// ์คํ๋์ด์ผ ํ๋ ๋ก์ง
}
}
}
```
**Good:**
```ts
enum GENRE {
ROMANTIC,
DRAMA,
COMEDY,
DOCUMENTARY,
}
projector.configureFilm(GENRE.COMEDY);
class Projector {
// Projector์ ์ ์ธ
configureFilm(genre) {
switch (genre) {
case GENRE.ROMANTIC:
// ์คํ๋์ด์ผ ํ๋ ๋ก์ง
}
}
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
## ํจ์(Functions)
### ํจ์์ ๋งค๊ฐ๋ณ์๋ 2๊ฐ ํน์ ๊ทธ ์ดํ๊ฐ ์ด์์ ์
๋๋ค
ํจ์ ๋งค๊ฐ๋ณ์์ ๊ฐ์๋ฅผ ์ ํํ๋ ๊ฒ์ ํจ์๋ฅผ ํ
์คํธํ๊ธฐ ์ฝ๊ฒ ๋ง๋ค์ด์ฃผ๊ธฐ ๋๋ฌธ์ ๋๋ผ์ธ ์ ๋๋ก ์ค์ํฉ๋๋ค.
ํจ์ ๋งค๊ฐ๋ณ์๊ฐ 3๊ฐ ์ด์์ธ ๊ฒฝ์ฐ, ๊ฐ๊ธฐ ๋ค๋ฅธ ์ธ์๋ก ์ฌ๋ฌ ๋ค๋ฅธ ์ผ์ด์ค๋ฅผ ํ
์คํธํด์ผ ํ๋ฏ๋ก ๊ฒฝ์ฐ์ ์๊ฐ ๋งค์ฐ ๋ง์์ง๋๋ค.
ํ ๊ฐ ํน์ ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๊ฐ ์ด์์ ์ธ ๊ฒฝ์ฐ๊ณ , ๊ฐ๋ฅํ๋ค๋ฉด ์ธ ๊ฐ๋ ํผํด์ผ ํฉ๋๋ค. ๊ทธ ์ด์์ ๊ฒฝ์ฐ์๋ ํฉ์ณ์ผ ํฉ๋๋ค.
๋ ๊ฐ ์ด์์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง ๊ฒฝ์ฐ, ํจ์๊ฐ ๋ง์ ๊ฒ์ ํ ๊ฐ๋ฅ์ฑ์ด ๋์์ง๋๋ค.
๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ, ๋๋ถ๋ถ ์์ ๊ฐ์ฒด๋ ํ๋์ ๋งค๊ฐ๋ณ์๋ก ์ถฉ๋ถํ ๊ฒ์
๋๋ค.
๋ง์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค๋ฉด ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ์ฌ์ฉํ๋ ๊ฒ์ ๊ณ ๋ คํด๋ณด์ธ์.
ํจ์๊ฐ ๊ธฐ๋ํ๋ ์์ฑ์ ๋ช
ํํ๊ฒ ํ๊ธฐ ์ํด, [๊ตฌ์กฐ ๋ถํด](https://basarat.gitbook.io/typescript/future-javascript/destructuring) ๊ตฌ๋ฌธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ด ๊ตฌ๋ฌธ์ ๋ช ๊ฐ์ ์ฅ์ ์ ๊ฐ์ง๊ณ ์์ต๋๋ค:
1. ์ด๋ค ์ฌ๋์ด ํจ์ ์๊ทธ๋์ณ(๋งค๊ฐ๋ณ์์ ํ์
, ๋ฐํ๊ฐ์ ํ์
๋ฑ)๋ฅผ ๋ณผ ๋, ์ด๋ค ์์ฑ์ด ์ฌ์ฉ๋๋์ง ์ฆ์ ์ ์ ์์ต๋๋ค.
2. ๋ช
๋ช
๋ ๋งค๊ฐ๋ณ์์ฒ๋ผ ๋ณด์ด๊ฒ ํ ๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
3. ๋ํ ๊ตฌ์กฐ ๋ถํด๋ ํจ์๋ก ์ ๋ฌ๋ ๋งค๊ฐ๋ณ์ ๊ฐ์ฒด์ ํน์ ํ ์์ ๊ฐ์ ๋ณต์ ํ๋ฉฐ ์ด๊ฒ์ ์ฌ์ด๋ ์ดํํธ๋ฅผ ๋ฐฉ์งํ๋๋ฐ ๋์์ ์ค๋๋ค. ์ ์์ฌํญ: ๋งค๊ฐ๋ณ์ ๊ฐ์ฒด๋ก๋ถํฐ ๊ตฌ์กฐ ๋ถํด๋ ๊ฐ์ฒด์ ๋ฐฐ์ด์ **๋ณต์ ๋์ง ์์ต๋๋ค.**
4. ํ์
์คํฌ๋ฆฝํธ๋ ์ฌ์ฉํ์ง ์์ ์์ฑ์ ๋ํด์ ๊ฒฝ๊ณ ๋ฅผ ์ฃผ๋ฉฐ, ๊ตฌ์กฐ ๋ถํด๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฒฝ๊ณ ๋ฅผ ๋ฐ์ง ์์ ์ ์์ต๋๋ค.
**Bad:**
```ts
function createMenu(title: string, body: string, buttonText: string, cancellable: boolean) {
// ...
}
createMenu('Foo', 'Bar', 'Baz', true);
```
**Good:**
```ts
function createMenu(options: { title: string, body: string, buttonText: string, cancellable: boolean }) {
// ...
}
createMenu({
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
});
```
[type aliases](https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-aliases)๋ฅผ ์ฌ์ฉํด์ ๊ฐ๋
์ฑ์ ๋ ๋์ผ ์ ์์ต๋๋ค:
```ts
type MenuOptions = { title: string, body: string, buttonText: string, cancellable: boolean };
function createMenu(options: MenuOptions) {
// ...
}
createMenu({
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
});
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ํจ์๋ ํ๊ฐ์ง๋ง ํด์ผํฉ๋๋ค
์ด๊ฒ์ ์ํํธ์จ์ด ๊ณตํ์์ ๋จ์ฐ์ฝ ์ค์ํ ๊ท์น์
๋๋ค. ํจ์๊ฐ ํ๊ฐ์ง ์ด์์ ์ญํ ์ ์ํํ ๋ ์์ฑํ๊ณ ํ
์คํธํ๊ณ ์ถ๋ก ํ๊ธฐ ์ด๋ ค์์ง๋๋ค. ํจ์๋ฅผ ํ๋์ ํ๋์ผ๋ก ์ ์ํ ์ ์์ ๋, ์ฝ๊ฒ ๋ฆฌํฉํ ๋งํ ์ ์์ผ๋ฉฐ ์ฝ๋๋ฅผ ๋์ฑ ๋ช
๋ฃํ๊ฒ ์ฝ์ ์ ์์ต๋๋ค.
์ด ๊ฐ์ด๋์์ ์ด ๋ถ๋ถ๋ง ์๊ธฐ๊ฒ์ผ๋ก ๋ง๋ค์ด๋ ๋น์ ์ ๋ง์ ๊ฐ๋ฐ์๋ณด๋ค ์์ค ์ ์์ต๋๋ค.
**Bad:**
```ts
function emailClients(clients: Client[]) {
clients.forEach((client) => {
const clientRecord = database.lookup(client);
if (clientRecord.isActive()) {
email(client);
}
});
}
```
**Good:**
```ts
function emailClients(clients: Client[]) {
clients.filter(isActiveClient).forEach(email);
}
function isActiveClient(client: Client) {
const clientRecord = database.lookup(client);
return clientRecord.isActive();
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ํจ์๊ฐ ๋ฌด์์ ํ๋์ง ์ ์ ์๋๋ก ํจ์ ์ด๋ฆ์ ์ง์ผ์ธ์
**Bad:**
```ts
function addToDate(date: Date, month: number): Date {
// ...
}
const date = new Date();
// ๋ฌด์์ด ์ถ๊ฐ๋๋์ง ํจ์ ์ด๋ฆ๋ง์ผ๋ก ์ ์ถํ๊ธฐ ์ด๋ ต์ต๋๋ค
addToDate(date, 1);
```
**Good:**
```ts
function addMonthToDate(date: Date, month: number): Date {
// ...
}
const date = new Date();
addMonthToDate(date, 1);
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ํจ์๋ ๋จ์ผ ํ๋์ ์ถ์ํํด์ผ ํฉ๋๋ค
ํจ์๊ฐ ํ๊ฐ์ง ์ด์์ ์ถ์ํํ๋ค๋ฉด ๊ทธ ํจ์๋ ๋๋ฌด ๋ง์ ์ผ์ ํ๊ฒ ๋ฉ๋๋ค. ์ฌ์ฌ์ฉ์ฑ๊ณผ ์ฌ์ด ํ
์คํธ๋ฅผ ์ํด์ ํจ์๋ฅผ ์ชผ๊ฐ์ธ์.
**Bad:**
```ts
function parseCode(code: string) {
const REGEXES = [ /* ... */ ];
const statements = code.split(' ');
const tokens = [];
REGEXES.forEach((regex) => {
statements.forEach((statement) => {
// ...
});
});
const ast = [];
tokens.forEach((token) => {
// lex...
});
ast.forEach((node) => {
// parse...
});
}
```
**Good:**
```ts
const REGEXES = [ /* ... */ ];
function parseCode(code: string) {
const tokens = tokenize(code);
const syntaxTree = parse(tokens);
syntaxTree.forEach((node) => {
// parse...
});
}
function tokenize(code: string): Token[] {
const statements = code.split(' ');
const tokens: Token[] = [];
REGEXES.forEach((regex) => {
statements.forEach((statement) => {
tokens.push( /* ... */ );
});
});
return tokens;
}
function parse(tokens: Token[]): SyntaxTree {
const syntaxTree: SyntaxTree[] = [];
tokens.forEach((token) => {
syntaxTree.push( /* ... */ );
});
return syntaxTree;
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ์ค๋ณต๋ ์ฝ๋๋ฅผ ์ ๊ฑฐํด์ฃผ์ธ์
์ฝ๋๊ฐ ์ค๋ณต๋์ง ์๋๋ก ์ต์ ์ ๋คํ์ธ์.
์ค๋ณต๋ ์ฝ๋๋ ์ด๋ค ๋ก์ง์ ๋ณ๊ฒฝํ ๋ ํ ๊ณณ ์ด์์ ๋ณ๊ฒฝํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ข์ง ์์ต๋๋ค.
๋น์ ์ด ๋ ์คํ ๋์ ์ด์ํ๋ฉด์ ์ฌ๊ณ ๋ฅผ ์ถ์ ํ๋ค๊ณ ์์ํด๋ณด์ธ์: ๋ชจ๋ ํ ๋งํ , ์ํ, ๋ง๋, ์๋
๋ฑ.
๊ด๋ฆฌํ๋ ๋ชฉ๋ก์ด ์ฌ๋ฌ๊ฐ์ผ ๋ ํ ๋งํ ๋ฅผ ๋ฃ์ ์๋ฆฌ๋ฅผ ์ ๊ณตํ ๋๋ง๋ค ๋ชจ๋ ๋ชฉ๋ก์ ์์ ํด์ผ ํฉ๋๋ค.
๊ด๋ฆฌํ๋ ๋ชฉ๋ก์ด ๋จ ํ๋์ผ ๋๋ ํ ๊ณณ๋ง ์์ ํ๋ฉด ๋ฉ๋๋ค!
๋น์ ์ ์ข
์ข
๋ ๊ฐ ์ด์์ ์ฌ์ํ ๋ค๋ฅธ ๊ฒ๋ค์ด ์๋ค๊ณ ๋ง์ ๊ฒ๋ค์ด ๊ณต์ ๋๋ ์ค๋ณต๋๋ ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค. ํ์ง๋ง ๊ทธ ๋ช๊ฐ์ง ๋ค๋ฅธ ๊ฒ์ผ๋ก ์ธํด ๊ฐ์ ์ญํ ์ ํ๋ ๋ ๊ฐ ์ด์์ ํจ์๋ฅผ ๋ง๋ค๊ฒ ๋ฉ๋๋ค. ์ค๋ณต๋ ์ฝ๋๋ฅผ ์ ๊ฑฐํ๋ ๊ฒ์ ์กฐ๊ธ์ฉ ๋ค๋ฅธ ์ญํ ์ ํ๋ ๊ฒ์ ๋ฌถ์์ผ๋ก์จ ํ๋์ ํจ์/๋ชจ๋/ํด๋์ค๋ก ์ฒ๋ฆฌํ๋ ์ถ์ํ๋ฅผ ๋ง๋๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
์ถ์ํ๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ํ๋ ๊ฒ์ ์ค์ํ๋ฉฐ, ์ด๊ฒ์ [SOLID](#solid) ์์น์ ๋ฐ๋ฅด๋ ์ด์ ์ด๊ธฐ๋ ํฉ๋๋ค. ์ฌ๋ฐ๋ฅด์ง ์์ ์ถ์ํ๋ ์ค๋ณต๋ ์ฝ๋๋ณด๋ค ๋์๋ฏ๋ก ์ฃผ์ํ์ธ์! ์ข์ ์ถ์ํ๋ฅผ ํ ์ ์๋ค๋ฉด ๊ทธ๋ ๊ฒ ํ๋ผ๋ ๋ง์
๋๋ค! ๋ฐ๋ณตํ์ง ๋ง์ธ์. ๊ทธ๋ ์ง ์์ผ๋ฉด ํ๋๋ฅผ ๋ณ๊ฒฝํ ๋๋ง๋ค ์ฌ๋ฌ ๊ณณ์ ๋ณ๊ฒฝํ๊ฒ ๋ ๊ฒ์
๋๋ค.
**Bad:**
```ts
function showDeveloperList(developers: Developer[]) {
developers.forEach((developer) => {
const expectedSalary = developer.calculateExpectedSalary();
const experience = developer.getExperience();
const githubLink = developer.getGithubLink();
const data = {
expectedSalary,
experience,
githubLink
};
render(data);
});
}
function showManagerList(managers: Manager[]) {
managers.forEach((manager) => {
const expectedSalary = manager.calculateExpectedSalary();
const experience = manager.getExperience();
const portfolio = manager.getMBAProjects();
const data = {
expectedSalary,
experience,
portfolio
};
render(data);
});
}
```
**Good:**
```ts
class Developer {
// ...
getExtraDetails() {
return {
githubLink: this.githubLink,
}
}
}
class Manager {
// ...
getExtraDetails() {
return {
portfolio: this.portfolio,
}
}
}
function showEmployeeList(employee: Developer | Manager) {
employee.forEach((employee) => {
const expectedSalary = employee.calculateExpectedSalary();
const experience = employee.getExperience();
const extra = employee.getExtraDetails();
const data = {
expectedSalary,
experience,
extra,
};
render(data);
});
}
```
๋น์ ์ ์ค๋ณต๋ ์ฝ๋์ ๋ํด์ ๋นํ์ ์ผ๋ก ์๊ฐํด์ผ ํฉ๋๋ค. ๊ฐ๋์ ์ค๋ณต๋ ์ฝ๋์ ๋ถํ์ํ ์ถ์ํ๋ก ์ธํ ๋ณต์ก์ฑ ๊ฐ์ ๋ง๋ฐ๊ฟ์ด ์์ ์ ์์ต๋๋ค. ์๋ก ๋ค๋ฅธ ๋ ๊ฐ์ ๋ชจ๋์ ๊ตฌํ์ด ์ ์ฌํด๋ณด์ด์ง๋ง ์๋ก ๋ค๋ฅธ ๋๋ฉ์ธ์ ์กด์ฌํ๋ ๊ฒฝ์ฐ, ์ฝ๋ ์ค๋ณต์ ๊ณตํต๋ ์ฝ๋์์ ์ถ์ถํด์ ์ค๋ณต์ ์ค์ด๋ ๊ฒ๋ณด๋ค ๋์ ์ ํ์ผ ์ ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ์ ์ถ์ถ๋ ๊ณตํต์ ์ฝ๋๋ ๋ ๋ชจ๋ ์ฌ์ด์์ ๊ฐ์ ์ ์ธ ์์กด์ฑ์ด ๋ํ๋๊ฒ ๋ฉ๋๋ค.
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### `Object.assign` ํน์ ๊ตฌ์กฐ ๋ถํด๋ฅผ ์ฌ์ฉํด์ ๊ธฐ๋ณธ ๊ฐ์ฒด๋ฅผ ๋ง๋์ธ์
**Bad:**
```ts
type MenuConfig = { title?: string, body?: string, buttonText?: string, cancellable?: boolean };
function createMenu(config: MenuConfig) {
config.title = config.title || 'Foo';
config.body = config.body || 'Bar';
config.buttonText = config.buttonText || 'Baz';
config.cancellable = config.cancellable !== undefined ? config.cancellable : true;
// ...
}
createMenu({ body: 'Bar' });
```
**Good:**
```ts
type MenuConfig = { title?: string, body?: string, buttonText?: string, cancellable?: boolean };
function createMenu(config: MenuConfig) {
const menuConfig = Object.assign({
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
}, config);
// ...
}
createMenu({ body: 'Bar' });
```
๋์์ผ๋ก, ๊ธฐ๋ณธ ๊ฐ์ ๊ตฌ์กฐ ๋ถํด๋ฅผ ์ฌ์ฉํด์ ํด๊ฒฐํ ์ ์์ต๋๋ค:
```ts
type MenuConfig = { title?: string, body?: string, buttonText?: string, cancellable?: boolean };
function createMenu({ title = 'Foo', body = 'Bar', buttonText = 'Baz', cancellable = true }: MenuConfig) {
// ...
}
createMenu({ body: 'Bar' });
```
์ฌ์ด๋ ์ดํํธ์ `undefined` ํน์ `null` ๊ฐ์ ๋ช
์์ ์ผ๋ก ๋๊ธฐ๋ ์์์น๋ชปํ ํ๋์ ํผํ๊ธฐ ์ํด์ ํ์
์คํฌ๋ฆฝํธ ์ปดํ์ผ๋ฌ์๊ฒ ๊ทธ๊ฒ์ ํ๋ฝํ์ง์๋๋ก ์ค์ ํ ์ ์์ต๋๋ค. ํ์
์คํฌ๋ฆฝํธ์์ [`--strictNullChecks`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#--strictnullchecks) ์ต์
์ ํ์ธํ์ธ์.
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ํจ์ ๋งค๊ฐ๋ณ์๋ก ํ๋๊ทธ๋ฅผ ์ฌ์ฉํ์ง ๋ง์ธ์
ํ๋๊ทธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ํด๋น ํจ์๊ฐ ํ ๊ฐ์ง ์ด์์ ์ผ์ ํ๋ค๋ ๊ฒ์ ๋ปํฉ๋๋ค.
ํจ์๋ ํ ๊ฐ์ง์ ์ผ์ ํด์ผํฉ๋๋ค. boolean ๋ณ์๋ก ์ธํด ๋ค๋ฅธ ์ฝ๋๊ฐ ์คํ๋๋ค๋ฉด ๊ทธ ํจ์๋ฅผ ์ชผ๊ฐ๋๋ก ํ์ธ์.
**Bad:**
```ts
function createFile(name: string, temp: boolean) {
if (temp) {
fs.create(`./temp/${name}`);
} else {
fs.create(name);
}
}
```
**Good:**
```ts
function createTempFile(name: string) {
createFile(`./temp/${name}`);
}
function createFile(name: string) {
fs.create(name);
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ์ฌ์ด๋ ์ดํํธ๋ฅผ ํผํ์ธ์ (ํํธ 1)
ํจ์๋ ๊ฐ์ ๊ฐ์ ธ์์ ๋ค๋ฅธ ๊ฐ์ ๋ฐํํ๋ ๊ฒ ์ด์ธ์ ๋ค๋ฅธ ๊ฒ์ ํ ๊ฒฝ์ฐ ์ฌ์ด๋ ์ดํํธ๋ฅผ ๋ฐ์์ํฌ ์ ์์ต๋๋ค. ์ฌ์ด๋ ์ดํํธ๋ ํ์ผ์ ์ด๋ค๊ฑฐ๋, ์ ์ญ ๋ณ์๋ฅผ ์กฐ์ํ๋ค๊ฑฐ๋, ๋ปํ์ง ์๊ฒ ๋ฏ์ ์ฌ๋์๊ฒ ๋น์ ์ ์ ์ฌ์ฐ์ ์ก๊ธํ ์ ์์ต๋๋ค.
๋น์ ์ ๊ฐ๋ ํ๋ก๊ทธ๋จ์์ ์ฌ์ด๋ ์ดํํธ๋ฅผ ๊ฐ์ง ํ์๊ฐ ์์ต๋๋ค. ์ด์ ์ ์ฌ๋ก์์์ ๊ฐ์ด ๋น์ ์ ํ์ผ์ ์จ์ผํ ๋๊ฐ ์์ต๋๋ค.
๋น์ ์ด ํ๊ณ ์ถ์ ๊ฒ์ ์ด๊ฒ์ ํ๋ ๊ณณ์ ์ค์ํํ๋ ๊ฒ์
๋๋ค. ํน์ ํ์ผ์ ์ฐ๊ธฐ ์ํด ๋ช ๊ฐ์ ํจ์์ ํด๋์ค๋ฅผ ๋ง๋ค์ง ๋ง์ธ์.
๊ทธ๊ฒ์ ํํ๋ ์๋น์ค๋ฅผ ํ๋๋ง ๋ง๋์ธ์. ์ค์ง ํ๋์
๋๋ค.
The main point is to avoid common pitfalls like sharing state between objects without any structure, using mutable data types that can be written to by anything, and not centralizing where your side effects occur. If you can do this, you will be happier than the vast majority of other programmers.
**Bad:**
```ts
// Global variable referenced by following function.
let name = 'Robert C. Martin';
function toBase64() {
name = btoa(name);
}
toBase64();
// If we had another function that used this name, now it'd be a Base64 value
console.log(name); // expected to print 'Robert C. Martin' but instead 'Um9iZXJ0IEMuIE1hcnRpbg=='
```
**Good:**
```ts
const name = 'Robert C. Martin';
function toBase64(text: string): string {
return btoa(text);
}
const encodedName = toBase64(name);
console.log(name);
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ์ฌ์ด๋ ์ดํํธ๋ฅผ ํผํ์ธ์ (ํํธ 2)
In JavaScript, primitives are passed by value and objects/arrays are passed by reference. In the case of objects and arrays, if your function makes a change in a shopping cart array, for example, by adding an item to purchase, then any other function that uses that `cart` array will be affected by this addition. That may be great, however it can be bad too. Let's imagine a bad situation:
The user clicks the "Purchase", button which calls a `purchase` function that spawns a network request and sends the `cart` array to the server. Because of a bad network connection, the purchase function has to keep retrying the request. Now, what if in the meantime the user accidentally clicks "Add to Cart" button on an item they don't actually want before the network request begins? If that happens and the network request begins, then that purchase function will send the accidentally added item because it has a reference to a shopping cart array that the `addItemToCart` function modified by adding an unwanted item.
A great solution would be for the `addItemToCart` to always clone the `cart`, edit it, and return the clone. This ensures that no other functions that are holding onto a reference of the shopping cart will be affected by any changes.
Two caveats to mention to this approach:
1. There might be cases where you actually want to modify the input object, but when you adopt this programming practice you will find that those cases are pretty rare. Most things can be refactored to have no side effects! (see [pure function](https://en.wikipedia.org/wiki/Pure_function))
2. Cloning big objects can be very expensive in terms of performance. Luckily, this isn't a big issue in practice because there are great libraries that allow this kind of programming approach to be fast and not as memory intensive as it would be for you to manually clone objects and arrays.
**Bad:**
```ts
function addItemToCart(cart: CartItem[], item: Item): void {
cart.push({ item, date: Date.now() });
};
```
**Good:**
```ts
function addItemToCart(cart: CartItem[], item: Item): CartItem[] {
return [...cart, { item, date: Date.now() }];
};
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ์ ์ญ ํจ์๋ฅผ ์์ฑํ์ง ๋ง์ธ์
Polluting globals is a bad practice in JavaScript because you could clash with another library and the user of your API would be none-the-wiser until they get an exception in production. Let's think about an example: what if you wanted to extend JavaScript's native Array method to have a `diff` method that could show the difference between two arrays? You could write your new function to the `Array.prototype`, but it could clash with another library that tried to do the same thing. What if that other library was just using `diff` to find the difference between the first and last elements of an array? This is why it would be much better to just use classes and simply extend the `Array` global.
**Bad:**
```ts
declare global {
interface Array<T> {
diff(other: T[]): Array<T>;
}
}
if (!Array.prototype.diff) {
Array.prototype.diff = function <T>(other: T[]): T[] {
const hash = new Set(other);
return this.filter(elem => !hash.has(elem));
};
}
```
**Good:**
```ts
class MyArray<T> extends Array<T> {
diff(other: T[]): T[] {
const hash = new Set(other);
return this.filter(elem => !hash.has(elem));
};
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ๋ช
๋ นํ ํ๋ก๊ทธ๋๋ฐ๋ณด๋ค ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ์งํฅํ์ธ์
๊ฐ๋ฅํ๋ค๋ฉด ์ด๋ฐ ๋ฐฉ์์ ํ๋ก๊ทธ๋๋ฐ์ ์งํฅํ์ธ์.
**Bad:**
```ts
const contributions = [
{
name: 'Uncle Bobby',
linesOfCode: 500
}, {
name: 'Suzie Q',
linesOfCode: 1500
}, {
name: 'Jimmy Gosling',
linesOfCode: 150
}, {
name: 'Gracie Hopper',
linesOfCode: 1000
}
];
let totalOutput = 0;
for (let i = 0; i < contributions.length; i++) {
totalOutput += contributions[i].linesOfCode;
}
```
**Good:**
```ts
const contributions = [
{
name: 'Uncle Bobby',
linesOfCode: 500
}, {
name: 'Suzie Q',
linesOfCode: 1500
}, {
name: 'Jimmy Gosling',
linesOfCode: 150
}, {
name: 'Gracie Hopper',
linesOfCode: 1000
}
];
const totalOutput = contributions
.reduce((totalLines, output) => totalLines + output.linesOfCode, 0);
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ์กฐ๊ฑด๋ฌธ์ ์บก์ํํ์ธ์
**Bad:**
```ts
if (subscription.isTrial || account.balance > 0) {
// ...
}
```
**Good:**
```ts
function canActivateService(subscription: Subscription, account: Account) {
return subscription.isTrial || account.balance > 0;
}
if (canActivateService(subscription, account)) {
// ...
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ๋ถ์ ์กฐ๊ฑด๋ฌธ์ ํผํ์ธ์
**Bad:**
```ts
function isEmailNotUsed(email: string): boolean {
// ...
}
if (isEmailNotUsed(email)) {
// ...
}
```
**Good:**
```ts
function isEmailUsed(email): boolean {
// ...
}
if (!isEmailUsed(node)) {
// ...
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ์กฐ๊ฑด๋ฌธ์ ํผํ์ธ์
This seems like an impossible task. Upon first hearing this, most people say, "how am I supposed to do anything without an `if` statement?" The answer is that you can use polymorphism to achieve the same task in many cases. The second question is usually, "well that's great but why would I want to do that?" The answer is a previous clean code concept we learned: a function should only do one thing. When you have classes and functions that have `if` statements, you are telling your user that your function does more than one thing. Remember, just do one thing.
**Bad:**
```ts
class Airplane {
private type: string;
// ...
getCruisingAltitude() {
switch (this.type) {
case '777':
return this.getMaxAltitude() - this.getPassengerCount();
case 'Air Force One':
return this.getMaxAltitude();
case 'Cessna':
return this.getMaxAltitude() - this.getFuelExpenditure();
default:
throw new Error('Unknown airplane type.');
}
}
private getMaxAltitude(): number {
// ...
}
}
```
**Good:**
```ts
abstract class Airplane {
protected getMaxAltitude(): number {
// shared logic with subclasses ...
}
// ...
}
class Boeing777 extends Airplane {
// ...
getCruisingAltitude() {
return this.getMaxAltitude() - this.getPassengerCount();
}
}
class AirForceOne extends Airplane {
// ...
getCruisingAltitude() {
return this.getMaxAltitude();
}
}
class Cessna extends Airplane {
// ...
getCruisingAltitude() {
return this.getMaxAltitude() - this.getFuelExpenditure();
}
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ํ์
์ฒดํน์ ํผํ์ธ์
TypeScript is a strict syntactical superset of JavaScript and adds optional static type checking to the language.
Always prefer to specify types of variables, parameters and return values to leverage the full power of TypeScript features.
It makes refactoring more easier.
**Bad:**
```ts
function travelToTexas(vehicle: Bicycle | Car) {
if (vehicle instanceof Bicycle) {
vehicle.pedal(currentLocation, new Location('texas'));
} else if (vehicle instanceof Car) {
vehicle.drive(currentLocation, new Location('texas'));
}
}
```
**Good:**
```ts
type Vehicle = Bicycle | Car;
function travelToTexas(vehicle: Vehicle) {
vehicle.move(currentLocation, new Location('texas'));
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ํ์ ์ด์์ผ๋ก ์ต์ ํํ์ง ๋ง์ธ์
Modern browsers do a lot of optimization under-the-hood at runtime. A lot of times, if you are optimizing then you are just wasting your time. There are good [resources](https://github.com/petkaantonov/bluebird/wiki/Optimization-killers) for seeing where optimization is lacking. Target those in the meantime, until they are fixed if they can be.
**Bad:**
```ts
// On old browsers, each iteration with uncached `list.length` would be costly
// because of `list.length` recomputation. In modern browsers, this is optimized.
for (let i = 0, len = list.length; i < len; i++) {
// ...
}
```
**Good:**
```ts
for (let i = 0; i < list.length; i++) {
// ...
}
```
**[โฌ ์๋จ์ผ๋ก](#๋ชฉ์ฐจ)**
### ํ์ํ์ง ์๋ ์ฝ๋๋ ์ ๊ฑฐํ์ธ์
Dead code is just as bad as duplicate code. There's no reason to keep it in your codebase.
If it's not being called, get rid of it! It will still be safe in your version history if you still need it.
**Bad:**
```ts
function oldRequestModule(url: string) {