-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdry.ts
More file actions
48 lines (36 loc) · 1.33 KB
/
dry.ts
File metadata and controls
48 lines (36 loc) · 1.33 KB
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
// DRY PRINCIPLE
/*
Fast Overview
- D: Dont
- R: Repeat
- Y: Yourself
*/
// Each code block should be defined just one time in the codebase
// If you should modify something, you should be able to do it in just one section of the code
// The opposite to the DRY principle is WET ( Write Everything Twice )
/*
Sometimes forcing ourselves to use DRY could make the code less readable.
- If two functions are similar but not strictly equal, its better to make 2 separate funcitons rather than 1
- The DRY principle should always be balanced with the KISS principle (see kiss.ts) to mantain the code simple
*/
// ? Wrong Example
// If i'm gonna change the discount logic, then i need to modify it in each part where i use it
function makeCheckout(price: number): number {
const discount = price > 100 ? price * 0.1 : 0;
return price - discount
}
function makeRefund(price: number): number {
const discount = price > 100 ? price * 0.1 : 0;
return price - discount
}
// ? Correct Example
// I can change the discount calculus in just one place and it affects everything related to that
function calculateDiscount(price: number): number {
return price > 100 ? price * 0.1 : 0
}
function checkout(price: number): number {
return price - calculateDiscount(price)
}
function refund(price: number): number {
return price - calculateDiscount(price)
}