← 教程列表
coding

Anti-Pattern 2: Test-Only Methods in Production

📅 2026/6/12 16:30:02⏱ 约 5 分钟阅读👁 7 阅读

**The violation:** ```typescript // ❌ BAD: destroy() only used in tests class Session { async destroy() { // Looks like production API! await this._workspaceManager?.destroyWorkspace(this.id); // ... cleanup } } // In tests afterEach(() => session.destroy()); ``` **Why this is wrong:** - Production class polluted with test-only code - Dangerous if accidentally called in production - Violates YAGNI and separation of concerns - Confuses object lifecycle with entity lifecycle **The fix:** ```typescript // ✅ GOOD: Test utilities handle test cleanup // Session has no destroy() - it's stateless in production // In test-utils/ export async function cleanupSession(session: Session) { const workspace = session.getWorkspaceInfo(); if (workspace) { await workspaceManager.destroyWorkspace(workspace.id); } } // In tests afterEach(() => cleanupSession(session)); ```

← 返回教程列表 AI8899 · 内容仅供学习参考