Cách Sử Dụng Claude Code

Best practices cho AI-assisted development và cộng tác hiệu quả với Claude Code

Làm việc với AI là cộng tác, không phải ra lệnh. Claude Code là partner mạnh, nhưng như mọi partner -- làm việc hiệu quả đòi hỏi hiểu nó giỏi gì, cần human judgment ở đâu, và cách giao tiếp rõ ràng.

Nguyên tắc: AI là cộng tác viên, không phải thay thế. Judgment của mình định hình kết quả.

Claude Code đọc, viết, và suy luận về code được. Nhưng mình mới hiểu business context, user cần gì, và hậu quả dài hạn. Kết quả tốt nhất đến từ kết hợp cả hai.

Tại sao prompt bằng tiếng Anh

Dù handbook viết tiếng Việt, luôn prompt Claude Code bằng tiếng Anh:

  1. Training data: LLMs train chủ yếu trên data tiếng Anh, đặc biệt code và technical content. Prompt tiếng Anh = hiểu chính xác hơn
  2. Technical terms: Không cần dịch qua dịch lại. "API route", "middleware", "authentication" giữ nguyên nghĩa
  3. Code output: Comments, variable names, error messages bằng tiếng Anh -- consistent với codebase
  4. Community resources: Docs, Stack Overflow, GitHub issues -- tất cả tiếng Anh
  5. Ít mơ hồ: Tiếng Anh technical ít ambiguous hơn khi dịch sang tiếng Việt

Quy tắc: Prompt bằng tiếng Anh, đọc docs bằng tiếng Việt nếu thích, nhưng giao tiếp với AI luôn bằng tiếng Anh.

Nguyên tắc làm việc

1. Context trước, hành động sau

Chất lượng output phụ thuộc chất lượng input. Garbage in, garbage out.

Prompt "Thêm authentication" thì mơ hồ quá -- nhận được code generic không fit pattern của mình. Thay vào đó, giải thích đang build gì, constraint nào, pattern nào cần follow. Chỉ đến code có sẵn làm ví dụ.

Cần bao gồm:

  • Đang cố đạt được gì?
  • Codebase hiện tại trông thế nào?
  • Có pattern hoặc file nào để tham chiếu?
  • Constraint quan trọng? (không dependency mới, giữ đơn giản, etc.)
