返回 Skill 列表
extension
分类: 开发与工程无需 API Key

clean-codejs-functions

强调单一职责和清晰性的函数设计模式

person作者: jakexiaohubgithub

Clean Code JavaScript – Function Patterns

Table of Contents

  • Single Responsibility
  • Function Size
  • Parameters
  • Side Effects

Single Responsibility

// ❌ Bad
function handleUser(user) {
  saveUser(user);
  sendEmail(user);
}

// ✅ Good
function saveUser(user) {}
function notifyUser(user) {}

Function Size

Keep functions small (ideally < 20 lines).

Parameters

// ❌ Bad
function createUser(name, age, city, zip) {}

// ✅ Good
function createUser({ name, age, address }) {}

Side Effects

// ❌ Bad
let total = 0;
function add(value) {
  total += value;
}

// ✅ Good
function add(total, value) {
  return total + value;
}