GitHub Copilot: เทคนิคการใช้งานให้คุ้มค่า
- AI Unlocked Team
- Vibe Coding , Tools
- 22 Oct, 2025
GitHub Copilot: เทคนิคการใช้งานให้คุ้มค่า
GitHub Copilot เป็น AI coding assistant ที่ได้รับความนิยมสูงสุด แต่หลายคนใช้ไม่เต็มศักยภาพ บทความนี้จะแนะนำเทคนิคการใช้งาน Copilot ให้ได้ประโยชน์สูงสุด คุ้มค่ากับเงินที่จ่ายไป
GitHub Copilot คืออะไร?
GitHub Copilot = AI Pair Programmer โดย GitHub + OpenAI
- แนะนำโค้ดขณะพิมพ์
- สร้างฟังก์ชันทั้งหมด
- อธิบายโค้ด
- แปลงภาษา
- เขียน tests
- Fix bugs
Plans & Pricing
Individual ($10/เดือน)
- รายบุคคล
- ใช้ IDE ส่วนตัว
- Public code training
Business ($19/user/เดือน)
- องค์กร
- Admin controls
- IP protection
- Private code option
Enterprise ($39/user/เดือน)
- Enterprise features
- Custom models
- Security & compliance
อ่านเพิ่มเติม: Cursor AI Guide
เทคนิคพื้นฐาน
1. Comment-Driven Development
หลักการ: เขียน comment อธิบายก่อน จะได้โค้ดที่ตรงใจ
ตัวอย่าง:
// Function to validate email format
// Returns true if valid, false if invalid
// Should handle edge cases like:
// - Empty string
// - Missing @ sign
// - Missing domain
// - Special characters
// กด Enter → Copilot เขียนให้
function validateEmail(email) {
if (!email || email.length === 0) {
return false;
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
return false;
}
// Additional validations
const parts = email.split('@');
if (parts.length !== 2) {
return false;
}
const [localPart, domain] = parts;
if (localPart.length === 0 || domain.length === 0) {
return false;
}
return true;
}
Tips:
- ยิ่ง comment ละเอียดยิ่งดี
- ใส่ edge cases
- ระบุ input/output
- ให้ตัวอย่าง
2. Function Signature First
หลักการ: เขียน function signature ก่อน Copilot จะเติมเนื้อหา
// เขียนแค่ signature
async function fetchUserProfile(
userId: string
): Promise<UserProfile> {
// กด Enter → Copilot เขียนให้
const response = await fetch(`/api/users/${userId}`);
if (!response.ok) {
throw new Error(`Failed to fetch user: ${response.statusText}`);
}
const data = await response.json();
return data as UserProfile;
}
3. Example-Driven Generation
หลักการ: ให้ตัวอย่างก่อน Copilot จะทำตามแบบ
// ตัวอย่าง: Convert Celsius to Fahrenheit
function celsiusToFahrenheit(celsius) {
return (celsius * 9/5) + 32;
}
// ตัวอย่าง: Convert Fahrenheit to Celsius
function fahrenheitToCelsius(fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
// Convert Celsius to Kelvin
// กด Enter → Copilot เข้าใจ pattern
function celsiusToKelvin(celsius) {
return celsius + 273.15;
}
4. Keyboard Shortcuts
รู้ทางลัดเหล่านี้:
Tab- Accept suggestionAlt+]- Next suggestionAlt+[- Previous suggestionCtrl+Enter- Open Copilot (list of suggestions)Esc- Dismiss suggestion
อ่านเพิ่มเติม: Vibe Coding vs Traditional
เทคนิคขั้นสูง
1. Multi-Step Generation
ใช้ comments เป็น checklist:
// Create a React component for a Todo List
// 1. useState for todos array
// 2. useState for input value
// 3. Function to add todo
// 4. Function to toggle todo
// 5. Function to delete todo
// 6. Render todo list
// 7. Render input and add button
const TodoList = () => {
// 1. State
const [todos, setTodos] = useState([]);
const [inputValue, setInputValue] = useState('');
// 2. Add todo
const addTodo = () => {
// Copilot จะเขียนให้ตาม spec
if (inputValue.trim() === '') return;
setTodos([
...todos,
{
id: Date.now(),
text: inputValue,
completed: false
}
]);
setInputValue('');
};
// 3. Toggle complete
const toggleTodo = (id) => {
// Copilot continues...
setTodos(todos.map(todo =>
todo.id === id
? { ...todo, completed: !todo.completed }
: todo
));
};
// ... rest of code
};
2. Context-Aware Suggestions
Copilot อ่าน:
- ไฟล์ที่เปิดอยู่
- Code ข้างๆ
- Import statements
- Types/Interfaces
ตัวอย่าง:
// Define interface
interface User {
id: number;
name: string;
email: string;
role: 'admin' | 'user';
}
// Copilot รู้ว่า User มี properties อะไรบ้าง
function displayUser(user: User) {
// พิมพ์ user. → Copilot แนะนำ properties
return `${user.name} (${user.email})`;
}
// สร้าง mock data
const mockUsers: User[] = [
// กด Enter → Copilot สร้าง mock ตาม interface
{
id: 1,
name: 'John Doe',
email: 'john@example.com',
role: 'admin'
},
// กด Enter อีก → สร้างต่อ
{
id: 2,
name: 'Jane Smith',
email: 'jane@example.com',
role: 'user'
}
];
3. Test Generation
Copilot เขียน tests ได้ดี:
// Original function
function calculateDiscount(price, discountPercent) {
if (price < 0 || discountPercent < 0 || discountPercent > 100) {
throw new Error('Invalid input');
}
return price * (1 - discountPercent / 100);
}
// Test cases:
// - Normal case
// - Zero discount
// - 100% discount
// - Negative price (should throw)
// - Negative discount (should throw)
// - Discount > 100 (should throw)
describe('calculateDiscount', () => {
// กด Enter → Copilot สร้าง tests ทั้งหมด
test('calculates discount correctly', () => {
expect(calculateDiscount(100, 10)).toBe(90);
expect(calculateDiscount(50, 20)).toBe(40);
});
test('handles zero discount', () => {
expect(calculateDiscount(100, 0)).toBe(100);
});
test('handles 100% discount', () => {
expect(calculateDiscount(100, 100)).toBe(0);
});
test('throws error for negative price', () => {
expect(() => calculateDiscount(-100, 10))
.toThrow('Invalid input');
});
// ... Copilot continues
});
4. Documentation Generation
Copilot เขียน docs:
/**
* กด /** แล้ว Enter → Copilot สร้าง JSDoc
*
* Fetches user data from the API
*
* @param {string} userId - The unique identifier of the user
* @param {Object} options - Optional configuration
* @param {boolean} options.includeProfile - Include profile data
* @param {boolean} options.includePosts - Include user posts
* @returns {Promise<User>} The user object
* @throws {Error} If the user is not found or API fails
*
* @example
* const user = await fetchUser('123', {
* includeProfile: true,
* includePosts: false
* });
*/
async function fetchUser(userId, options = {}) {
// ... implementation
}
5. Code Translation
แปลงภาษา:
# Python version
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
# JavaScript version:
// พิมพ์แค่ comment → Copilot แปลให้
function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
# TypeScript version:
// Copilot continues...
function fibonacci(n: number): number {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
อ่านเพิ่มเติม: AI Code Review
Patterns ที่ Copilot เก่ง
1. CRUD Operations
// Create user in database
// Copilot รู้ pattern นี้ดี
async function createUser(userData) {
const user = await db.users.create({
data: userData
});
return user;
}
// Read user
// กด Enter → Copilot ทำต่อ
async function getUser(userId) {
const user = await db.users.findUnique({
where: { id: userId }
});
return user;
}
// Update, Delete → Copilot เขียนต่อตาม pattern
2. API Routes
// GET /api/products
app.get('/api/products', async (req, res) => {
// Copilot รู้ว่าต้อง:
// 1. Parse query params
// 2. Fetch from DB
// 3. Handle errors
// 4. Return JSON
try {
const { category, limit = 10, page = 1 } = req.query;
const products = await db.products.findMany({
where: category ? { category } : {},
take: parseInt(limit),
skip: (parseInt(page) - 1) * parseInt(limit)
});
res.json({
data: products,
page: parseInt(page),
limit: parseInt(limit)
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// POST, PUT, DELETE → Copilot follows pattern
3. React Hooks
// Custom hook for fetching data
function useFetch(url) {
// Copilot รู้ pattern:
// 1. useState for data, loading, error
// 2. useEffect for fetching
// 3. Return { data, loading, error }
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const fetchData = async () => {
try {
setLoading(true);
const response = await fetch(url);
if (!response.ok) throw new Error('Failed to fetch');
const json = await response.json();
setData(json);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
fetchData();
}, [url]);
return { data, loading, error };
}
4. Form Validation
// Validate registration form
function validateRegistrationForm(formData) {
// Copilot knows common validations:
const errors = {};
// Name
if (!formData.name || formData.name.length < 2) {
errors.name = 'Name must be at least 2 characters';
}
// Email
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!formData.email || !emailRegex.test(formData.email)) {
errors.email = 'Invalid email format';
}
// Password
if (!formData.password || formData.password.length < 8) {
errors.password = 'Password must be at least 8 characters';
}
// Copilot continues with more validations...
if (formData.password !== formData.confirmPassword) {
errors.confirmPassword = 'Passwords do not match';
}
return {
isValid: Object.keys(errors).length === 0,
errors
};
}
อ่านเพิ่มเติม: Debugging with AI
Tips & Tricks
1. Tab ไว ไว คือดี
หลักการ:
- Copilot แนะนำเร็วมาก
- อย่ารอให้เขียนครบ ค่อยๆ Tab ไปเรื่อยๆ
ตัวอย่าง:
function cal // เห็น suggestion → Tab
function calculateTotal // Tab
function calculateTotal(items) { // Tab
return items // Tab
return items.reduce // Tab
return items.reduce((total, item) => // Tab
// ... Tab เรื่อยๆ
2. ใช้ Descriptive Names
ดี:
// Names บอกความหมาย → Copilot เข้าใจ
function calculateTotalPriceWithTaxAndDiscount(items, taxRate, discountPercent) {
// Copilot รู้ว่าต้องทำอะไร
}
ไม่ดี:
// Names งง → Copilot งง
function calc(a, b, c) {
// Copilot ไม่รู้ว่า a, b, c คืออะไร
}
3. เรียนรู้ Patterns
Copilot ดีกับ:
- Standard patterns (CRUD, API routes)
- Common algorithms (sort, search, filter)
- Popular libraries (React, Express)
- Boilerplate code
Copilot ไม่เก่ง:
- Business logic ซับซ้อน
- Domain-specific code
- Novel algorithms
- Edge cases พิเศษ
4. Iterate อย่างรวดเร็ว
Workflow:
1. เขียน comment → Tab → ได้โค้ด
2. ไม่ชอบ → ลบ → เขียน comment ใหม่
3. ได้โค้ดดีขึ้น → Tab
4. Repeat จนพอใจ
อย่ากลัวลบ: ลบแล้วเขียนใหม่เร็วกว่าแก้
5. ใช้ GitHub Copilot Chat
Copilot Chat = ChatGPT ใน IDE:
คำสั่งที่มีประโยชน์:
/explain - อธิบายโค้ด
/fix - แก้ bugs
/tests - สร้าง unit tests
/optimize - optimize performance
ตัวอย่าง:
เลือกโค้ด → พิมพ์:
"/fix this bug"
"/explain what this does"
"/tests for this function"
อ่านเพิ่มเติม: Build E-commerce Vibe Coding
สถานการณ์การใช้งาน
Scenario 1: สร้าง API Endpoint
จุดเริ่มต้น:
// POST endpoint to create a blog post
// Input: { title, content, author, tags[] }
// Validation: title (required, min 5 chars)
// content (required, min 100 chars)
// author (required)
// tags (optional array)
// Save to database with created_at timestamp
// Return created post with id
app.post('/api/posts', async (req, res) => {
// Tab → Copilot เขียนให้ทั้งหมด
});
ผลลัพธ์:
- Validation logic
- Error handling
- Database operation
- Response formatting
- เวลา: 30 วินาที
Scenario 2: React Component
จุดเริ่มต้น:
// Component: UserProfile
// Props: userId
// Features:
// - Fetch user data on mount
// - Show loading spinner
// - Show error if fetch fails
// - Display avatar, name, email, bio
// - Edit button (for own profile)
// - Follow button (for other profiles)
const UserProfile = ({ userId }) => {
// Tab → Copilot สร้าง component
};
ผลลัพธ์:
- All states (data, loading, error)
- useEffect for fetching
- Conditional rendering
- UI components
- เวลา: 1-2 นาที
Scenario 3: Algorithm Implementation
จุดเริ่มต้น:
// Implement binary search algorithm
// Input: sorted array and target value
// Output: index of target (-1 if not found)
// Time complexity: O(log n)
function binarySearch(arr, target) {
// Tab → Copilot เขียน algorithm
}
ผลลัพธ์:
- Correct algorithm
- Edge case handling
- Comments explaining logic
- เวลา: 20-30 วินาที
Common Mistakes
❌ Mistake 1: ไม่อ่านโค้ดที่ Copilot เขียน
ผลลัพธ์:
- ใช้โค้ดที่มี bugs
- ไม่เข้าใจโค้ดตัวเอง
- แก้ไขยาก
แก้ไข:
- อ่านทุกบรรทัด
- เข้าใจว่าทำอะไร
- Test ก่อนใช้
❌ Mistake 2: Comments ไม่ชัดเจน
ไม่ดี:
// Function to do something
function doSomething(data) {
// Copilot งง
}
ดี:
// Calculate total price including tax
// Input: items array with price and quantity
// Tax rate: 7%
// Return: total price number
function calculateTotalWithTax(items) {
// Copilot เข้าใจ
}
❌ Mistake 3: ปล่อย Copilot เขียนทั้งหมด
ปัญหา:
- โค้ดไม่เข้ากับ architecture
- ไม่มี error handling
- ไม่มี validation
แก้ไข:
- Guide Copilot ด้วย comments
- Review และปรับแต่ง
- เพิ่ม error handling เอง
Copilot vs Cursor vs ChatGPT
| ฟีเจอร์ | Copilot | Cursor | ChatGPT |
|---|---|---|---|
| Autocomplete | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ❌ |
| Chat | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| Context | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐ |
| Multi-file | ⭐⭐ | ⭐⭐⭐⭐⭐ | ❌ |
| ราคา | $10 | $20 | $20 |
| IDE | VS Code, JetBrains | VS Code fork | Web |
แนะนำ:
- Copilot: สำหรับ VS Code users
- Cursor: สำหรับ Vibe Coding (ดีที่สุด)
- ChatGPT: สำหรับ planning และ learning
อ่านเพิ่มเติม: Cursor AI Complete Guide
สรุป
GitHub Copilot ช่วยให้เขียนโค้ดได้เร็วขึ้น 2-5 เท่า ถ้าใช้เป็น
Tips สำคัญ:
- ✍️ เขียน comments ที่ชัดเจน
- ⚡ Tab ไวๆ อย่ารอ
- 👀 อ่านและเข้าใจโค้ดที่ Copilot เขียน
- 🔄 Iterate รวดเร็ว
- 🧪 Test ก่อนใช้
ROI:
- ค่าบริการ: 300 บาท/เดือน
- ประหยัดเวลา: 10-20 ชม./สัปดาห์
- คุ้มค่ามากๆ!
หากต้องการเรียน GitHub Copilot และ Vibe Coding เจาะลึก เรียน คอร์ส Vibe Coding กับเราได้
บทความที่เกี่ยวข้อง:
สนใจเรียน Vibe Coding? คอร์ส Vibe Coding ออนไลน์ | ติดต่อเรา
🚀 พร้อมเริ่มต้นเรียน AI แล้วหรือยัง?
เรียนคอร์ส AI, Vibe Coding และ n8n Automation แบบออนไลน์
เรียนได้ทันทีผ่านแพลทฟอร์มของเรา
✨ สอนภาษาไทย | ไม่ต้องมีพื้นฐาน | เรียนได้ทันที