Nghệ Thuật Giao Tiếp Với AI Code Assistant
Prompt Engineering là kỹ năng số 1 bạn cần master trong Vibe Coding. Một prompt tốt có thể tiết kiệm hàng giờ làm việc, trong khi prompt kém sẽ dẫn đến vòng lặp sửa lỗi vô tận.
Framework C.O.D.E cho Prompting
C - Context (Bối cảnh): Luôn bắt đầu bằng việc cho AI biết bạn đang làm gì.
❌ BAD: "Tạo form đăng nhập"
✅ GOOD: "Trong dự án Next.js 14 với App Router,
tôi cần tạo form đăng nhập sử dụng react-hook-form
và zod validation. Backend dùng Supabase Auth."
O - Objective (Mục tiêu): Nói rõ bạn muốn đạt được điều gì.
✅ "Form cần: email validation, password min 8 ký tự,
loading state khi submit, error message hiển thị dưới input"
D - Details (Chi tiết): Cung cấp constraints và requirements cụ thể.
✅ "Sử dụng Shadcn/ui components, không dùng custom CSS.
Handle cả success và error states từ Supabase."
E - Examples (Ví dụ): Nếu có thể, đưa ví dụ về output mong muốn.
✅ "Tương tự form đăng nhập của Vercel dashboard:
minimalist, dark mode, với "Forgot password?" link"
Các Mẫu Prompt Hiệu Quả
1. Feature Implementation:
Implement [feature name] trong [file path].
Requirements:
- [Requirement 1]
- [Requirement 2]
Follow existing code patterns trong codebase.
2. Bug Fix:
Lỗi: [paste error message]
File: [file path]
Expected behavior: [mô tả]
Current behavior: [mô tả]
Fix lỗi này và giải thích nguyên nhân.
3. Refactoring:
Refactor [function/component name] để:
- Improve readability
- Better error handling
- Follow [specific pattern]
Giữ nguyên functionality, chỉ improve code quality.
Pitfalls Cần Tránh
| Sai Lầm | Hậu Quả | Cách Khắc Phục |
|---|---|---|
| Prompt quá dài | AI bỏ sót chi tiết | Chia nhỏ thành steps |
| Quá mơ hồ | Output không đúng ý | Cung cấp examples |
| Không context | AI đoán sai stack | Always mention tech stack |
| Yêu cầu nhiều việc | Partial completion | One task per prompt |
Pro Tips Từ Production
- Iterative Prompting: Bắt đầu đơn giản, thêm chi tiết dần
- Reference Files: "Tham khảo cách implement trong
src/components/Button.tsx" - Negative Constraints: "Không sử dụng any type, không console.log"
- Output Format: "Trả về chỉ code, không giải thích"
🔥 Rule of Thumb: Nếu bạn phải sửa output của AI nhiều hơn 2 lần, prompt của bạn cần cải thiện, không phải AI kém.
Bài Tập
Viết prompt để tạo một Toast notification system với các yêu cầu: success/error/warning variants, auto-dismiss sau 5 giây, có close button. So sánh output khi dùng prompt ngắn vs prompt chi tiết.
