Tạo tệp CLAUDE.md hiệu quả

Phát triển phần mềm với Claude Code

Dani Zysman

AI Curriculum Manager @ DataCamp

Thực hành tốt cho CLAUDE.md

  • Bắt đầu với /init, rồi lặp cải tiến
    • Bổ sung ngữ cảnh khi Claude mắc lỗi
    • Ghi lại thay đổi quy ước
  • Mục tiêu: Tài liệu sống, tiến hóa cùng dự án
Phát triển phần mềm với Claude Code

Dự án hiện tại

music-analytics-api/
  app.py           # Điểm vào ứng dụng
  routes.py        # Endpoint API
  models.py        # Model Artist/Album
  analytics.py     # Hàm thống kê
  middleware.py    # Ghi log request
  config.py        # Thiết lập phiên
  CLAUDE.md        # <- Tệp bộ nhớ dự án
Phát triển phần mềm với Claude Code

NÊN vs. KHÔNG NÊN trong CLAUDE.md

recraft: Nên đưa gì vào và bỏ gì khỏi CLAUDE.md

Quy tắc: Nếu dài hơn một tờ ghi chú nhỏ, đừng đưa vào.

Phát triển phần mềm với Claude Code

Lặp cải tiến CLAUDE.md

> Add error handling to the /artists endpoint.
Done. I've added error handling to /artists (routes.py:9-19):
- Try-except block wrapping retrieval and serialization
- AttributeError handler if the data store isn't initialized
- General exception handler returning error details with 500 status

return jsonify({'error': f'Failed to retrieve artists: {str(e)}'}), 500
> Don't expose exception details to users, that's a security risk. Log internally 
and return a generic message.  Update CLAUDE.md.
Updated /artists to log errors internally and return a 
generic 500 message. CLAUDE.md updated with the convention: 
never expose exception details in responses.
Phát triển phần mềm với Claude Code

CLAUDE.md tốt vs. cồng kềnh

Cồng kềnh (200+ dòng)

# Inventory API
This is an Inventory Management 
API built with Flask...

## Full Source Code
### routes.py
from flask import jsonify...
[entire file pasted]

### models.py
[entire file pasted]

## API Documentation
GET /items returns...

Tốt (15 dòng)

# Inventory API
Flask REST API cho quản lý tồn kho.

## Lệnh
- Chạy: flask run --port 5001
- Test: pytest tests/

## Quy ước
- Dùng type hints
- Giữ route gọn nhẹ
Phát triển phần mềm với Claude Code

Tham chiếu tệp - Dùng @

> @routes.py what endpoints does this API have?
API Endpoints (6 GET routes)

Artists
- GET /artists              All artists
- GET /artists/<id>         Single artist by ID
- GET /artists/<id>/albums  Albums for an artist
Albums
- GET /albums               All albums
- GET /stats/albums         Album popularity statistics
- GET /stats/average-tracks Average track count per album

All endpoints return JSON.
Phát triển phần mềm với Claude Code

Kết hợp CLAUDE.md + @

# CLAUDE.md - Music Analytics API
A Flask-based REST API for music data analytics, providing endpoints for artists,
albums, and statistics.
...

## Key Files @routes.py - API endpoint definition @models.py - Artist and Album data models with in-memory store ...

CLAUDE.md cung cấp ngữ cảnh bền vững. Hãy dùng @ để Claude luôn biết các tệp chính của bạn.

Phát triển phần mềm với Claude Code

Ba cấp độ ngữ cảnh

Ba cấp độ ngữ cảnh của CLAUDE.md

Phát triển phần mềm với Claude Code

Khi nào dùng mỗi loại

  • CLAUDE.md (dự án, dùng chung)

    • Lệnh test, quy ước code, cấu trúc dự án
  • CLAUDE.local.md (dự án, cá nhân)

    • Đường dẫn cục bộ, API key, thói quen làm việc cá nhân
  • ~/.claude/CLAUDE.md (toàn cục, cá nhân)

    • Sở thích về phong cách cho mọi dự án
    • Hành vi mặc định bạn luôn muốn
Phát triển phần mềm với Claude Code

Tóm tắt Chương 1

  • /init và CLAUDE.md → Ngữ cảnh dự án bền vững
  • Shift+Tab hoặc /plan → Bật/tắt Chế độ Plan
  • think ... ultrathink → Lập luận sâu (5 mức)
Phát triển phần mềm với Claude Code

Cùng luyện tập nào!

Phát triển phần mềm với Claude Code

Preparing Video For Download...