学习中心/Claude Code/Claude Code 核心开发工作流
入门

Claude Code 核心开发工作流

掌握探索代码、修复 Bug、重构、写测试、Git 工作流等日常开发场景

Anthropic30 min

学习笔记

Claude Code 核心开发工作流

Claude Code 的核心价值在于无缝融入你的日常开发流程。本课程覆盖最常见的开发场景:探索代码库、定位和修复 Bug、重构代码、编写测试、管理 Git 工作流。

你将学到

  • 四阶段开发流程:Explore → Plan → Implement → Commit
  • 如何让 Claude 探索和理解不熟悉的代码库
  • 高效的 Bug 修复和调试流程
  • 自动化测试编写和 Git 工作流

适合谁

  • 已安装 Claude Code、想快速上手日常使用的开发者
  • 希望建立 AI 辅助开发习惯的工程师

1. 推荐工作流:Explore → Plan → Implement → Commit

Anthropic 官方推荐的四阶段工作流,适用于大多数开发任务:

Phase 1: Explore(探索)

让 Claude 先阅读和理解相关代码:

explain the architecture of this project

how does the payment module handle refunds?

what files are involved in user authentication?

Phase 2: Plan(计划)

Shift+Tab 进入 Plan Mode,让 Claude 制定方案而不直接修改代码:

I want to add rate limiting to the API endpoints.
Analyze the current middleware stack and propose an implementation plan.

Claude 会列出:需要修改的文件、实施步骤、可能的影响。你可以在编辑器中调整方案。

Phase 3: Implement(实施)

确认方案后,切回 Normal Mode(再按 Shift+Tab),让 Claude 执行:

好的,按照这个方案实施

Phase 4: Commit(提交)

commit these changes with a descriptive message

Claude 会自动:暂存文件、生成描述性的 commit message、执行提交。


2. 探索不熟悉的代码库

当你接手一个新项目时,Claude Code 是最好的向导:

全局概览

give me a high-level overview of this project's architecture

what frameworks and libraries does this project use?

draw an ASCII diagram of the module dependencies

定位特定逻辑

where is the database connection configured?

which files handle API routing?

how does the error handling middleware work?

追踪数据流

trace the flow of a user login request from the API endpoint to the database

what happens when a webhook is received at /api/webhooks?

3. 修复 Bug

给出错误信息

最高效的方式是直接粘贴错误信息:

I'm getting this error:
TypeError: Cannot read properties of undefined (reading 'map')
at UserList (src/components/UserList.tsx:23:15)

Fix it.

自动化修复流程

让 Claude 一条龙完成:

write tests for the auth module, run them, and fix any failures

Claude 会:

  1. 分析模块代码
  2. 编写测试用例
  3. 运行测试
  4. 修复失败的测试
  5. 重新运行确认全部通过

调试技巧

add console.log statements to trace the data flow in processPayment()

the API returns 500 but only in production. analyze possible causes.

关键提示:如果 Claude 连续两次未能修复同一个 Bug,最好用 /clear 清理上下文,用更精确的描述重新开始。


4. 编写测试

write unit tests for src/utils/validator.ts

add integration tests for the user registration flow

write tests for this function:
- 'user@example.com' -> true
- 'invalid' -> false
- 'user@.com' -> false
Run the tests after.

给 Claude 明确的测试用例能显著提升结果质量。


5. 重构代码

refactor the database queries in UserService to use the repository pattern

extract the validation logic from the controller into a separate middleware

convert this callback-based code to async/await

技巧:重构时使用 Plan Mode,先让 Claude 列出所有受影响的文件和修改步骤,确认后再执行。


6. Git 工作流

提交变更

commit my changes with a descriptive message

创建 PR

create a pull request with a summary of all changes, including test coverage

Claude 会自动草拟 PR 描述,包含实现细节和测试说明。

查看变更

show me what changed since the last commit

summarize the changes in the last 5 commits

代码审查

review the changes in this PR for potential issues

check if there are any security concerns in the recent changes

7. 会话管理

恢复会话

# 继续上一次会话
claude --continue

# 选择历史会话
claude --resume

# 从 PR 恢复
claude --from-pr 123

上下文管理

/clear       # 清理上下文(切换任务时使用)
/compact     # 压缩历史(保留摘要,释放空间)
/context     # 查看上下文使用量

建议:在上下文使用量接近 60% 时主动执行 /compact,不要等到自动压缩(~80%)。


小结

  • Explore → Plan → Implement → Commit 是最高效的工作流
  • 探索代码时充分利用自然语言提问
  • 修复 Bug 时直接粘贴错误信息,给 Claude 充足上下文
  • 编写测试时提供具体的测试用例
  • 善用 /clear/compact 管理上下文
  • 两次失败后重新开始,不要在一个方向反复尝试

延伸阅读

内容来源:Anthropic 官方文档