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