Loading...
Loading...
Compare original and translation side by side
NEVER add functionality that introduces a second reason to change.绝不添加会引入第二个变更理由的功能。| Description | Verdict |
|---|---|
| "Handles user authentication" | ✅ Single responsibility |
| "Handles authentication AND sends emails" | ❌ Two responsibilities |
| "Manages orders AND processes payments AND tracks inventory" | ❌ Three responsibilities |
| 描述 | 结论 |
|---|---|
| “处理用户认证” | ✅ 单一职责 |
| “处理认证和发送邮件” | ❌ 两个职责 |
| “管理订单、处理支付和跟踪库存” | ❌ 三个职责 |
// ❌ BAD: UserManager - 4 reasons to change
class UserManager {
login() {} // Auth logic changes
updateProfile() {} // Profile requirements change
sendEmail() {} // Email provider changes
trackAnalytics() {} // Analytics requirements change
}
// ✅ GOOD: Split by responsibility
class AuthService { login() {} }
class ProfileService { updateProfile() {} }
class NotificationService { sendEmail() {} }
class AnalyticsService { track() {} }// ❌ 错误示例:UserManager - 4个变更理由
class UserManager {
login() {} // 认证逻辑变更
updateProfile() {} // 个人资料需求变更
sendEmail() {} // 邮件服务商变更
trackAnalytics() {} // 数据分析需求变更
}
// ✅ 正确示例:按职责拆分
class AuthService { login() {} }
class ProfileService { updateProfile() {} }
class NotificationService { sendEmail() {} }
class AnalyticsService { track() {} }"I'd recommend splitting this because [specific reason].
If we keep it together, we'll likely need to refactor when [consequence].
Should I proceed with the split, or document this as tech debt?"“我建议拆分这个类,因为[具体原因]。
如果我们保留在一个类里,当[后果]发生时,我们可能需要重构。
我是应该进行拆分,还是将此记录为技术债务?”// Found: OrderService with 500 lines handling orders, payments, inventory, emails
// ❌ WRONG: Add shipping logic to OrderService
// ✅ RIGHT: Create ShippingService, note that OrderService needs refactoring// 现状:OrderService有500行代码,处理订单、支付、库存、邮件
// ❌ 错误:向OrderService添加物流逻辑
// ✅ 正确:创建ShippingService,记录OrderService需要重构| Excuse | Reality |
|---|---|
| "It's faster to put it in one class" | It's not. You type the same code either way. |
| "Small classes are over-engineering" | Small classes are correct engineering. |
| "It's just one more method" | That's how god classes start. Every time. |
| "We can refactor later" | You won't. Tech debt compounds. |
| "The class is already big" | That's a reason to stop, not continue. |
| "It's related functionality" | Related ≠ same responsibility. |
| "Section comments help navigate" | If you need navigation, class is too big. |
| 借口 | 真相 |
|---|---|
| “放在一个类里更快” | 并没有。两种方式敲的代码量一样。 |
| “小类是过度设计” | 小类是正确的工程设计。 |
| “只是多一个方法而已” | 全能类都是这么开始的。每次都是。 |
| “我们以后可以重构” | 你们不会的。技术债务会不断累积。 |
| “这个类已经很大了” | 这是停止的理由,不是继续的理由。 |
| “这是相关功能” | 相关≠同一职责。 |
| “分段注释有助于导航” | 如果需要导航,说明类太大了。 |
| Symptom | Action |
|---|---|
| Class does X AND Y | Split into XService and YService |
| Adding unrelated method | Create new class |
| File > 200 lines | Look for extraction opportunities |
| Multiple reasons to change | One class per reason |
| "Manager/Handler/Processor" name | Be more specific or split |
| 症状 | 行动 |
|---|---|
| 类做X和Y | 拆分为XService和YService |
| 添加不相关方法 | 创建新类 |
| 文件超过200行 | 寻找提取机会 |
| 多个变更理由 | 一个理由对应一个类 |
| 类名是“Manager/Handler/Processor” | 更具体命名或拆分 |