Back

Single Responsibility

Each function or component should do one thing. If you need the word "and" to describe what it does, it probably should be split up. Small, focused pieces are easier to read, test, and reuse.

// Bad: does three things
function handleSignup(data) {
  if (!data.email.includes("@")) throw new Error("Bad email")
  db.users.insert(data)
  sendEmail(data.email, "Welcome!")
}

// Good: each function has one job
function validateUser(data) {
  if (!data.email.includes("@")) throw new Error("Bad email")
}
function saveUser(data) {
  return db.users.insert(data)
}
function sendWelcome(email) {
  return sendEmail(email, "Welcome!")
}

function handleSignup(data) {
  validateUser(data)
  saveUser(data)
  sendWelcome(data.email)
}

Roadmap