Clean Code

🔗 Junior Vs Senior Code - How To Write Better Code - Part 2를 보고 정리한 내용입니다.

🔗 Noob-Vs-Pro-Code - Github

3. Async/Await

Step 1️⃣

  • 콜백 함수 - 콜백이 계속 중첩되는 🔥콜백 지옥🔥

const readline = require("readline")
const readlineInterface = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})

readlineInterface.question("What is your name? ", name => {
  readlineInterface.question("What is your job? ", job => {
    readlineInterface.question("How old are you? ", age => {
      console.log("Hello " + name + ". You are a " + age + " year old " + job + ".")
      readlineInterface.close()
    })
  })
})

Step 2️⃣

  • Async/Await - 콜백이 중첩되는 문제 해결

Step 3️⃣

  • readlineInterface 리팩토링, 함수 분리 - 모듈화

4. Single Responsibility Principle(SRP, 단일 책임 원칙)

SOLID: Single Responsibility Principle in JavaScript

"class(a.k.a module) 은 오직 한 가지 이유 때문에 변경되어야 한다."

  • Robert C. Martin

    • SRP는 객체 지향 언어에 국한되어 있지 않고, 프로토타입 상속을 하는 자바스크립트에도 적용된다.

    • 하나의 class는 오직 한 가지 책임만 가져야 한다(단일 책임 원칙)

    • Employee class는 SRP 원칙에 따라 여러 class로 분리되어야 한다.

Step 1️⃣

  • 의도하지 않은 🚫side effects🚫 존재

Step 2️⃣

  • 기능별 함수 분리(validateUser, saveUser)

  • 💣 validation 관련 코드 반복(비효율적)

Step 3️⃣

  • 🔑 SRP(단일 책임 원칙) 충족하도록 validation 관련 코드 분리

Last updated