claude-code-usage

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Claude Code Best Practices

Claude编码最佳实践

Language Rule

语言规则

  • Always respond in the same language the user is using. If the user asks in Chinese, respond in Chinese. If in English, respond in English.
  • 始终使用与用户相同的语言回应。如果用户用中文提问,就用中文回应;如果用英文提问,就用英文回应。

Context Management Rules

上下文管理规则

RuleWhy
One window = one taskMixing tasks pollutes context and degrades output quality
Use
/clear
over
/compact
Clean start is more reliable than compressed context
/clear
after complex tasks
Prevents old context from interfering with new work
Copy key info to new windowsDon't rely on context persistence — paste critical details
规则原因
一个窗口对应一项任务混合任务会污染上下文,降低输出质量
优先使用
/clear
而非
/compact
全新开始比压缩上下文更可靠
复杂任务完成后使用
/clear
避免旧上下文干扰新工作
将关键信息复制到新窗口不要依赖上下文持久性——粘贴关键细节

Task Execution Strategy

任务执行策略

Task TypeRecommended Approach
Small bug fix (few lines)Describe directly, let Claude modify in-place
Large feature / refactor
/plan
→ review approach →
/clear
→ paste plan → execute step by step
Multi-file changesMust use
/plan
workflow — never modify multiple files without a plan
Code analysis / learningAsk Claude to analyze directly — no plan needed
DebuggingProvide error message + file path + relevant code — ask for root cause
任务类型推荐方案
小型bug修复(仅几行代码)直接描述问题,让Claude就地修改
大型功能开发 / 代码重构
/plan
→ 评审方案 →
/clear
→ 粘贴方案 → 逐步执行
多文件变更必须使用
/plan
工作流——绝不无计划地修改多个文件
代码分析 / 学习直接让Claude分析——无需制定计划
调试提供错误信息 + 文件路径 + 相关代码——询问根本原因

Prompt Techniques

提示词技巧

Do This

建议做法

  • Give specific paths: "Modify the
    _transfer
    function in
    src/MyToken.sol
    "
  • Give examples: "Input: 100 tokens, Expected: 95 tokens after 5% fee"
  • Set boundaries: "Only modify this function, don't touch other code"
  • Reference tests: "The fix should make
    test_transfer_feeDeduction
    pass"
  • 提供具体路径:"修改
    src/MyToken.sol
    中的
    _transfer
    函数"
  • 给出示例:"输入:100代币,预期:扣除5%手续费后剩余95代币"
  • 设置边界:"仅修改此函数,不要改动其他代码"
  • 参考测试用例:"修复需使
    test_transfer_feeDeduction
    测试通过"

Avoid This

避免做法

  • Vague references: "Modify that transfer function" — which one? Where?
  • Open-ended requests without constraints: "Make it better"
  • Multiple unrelated tasks in one message
  • 模糊指代:"修改那个转账函数"——哪一个?在哪里?
  • 无约束的开放式请求:"让它变得更好"
  • 一条消息中包含多个不相关任务

Git Operation Rules

Git操作规则

  • Always run
    git diff
    before committing to review changes
  • Only commit — do not push unless explicitly requested
  • Never push directly to main/master branch
  • Stage specific files — never use
    git add .
    in Solidity projects (risk of committing
    .env
    )
  • 提交前务必运行
    git diff
    来检查变更内容
  • 仅执行提交操作——除非明确要求,否则不要推送
  • 绝不直接推送到main/master分支
  • 暂存指定文件——在Solidity项目中绝不使用
    git add .
    (存在提交
    .env
    文件的风险)

Foundry-Specific Workflow

Foundry专属工作流

ActionCommand
Before committing
forge fmt && forge test
After modifying contracts
forge build
to check compilation
Before PR
forge test --gas-report
to check gas impact
Debugging failed test
forge test --match-test <name> -vvvv
for full trace
操作命令
提交前
forge fmt && forge test
修改合约后
forge build
检查编译情况
发起PR前
forge test --gas-report
检查Gas消耗影响
调试失败的测试用例
forge test --match-test <name> -vvvv
获取完整调用栈

Quick Command Reference

快速命令参考

CommandPurpose
/clear
Clear context, start fresh
/plan
Enter planning mode — analyze before modifying
/help
View all available commands
/compact
Compress context (prefer
/clear
instead)
命令用途
/clear
清空上下文,重新开始
/plan
进入规划模式——修改前先分析
/help
查看所有可用命令
/compact
压缩上下文(优先使用
/clear

Project-level Configuration

项目级配置

Create
.claude/instructions.md
in the project root with project-specific rules. Claude automatically reads it at the start of every conversation — no manual loading needed.
在项目根目录创建
.claude/instructions.md
文件,写入项目专属规则。Claude会在每次对话开始时自动读取该文件,无需手动加载。