打造高效的 CLAUDE.md 檔案

使用 Claude Code 進行軟體開發

Dani Zysman

AI Curriculum Manager @ DataCamp

CLAUDE.md Best Practices

  • 先用 /init,再不斷迭代
    • Claude 出錯時加上脈絡
    • 記錄慣例變更
  • 目標:隨專案演進而更新的活文件
使用 Claude Code 進行軟體開發

目前專案概況

music-analytics-api/
  app.py           # Application entry point
  routes.py        # API endpoints
  models.py        # Artist/Album models
  analytics.py     # Statistics functions
  middleware.py    # Request logging
  config.py        # Session settings
  CLAUDE.md        # <- Project memory file
使用 Claude Code 進行軟體開發

CLAUDE.md 的取捨:該放與不該放

recraft:CLAUDE.md 該寫什麼、該省略什麼

原則:超過便條紙長度的內容,就不該放。

使用 Claude Code 進行軟體開發

迭代更新 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.
使用 Claude Code 進行軟體開發

良好 vs. 冗長的 CLAUDE.md

冗長(200+ 行)

# 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...

良好(15 行)

# Inventory API
Flask REST API for inventory.

## Commands
- Run: flask run --port 5001
- Test: pytest tests/

## Conventions
- Use type hints
- Keep routes thin
使用 Claude Code 進行軟體開發

檔案引用 - 使用 @

> @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.
使用 Claude Code 進行軟體開發

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 提供持久脈絡。使用 @,讓 Claude 隨時知道你的關鍵檔案。

使用 Claude Code 進行軟體開發

三個層級的脈絡

CLAUDE.md 脈絡的三個層級

使用 Claude Code 進行軟體開發

各自適用時機

  • CLAUDE.md(專案、共用)

    • 測試指令、程式慣例、專案結構
  • CLAUDE.local.md(專案、個人)

    • 本機路徑、API 金鑰、個人流程偏好
  • ~/.claude/CLAUDE.md(全域、個人)

    • 各專案通用的風格偏好
    • 一律想啟用的預設行為
使用 Claude Code 進行軟體開發

第 1 章重點回顧

  • /init 與 CLAUDE.md → 持久的專案脈絡
  • Shift+Tab 或 /plan → 切換規劃模式
  • think ... ultrathink → 深度推理(5 個等級)
使用 Claude Code 進行軟體開發

一起來練習吧!

使用 Claude Code 進行軟體開發

Preparing Video For Download...