วิธีเขียน Prompt สำหรับ AI Coding ที่ใช้ได้จริง เขียนโค้ดแม่น ลดการแก้ซ้ำ เหมาะทั้งมือใหม่และคนทำโปรเจ็กต์จริง
หลักคิดก่อนเขียน Prompt (สำคัญมาก)
AI เขียนโค้ดได้ดีแค่ไหน = ความชัดของโจทย์ × บริบทที่ให้
❌ Prompt แย่ → โค้ดมั่ว
✅ Prompt ดี → โค้ดใช้ได้ทันที
🧩 โครงสร้าง Prompt AI Coding (สูตรมาตรฐาน)
ใช้สูตรนี้เสมอ 👇
[ROLE]
[GOAL]
[CONTEXT]
[CONSTRAINTS]
[OUTPUT FORMAT]
1️⃣ ROLE – บอกให้ AI เป็นใคร
ช่วยปรับ “วิธีคิด” ของ AI
ตัวอย่าง
- “คุณคือ Senior Backend Developer”
- “คุณคือ Frontend Engineer ที่เชี่ยวชาญ React”
- “คุณคือ AI Coding Assistant ที่เน้น Clean Code”
You are a senior backend developer.
2️⃣ GOAL – ต้องการให้ทำอะไร (ชัดเจนที่สุด)
อย่าเขียนกว้าง ๆ
❌ “เขียน API ให้หน่อย”
✅ “เขียน REST API สำหรับระบบ Login”
Your task is to create a REST API for user authentication.
3️⃣ CONTEXT – บริบทของระบบ (สำคัญมาก)
AI ต้องรู้ว่าโค้ดจะไปอยู่ตรงไหน
ระบุ:
- ภาษา
- Framework
- Database
- Environment
Context:
- Language: Node.js (TypeScript)
- Framework: Express
- Database: PostgreSQL
- Auth: JWT
4️⃣ CONSTRAINTS – เงื่อนไขที่ “ห้ามพลาด”
ช่วยให้โค้ดตรงมาตรฐานที่คุณต้องการ
ตัวอย่าง:
- ใช้ Clean Architecture
- แยก service / controller
- ห้ามใช้ library บางตัว
- ต้องรองรับ error handling
Constraints:
- Follow clean architecture
- Separate controller, service, and repository
- Use async/await
- Handle errors properly
5️⃣ OUTPUT FORMAT – อยากได้ผลลัพธ์แบบไหน
ช่วยลดการแก้ prompt ซ้ำ
Output format:
- Show folder structure
- Provide complete code files
- Add brief comments in code
✅ Prompt ตัวอย่าง (ใช้ได้จริง)
🔹 ตัวอย่าง 1: เขียน API
You are a senior backend developer.
Create a REST API for user login.
Context:
- Language: Node.js (TypeScript)
- Framework: Express
- Database: PostgreSQL
- Authentication: JWT
Constraints:
- Clean architecture
- Separate controller, service, repository
- Password must be hashed
- Proper error handling
Output format:
- Folder structure
- Full code for each file
🔹 ตัวอย่าง 2: แก้บั๊ก
You are a debugging expert.
Fix the bug in the following code and explain why it happens.
Context:
- Language: Python
- Framework: FastAPI
Constraints:
- Do not change the API interface
- Keep the fix minimal
Output format:
- Fixed code
- Explanation
🔹 ตัวอย่าง 3: Refactor โค้ด
You are a senior software architect.
Refactor this code to improve readability and performance.
Constraints:
- Follow SOLID principles
- No breaking changes
Output format:
- Refactored code
- List of improvements
🚀 Prompt ขั้นสูง (AI Coding ระดับโปร)
🔥 Prompt แบบ “คิดเป็นขั้นตอน”
Think step by step before writing code.
Explain the design first, then write the implementation.
🔥 Prompt สำหรับ Vibe Coding / Agile
Act as an AI pair programmer.
Ask clarification questions if requirements are unclear.
Iterate in small steps.
🔥 Prompt สำหรับระบบใหญ่
First design the system architecture.
Then generate code module by module.
❌ ข้อผิดพลาดที่พบบ่อย
- ไม่บอกภาษา / framework
- ไม่บอกระดับคุณภาพโค้ด
- Prompt สั้นเกินไป
- ไม่กำหนด output format
🧠 Cheat Sheet (สั้นมาก)
Role + Task + Context + Constraints + Output
🎯 ถัดไปอยากทำอะไร?
ผมช่วยคุณได้ต่อ เช่น
- 📦 Prompt สำหรับ AI Agent / SaaS
- ⚡ Prompt สำหรับ Copilot / Cursor / Claude
- 🧩 Prompt สำหรับ Agile + AI Coding Workflow
- 📊 ทำเป็น Diagram / Infographic
บอกแนวที่คุณอยากใช้ได้เลยครับ 👌
ความคิดเห็น
แสดงความคิดเห็น