Clean Code
๐ Junior Vs Senior Code - How To Write Better Code๋ฅผ ๋ณด๊ณ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค.
1. Logic(๋ก์ง)
Step 1๏ธโฃ
// ๋งค๊ฐ๋ณ์๊ฐ ์์์ผ ๋ ์ธ์ ์์์ ๊ดํธ ํจ๊ป ๋ฐํํ๋ ํจ์
// step 1
function toAccount(n) {
if(n < 0) {
return '(' + Math.abs(n) + ')'
} else if(n >= 0) {
return n
}
}
Step 2๏ธโฃ
// step 2
// ํจ์๋ช
๊ณผ ๋ณ์๋ช
์์
function numberToAccountingString(number) {
// undefined, null ๊ณ ๋ ค
if(number !== null) {
if(number < 0) {
// template literal
return `(${Math.abs(number)})` // 4) ํ์
- ๋ฌธ์์ด
} else {
return number.toString() // ํ์
- ๋ฌธ์์ด
}
}
}
console.log(numberToAccountingString(undefined))
Step 3๏ธโฃ
// step 3
// ๋ณดํธ ๊ตฌ๋ฌธ(Guard Clauses) - ์ค์ฒฉ ๊ตฌ๋ฌธ X -- ์ฝ๋ ๊ฐ๋
์ฑ, ๊ฐ๊ฒฐ์ฑ
// https://blog.webdevsimplified.com/2020-01/guard-clauses/
// https://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html
function numberToAccountingString(number) {
// Guard Clauses
if(number == null) return
if(number < 0) return `(${Math.abs(number)})`
return number.toString()
}
console.log(numberToAccountingString(undefined))
2. Variable Manipulation(๋งค๊ฐ๋ณ์ ์กฐ์)
Step 1๏ธโฃ
// step 1
function caculateTotal(items, options) {
let t = 0
items.forEach(i => {
t += i.price * i.quan
})
t = t - t * (options.dis || 0)
t = t * 1.1
t = t + (options.ship || 5) // ๐ (0 || 5) === (false || 5) === 5
return t
}
const testItems = [
{ price: 15, quan: 2 },
{ price: 20, quan: 1 },
{ price: 5, quan: 4 },
]
console.log(caculateTotal()) // Error
console.log(caculateTotal(testItems, {})) // Error
console.log(caculateTotal([], {})) // Wrong value // 5
console.log(caculateTotal(testItems, { ship: 0 })) // Wrong value // 82
Step 2๏ธโฃ
// ์์(const) ์ ์ธ
const TAX_RATE = 1.1
const SHIPPING_DEFAULT = 5
// ๋งค๊ฐ๋ณ์์ ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐ({})
funtion caculateTotal(items, options = {}) {
// ๋ณดํธ ๊ตฌ๋ฌธ(์ฃ์ง ์ผ์ด์ค ์ฒ๋ฆฌ)
if(items == null || items.length === 0) return 0
// ๋ณ์๋ช
์์
let total = 0
items.forEach(item => {
total += item.price * item.quantity
})
total = total - total * (options.discount || 0)
total = total * TAX_RATE
// ๋ฐฐ์ก๋น ์ฒดํฌ
if(options.shipping !== 0) {
total = total + (options.shipping || SHIPPING_DEFAULT)
}
return total
}
const testItems = [
{ price: 15, quantity: 2 },
{ price: 20, quantity: 1 },
{ price: 5, quantity: 4 },
]
Step 3๏ธโฃ
const TAX_RATE = 1.1
const SHIPPING_DEFAULT = 5
// ๊ตฌ์กฐ ๋ถํด ํ ๋น(Destructuring)
// https://www.youtube.com/watch?v=NIq3qLaHCIs
funtion caculateTotal(items, { shipping = SHIPPING_DEFAULT, discount = 0 } = {}) {
if(items == null || items.length === 0) return 0
// ๋ฆฌ๋์ ํจ์ ์ฌ์ฉ
// https://www.youtube.com/watch?v=R8rmfD9Y5-c&t=387s
const itemCost = items.reduce((total, item) => {
return total + item.price * item.quantity
}, 0)
const discountRate = 1 - discount
// ์์(const) ์ฌ์ฉ - ์ค๊ฐ ๊ณ์ฐ(์์ ๋ณ์) ๊ฐ์
return itemCost * discountRate * TAX_RATE + shipping
}
const testItems = [
{ price: 15, quantity: 2 },
{ price: 20, quantity: 1 },
{ price: 5, quantity: 4 },
]
console.log(caculateTotal())
console.log(caculateTotal(testItems, {}))
console.log(caculateTotal([], {}))
console.log(caculateTotal(testItems, { ship: 0 }))
Last updated