Add authentication to this NextJS app. Use better-auth since it's already in the project.
Follow the pattern in src/lib/auth.ts. Create middleware to protect /dashboard/* routes.
No new dependencies. Keep it simple.

2. Plan trước khi build

Với task không trivial, thảo luận approach trước rồi hãy viết code. Nhảy thẳng vào implementation thì code chạy được nhưng không fit architecture.

Quy trình:

  1. Mô tả muốn đạt được gì
  2. Hỏi options hoặc approach
  3. Thảo luận trade-offs
  4. Đồng ý plan
  5. Rồi mới implement
I want to add CSV export for keyword clustering results.
Before coding, propose an approach. Consider:
- File size can be large (10k+ keywords)
- User needs immediate download, no email wait
- Currently using NextJS API routes

3. Làm việc incremental

Chia task lớn thành bước nhỏ. Verify từng bước trước khi tiếp.

Yêu cầu cả feature một lần thì nhận được change khổng lồ khó review, khó debug, có thể sai theo cách không nhận ra. Thay vào đó:

Step 1: "Create API route POST /api/export that takes job_id, returns CSV string"
[Test API route]

Step 2: "Add Export button to results page, call the API route we just created"
[Test button]

Step 3: "Trigger file download when response is received"
[Test full flow]

4. Verify, đừng tin

AI có thể sai rất tự tin. Code trông đúng không có nghĩa là chạy đúng.

Luôn: chạy code, check build, chạy tests, review logic, test edge cases.

Tự hỏi:

  • Có thực sự làm điều mình yêu cầu không?
  • Có follow pattern hiện tại không?
  • Có bug hoặc edge case rõ ràng không?
  • Độ phức tạp có phù hợp không?

5. Iterate và refine

Lần đầu hiếm khi hoàn hảo -- bình thường. Coi output AI là điểm bắt đầu:

"This works, but can you simplify it?"
"Approach is fine, but use pattern X instead of Y"
"This handles the happy path, but what about case Z?"

Nghĩ như pair programming. Mình guide, refine, quyết định. Claude Code execute và suggest.

Workflow cộng tác

┌─────────────────────────────────────────────────────────────┐
│                        PLANNING                              │
│  Thảo luận requirements, explore options, design approach   │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│                      IMPLEMENTATION                          │
│  Viết code từng bước, follow patterns, tạo tests            │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│                        REVIEW                                │
│  Check issues, verify behavior, refactor nếu cần            │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│                       DOCUMENT                               │
│  Update notes, commit với message rõ ràng                   │
└─────────────────────────────────────────────────────────────┘

Khi nào dùng AI

Phù hợp:

  • Hiểu code có sẵn: "What does src/lib/clustering.ts do? Explain the main flow"
  • Follow pattern đã có: "Create API route /api/jobs similar to /api/users"
  • Task routine: boilerplate, tests, migrations
  • Khám phá: "What are the options for implementing real-time updates?"
  • Debug: "Why is this useEffect running in an infinite loop?"
  • Refactor: "Simplify this function, keep the same behavior"

Cần human judgment:

  • Quyết định architecture (AI suggest, mình quyết định)
  • Code liên quan security (luôn review kỹ)
  • Validate business logic (có đúng user cần không?)
  • Yêu cầu performance (AI không biết constraints của mình)
  • Quyết định UX (AI không thấy users của mình)

Không phù hợp:

  • Task cần thông tin real-time
  • Quyết định cần context tổ chức
  • Công việc sáng tạo cần distinctly human

Cách đưa instruction tốt

Cụ thể về scope:

Bad:  "Add a feature"
Good: "Add CSV Export button to keywords table,
       export columns: keyword, rank, date"

Chỉ đến ví dụ:

"Follow the pattern in /api/users/route.ts"
"Use the existing Button component from /components/ui"

Nêu constraints:

"No new dependencies"
"Keep it under 100 lines"
"Must work with existing auth"

Rõ ràng về output:

"Just the implementation" vs "Explain your approach first"
"Step by step, wait for confirmation" vs "Complete the whole thing"

Khi nào hỏi giải thích

Trước implementation khi: không chắc approach, task phức tạp hoặc lạ, có nhiều approach valid, security hoặc performance quan trọng.

"I want to implement caching for API calls.
Explain the options first: in-memory vs Redis vs SWR.
What are the trade-offs of each in a NextJS app context?"

Sau implementation khi: code làm gì đó không hiểu, muốn học pattern, đang quyết định giữ approach hay đổi.

Xử lý khi sai

Output sai:

"This is wrong at X. I expected Y instead of Z.
Additional context: [more info]"

Stuck trong loop: lùi lại, reframe vấn đề, đưa constraints cụ thể hơn, thử approach khác.

Hướng đi sai:

"Stop. This approach is wrong because [reason].
Start over with a different direction: [description]"

Restart rẻ hơn sửa foundation tệ.

Sai lầm thường gặp

Sai lầmDấu hiệuCách sửa
Chấp nhận mà không hiểuKhông giải thích được code làm gì. Bug thì bí ẩnHỏi giải thích. Không hiểu thì học hoặc simplify
Scope quá lớnChanges khổng lồ, lỗi khó nhận ra, lạc trong complexityChia nhỏ. Một feature, một file một lần nếu cần
Intent không rõNhận thứ khác mong đợi, back-and-forth nhiềuCụ thể. Đưa ví dụ. Nêu constraints
Bỏ qua verificationBug mà test cơ bản đã bắt được. Build vỡLuôn chạy code, check build, chạy tests
Đánh nhau với toolBực bội, thử nhiều lần kết quả tệNhận ra AI giỏi gì và gì cần human judgment

AI giỏi gì

TaskHiệu quả
Hiểu code có sẵnXuất sắc
Follow pattern đã cóXuất sắc
Refactor multi-fileRất tốt
DebugRất tốt
Viết feature mới (có guidance)Rất tốt
Code reviewTốt
Design architectureTốt (có guidance)
Viết testsTốt

Gì cần human judgment

  • Quyết định architecture cuối cùng
  • Review code security-sensitive
  • Validate business logic
  • Quyết định deploy production
  • Yêu cầu performance
  • Quyết định UX
  • Context tổ chức
  • Cân nhắc đạo đức

Checklist

Cộng tác đang ổn nếu:

  • Hiểu tất cả code đang được viết
  • Code follow pattern hiện có
  • Bắt được issues trước khi merge
  • Tốn ít thời gian hơn cho task routine
  • Học được từ các giải thích

Cần điều chỉnh nếu:

  • Chấp nhận code không hiểu
  • Code không fit style của project
  • Bug lọt qua
  • Phải rewrite nhiều
  • Sửa output AI tốn thời gian hơn tự viết

Tham khảo nhanh

Mình muốn...Prompt
Plan feature"Help me plan [X]. What's the best approach given [constraints Y]?"
Hiểu code"Explain how [file/function] works"
Fix bug"Investigate [error]. What's the root cause?"
Refactor"Simplify this, keep the same behavior"
Review code"Review for bugs, security, and performance issues"
Viết tests"Add tests for [X] covering edge cases"
Học codebase"Give me an overview of this project"
Thêm feature"Add [feature] following the pattern in [existing file]. Constraints: [list]"
Debug"Why is [behavior] happening? Check [file] and suggest a fix"
Optimize"[Function] is slow. Profile and suggest optimizations"