实战

Claude Code 常用工作流大全:新功能开发、重构、测试、代码审查的最佳实践

Claude Code 常用工作流大全:6 个经过验证的完整流程——全流程功能开发、代码重构、测试覆盖补全、PR 代码审查、技术债清理、新人 Onboarding,含判断树帮你选对工作流。

2026/4/115分钟 阅读ClaudeEagle

这篇文章不讲基础操作,讲的是经过验证的工作流模式——从「功能需求到合并 PR」完整路径上每个节点,Claude Code 应该怎么用才最有效。


工作流 1:全流程功能开发

适合:从需求到实现的完整功能

第一步:先面试你

text
"I want to build a user notification system.
Interview me using AskUserQuestion tool.
Ask about delivery channels, read/unread tracking, types, real-time vs batch.
Keep asking until we've covered all edge cases, then write SPEC.md."

这一步的价值:把你脑子里模糊的想法变成精确的规格书,消灭「蝴蝶效应」的根源。

第二步:Plan Mode 分析影响

text
[切换到 plan 模式]
"Based on SPEC.md, analyze the implementation plan:
- Which files need to change
- New files to create
- Database schema changes
- API changes
- Estimated complexity"

确认方向没问题后再切换执行。

第三步:实现

text
"Implement the notification system per SPEC.md.
Follow patterns in @src/features/messaging/ for reference.
After each major step, run tests to verify."

第四步:测试和验证

text
"Write comprehensive tests for the notification system:
- Unit tests for NotificationService
- Integration tests for delivery
- Edge cases: empty recipient list, delivery failure, rate limiting
Run all tests and confirm they pass."

工作流 2:代码重构

适合:需要改动多个文件的重构

标准重构流程

text
# Step 1: 分析(plan 模式)
"Analyze impact of refactoring src/auth/ from callbacks to async/await:
- List all affected files
- Identify risky parts (especially concurrent operations)
- Propose step-by-step plan"

# Step 2: 确认方案没问题后开始执行
"Start with Step 1: refactor login.ts.
Run tests after each file to catch regressions early."

# Step 3: 渐进式推进,随时能停
"Continue with session.ts. Verify tests pass."

关键原则:每改一个文件就跑一次测试,不要等全部改完再测。早发现早修复。

大型迁移(JS → TS 等)

text
# 用 Fan-out 并行处理
"Create a migration plan for converting the entire src/ to TypeScript.
List all files, group by directory, propose parallel execution order.
Start with utils/ as it has fewest dependencies."

工作流 3:测试覆盖补全

适合:新增测试或补齐覆盖率

text
"Look at @src/api/payments.ts and @src/api/payments.test.ts.
The current tests cover happy paths but miss:
- What happens when payment processor returns timeout
- Concurrent duplicate payment requests
- Partial refund after full refund

Write tests for all three cases. Follow the existing test patterns."

好测试提示的特点

  • 引用现有测试文件(让 Claude 遵循风格)
  • 明确说出要覆盖的 edge case
  • 不要说「写更多测试」,要说「写这些具体场景的测试」

工作流 4:PR 代码审查

适合:在合并前发现问题

自审(用不同会话)

text
# 新开一个干净会话(不是写这段代码的那个会话)
"Review the changes in this PR: [git diff 或 @文件]
Focus on:
- Logic errors and edge cases
- Security vulnerabilities (injection, auth, sensitive data in logs)
- Race conditions in async code
- Consistency with existing patterns in @src/
List issues with file:line references and suggested fixes."

为什么要用不同的会话:在写代码的会话里 review,Claude 会有「自己孩子」的偏见,不够客观。干净会话没有这个问题。

安全专项审查

text
"Security review of @src/api/users.ts:
- Any SQL injection risks? (parameterized queries?)
- Are all endpoints properly authenticated?
- Is any sensitive data (passwords, tokens) logged?
- CSRF protection in place?
Report with severity (critical/high/medium/low) and fix suggestions."

