# 常用命令 - Dev: `npm run dev` - Tests: `npm run test` - Build: `npm run build`
多个CLAUDE.md文件
对于大型项目,可以创建更具体的配置文件:
项目根目录:通用CLAUDE.md
子目录:/frontend/CLAUDE.md、/backend/CLAUDE.md等
这样可以让Claude在不同上下文中获得更专注的指导。
实际效果
使用CLAUDE.md后:
✅ 代码首次尝试通过linting
✅ 新团队成员快速上手
✅ 代码审查周期缩短40%
✅ 消除对约定的争论
技巧2:Plan Mode——探索、规划、执行、提交
问题:直接开始编码看起来很快,但几乎总是导致后续痛苦。
解决方案:使用”Plan then Execute”工作流。
三阶段工作流
阶段1:探索(Explore)
1 2 3 4 5
"Act as a Senior Architect. Before proposing implementation: 1. Analyze existing codebase in /src/components/ 2. Identify all files affected by adding user authentication 3. List integration points and dependencies 4. Note architectural constraints"
关键:告诉Claude不要写代码。用明确指令:
1
"Present as numbered checklist. Do NOT write code yet."
阶段2:规划(Plan)
1 2 3 4 5
"Draft a detailed implementation plan for JWT authentication. Include: - Exact file paths for new/modified files - Function signatures that need changes - Database schema changes - Testing strategy"
阶段3:执行(Execute)
只有批准计划后才开始编码:
1
"Present as numbered checklist. Now write code."
切换技巧
使用Shift + Tab键在Plan Mode和正常模式之间切换。
为什么规划可以预防痛苦?
50%更少的重构:在规划阶段发现问题比在代码审查中便宜10倍
更好的文档:计划本身就成了PR描述
渐进式进展:将功能分解为计划步骤,减少不知所措
真实案例
重构任务:
1 2 3 4 5 6
❌ 错误方式:"Refactor user authentication module" ✅ Plan Mode方式: "Analyze src/auth/, src/components/, src/api/" "Identify 10 files affected" "Create refactoring plan" "Get approval, then implement"
结果:4小时重构任务,第一次尝试就完全符合规范。
技巧3:使用/agents——专业化与上下文隔离
问题:上下文污染
在一个Claude会话中处理所有事情会导致:
调试对话污染架构决策
长会话变慢
频繁的上下文丢失
解决方案:创建专门的子代理
1 2 3 4 5 6 7 8 9 10 11 12 13
# 创建调试代理 /agents create debugger System Prompt: You're a Debugger Agent. Only identify and fix bugs. # 创建安全代理 /agents create security System Prompt: Review code exclusively for security vulnerabilities. Flag: SQL injection, XSS, auth flaws, data exposure, insecure dependencies. # 创建前端代理 /agents create frontend System Prompt: React/TypeScript/Tailwind specialist. Focus: Component architecture, state, performance, accessibility.
### The Full Context Framework 1. Complete Error with Stack Trace Error: Cannot read property 'user' of undefined at UserProfile.render (UserProfile.tsx:45:23) at processChild (react-dom.development.js:3991:18) [full stack trace...]
步骤1:通过测试定义需求 "Using Jest, write FAILING tests for a ShoppingCart component: - Display empty state when no items - Show correct item count badge - Calculate total with 8.5% tax - Apply 'SUMMER10' discount (10% off) - Handle removing items - Persist to localStorage"
步骤2:AI生成测试,人类审查 "Review Tests Claude generates comprehensive tests. You check for edge cases and realistic scenarios."
步骤3:实现以通过测试 "Now implement ShoppingCart to make all tests pass."
步骤4:验证覆盖率 "Run tests and show coverage. Missing scenarios?"