taw·lmsĐăng nhập
Claude Code thực chiến — best practices từ Anthropic

Bài 1 · Best practices từ Anthropic Engineering

Best practices Claude Code — chính thống từ Anthropic

Bài học này chưa có video. Cứ đọc nội dung phía dưới nhé.

Constraint quan trọng nhất

Context window đầy nhanh, performance giảm khi context đầy.

Mọi best practice xoay quanh điều này.

1. Cho Claude cách tự verify

"This is the single highest-leverage thing you can do."

TrướcSau
"implement a function that validates email""write a validateEmail function. test cases: [email protected] → true, invalid → false. RUN tests after implementing"
"make dashboard look better""[paste screenshot] implement this design. take a screenshot of result, compare to original, list differences and fix"
"the build is failing""build fails with: [paste error]. fix and verify build succeeds. ADDRESS ROOT CAUSE, don't suppress"

Đầu tư vào verification (test suite, linter, bash check) — Claude tự sửa, bạn không phải làm feedback loop.

2. Explore → Plan → Code → Commit

4 phase rõ rệt:

  1. Explore (plan mode) — read /src/auth and understand how we handle sessions
  2. Plan (plan mode) — What files need to change? Create a plan. (Ctrl+G để edit plan)
  3. Implement (default mode) — implement the OAuth flow from your plan
  4. Commit — commit with descriptive message and open a PR

Khi nào skip plan? Task nhỏ (typo, log line, rename) — chỉ cần 1 câu mô tả diff.

3. Specific context — đừng vague

VagueSpecific
"add tests for foo.py""write test for foo.py covering edge case where user logged out. avoid mocks."
"fix the login bug""users report login fails after session timeout. check src/auth/, especially token refresh. write a FAILING test that reproduces, then fix"
"add a calendar widget""look at HotDogWidget.php for pattern. follow it. user selects month, paginate forward/back to pick year. no new libraries."

4. CLAUDE.md — pruning kỷ luật

Mỗi line, hỏi: "Removing this would cause Claude to make mistakes?" Nếu không → cắt.

✅ Include❌ Exclude
Bash command Claude không đoán đượcCái Claude tự đọc code biết
Code style khác defaultConvention chuẩn ai cũng biết
Test runner ưu tiênAPI doc dài (link tới doc)
Branch naming, PR conventionInfo thay đổi liên tục
Architectural decision đặc thùMô tả từng file
Dev env quirks (env var cần)"Write clean code"

Dấu hiệu CLAUDE.md quá dài: Claude bỏ qua rule, hoặc hỏi lại câu đã trả lời sẵn trong CLAUDE.md. → prune.

5. Course-correct sớm và thường xuyên

  • Esc — dừng Claude giữa chừng, context giữ nguyên
  • Esc Esc hoặc /rewind — quay về checkpoint cũ
  • "Undo that" — Claude tự revert
  • /clear — reset context giữa task khác nhau

Quy tắc 2 lần sửa: nếu correct 2 lần cùng issue → /clear + viết prompt tốt hơn. Long session với failed attempts kéo Claude xuống.

6. Tránh failure pattern

  • Kitchen sink session — nhiều task không liên quan trong 1 conversation → /clear
  • Over-specified CLAUDE.md — rule chính bị nhấn chìm trong noise → prune
  • Trust-then-verify gap — code "trông đúng" mà không verify → luôn có test/script check
  • Infinite exploration — "investigate X" mà không scope → dùng subagent

Nguồn chính thống đầy đủ: code.claude.com/docs/en/best-practices

Bạn đang xem ở chế độ preview công khai. Để nộp bài tập, hỏi AI tutor, và làm quiz — đăng nhập.
Auto mode — Claude tự quyết, ít hỏi hơn (an toàn)→