工作流 5:技术债清理

适合:定期的代码质量改善

text
# 先分析,不动手(plan 模式)
"Analyze src/utils/ for code quality issues:
- Functions longer than 50 lines
- Duplicate logic across files
- Missing type annotations in TypeScript strict mode
- Unused exports

Create a prioritized list with effort estimates."

然后按优先级逐项处理,不要一次性全改。


工作流 6:新人 Onboarding

适合:快速熟悉陌生代码库

text
"I'm new to this codebase. Help me understand it:
1. What does this project do?
2. What's the overall architecture?
3. How does a typical user request flow through the system?
4. What are the key design patterns used?
5. What should I know before making any changes?"

这比看文档快得多,还能问跟进问题。


选对工作流的判断树

任务是新功能? └── 是 → 先 Interview + Plan → 然后实现 任务是改已有代码? └── 改多个文件? ├── 是 → Plan Mode 看影响范围 └── 否 → 直接执行,精确指向文件和行号 任务是找 bug? └── 位置明确? ├── 是 → @ 引用文件,先写失败测试 └── 否 → 用子 Agent 搜索,告诉它方向 任务是写测试? └── 引用现有测试 + 列出具体 edge case 任务是 review? └── 开新会话(保持上下文干净)

来源:Claude Code 官方 Common Workflows | morphllm.com | 整理:ClaudeEagle

相关文章推荐

实战Claude Code 子 Agent 实战:如何用多个 Agent 并行处理复杂任务Claude Code 子 Agent 实战指南:如何用多个独立 Agent 并行处理复杂任务。含 4 个实战示例、自定义 Agent 配置和成本优化建议。2026/4/7实战Claude Code CI/CD 完全集成指南:GitHub Actions 自动化代码审查与测试Claude Code 与 CI/CD 流水线完整集成教程:GitHub Actions 中非交互模式(claude -p)调用、PR 自动代码审查 Workflow、自动测试生成、构建失败时的 AI 诊断、安全扫描集成、Claude API Key 的 Secrets 管理、控制成本的模型选择策略(PR 审查用 Sonnet/失败诊断用 Haiku),以及 GitLab CI 和 Jenkins 的适配方案。2026/3/20实战AI 辅助 Code Review:用 Claude Code 让 PR 审查效率提升 3 倍用 Claude Code 做 AI 辅助代码审查完整指南:Pre-commit Hook 自动检查、PR Review 流程接入、自定义审查规则、与 GitHub Actions 集成、常见审查场景的 Prompt 模板,及人机协作最佳实践。2026/3/14实战Claude Code 重构遗留代码实战:安全、系统化的大规模改造策略Claude Code 重构遗留代码系统化指南:四步流程(侦察/补测试/分批改造/持续验证)、callback hell 到 async/await 完整示例、/batch 大规模并行重构、安全重构四大核心策略(先写测试/小步提交/保留旧接口/git bisect 定位问题)与优先级框架。2026/3/14实战Claude Code Hooks 实战:每次保存自动格式化、拦截危险命令、桌面通知Claude Code Hooks 实战教程:五个即用示例(桌面通知/文件自动格式化/危险命令拦截/压缩后上下文注入/配置变更审计)、Hook 配置位置(全局/项目/本地)、退出码含义(允许/上下文/阻止)、七大 Hook 事件速查表、Prompt-based AI 判断 Hook 进阶用法。2026/3/14实战Claude Code 日常工作流实战:探索代码库、Plan Mode、Git Worktree 并行开发与管道集成Claude Code 13 大日常工作流实战:五步探索陌生代码库(从宏观到微观的提示词序列)、Plan Mode 安全规划(--permission-mode plan/Shift+Tab 切换/配置为默认)、专用 Subagent 配置、TDD 测试工作流、Git Worktree 并行开发(-w flag 自动创建/两终端并行任务)、Unix 管道集成(git diff | claude -p/--output-format json/CI 成本控制),以及扩展思考模式和会话管理。2026/3/5