GitHub Copilot Writing Better Prompts
A prompt is any instruction you give Copilot — whether it is a comment, a function name, or a message in Chat. Better prompts produce better code. This topic teaches you the craft of writing prompts that consistently get useful results from GitHub Copilot.
What Makes a Good Prompt
A good prompt answers four questions about the code you want:
┌─────────────────────────────────────────────────────┐ │ THE FOUR QUESTIONS OF A GOOD PROMPT │ ├─────────────────────────────────────────────────────┤ │ 1. WHAT should it do? (the action) │ │ 2. WITH WHAT input? (parameters) │ │ 3. WHAT should it return? (output/return value) │ │ 4. WHAT are the constraints? (edge cases, rules) │ └─────────────────────────────────────────────────────┘ BAD PROMPT (answers none of the four): // process data GOOD PROMPT (answers all four): // Read an array of transaction objects, each with amount and type. // Sum all transactions where type is 'credit'. // Subtract all transactions where type is 'debit'. // Return the final balance as a number rounded to 2 decimal places.
Prompt Anatomy: The Building Blocks
ELEMENT PURPOSE EXAMPLE ────────────── ────────────────────────────── ────────────────────────────── Action verb Tells Copilot what to do "Calculate", "Validate", "Sort" Object What it operates on "an array of users" Condition When or if something applies "only if they are active" Return format What comes back "return a string", "return true/false" Error behavior What to do when things go wrong "throw an error if the input is null"
From Weak to Strong: Prompt Transformation Examples
Here are four before-and-after examples showing how to upgrade weak prompts into strong ones.
EXAMPLE 1 — Weak:
// check user
Strong:
// Check if a user object has a valid email and is not blocked.
// Return true if both conditions are met, false otherwise.
EXAMPLE 2 — Weak:
// send notification
Strong:
// Send a push notification to a user device using the FCM API.
// Accept userId, title, and body as parameters.
// Log a warning and return false if the device token is missing.
EXAMPLE 3 — Weak:
// format date
Strong:
// Format a JavaScript Date object into the string "DD Mon YYYY"
// For example: new Date("2024-01-15") → "15 Jan 2024"
// Handle invalid Date objects by returning "Invalid date".
EXAMPLE 4 — Weak:
// get products
Strong:
// Fetch products from /api/products with optional filters:
// category (string), minPrice (number), maxPrice (number).
// Return an array of product objects.
// If the fetch fails, throw an Error with the status code.
One Prompt vs. Multiple Small Prompts
For complex functions, break a single big prompt into several smaller prompts placed at each step inside the function. This produces cleaner, more readable code than one giant description.
SINGLE BIG PROMPT (less effective for complex logic):
// Process an order: validate input, calculate totals with tax,
// check inventory, reserve stock, create order record, send confirmation
MULTIPLE SMALL PROMPTS (more effective):
async function processOrder(orderData) {
// Validate that all required fields are present
← Copilot writes validation here
// Calculate order total with 8% tax
← Copilot writes calculation here
// Check inventory levels for each item
← Copilot writes inventory check here
// Reserve the ordered quantities in inventory
← Copilot writes reservation here
// Insert order into the database
← Copilot writes DB insert here
// Send confirmation email to the customer
← Copilot writes email send here
}
Using Examples in Prompts
Showing Copilot an example of the expected input and output is one of the most powerful techniques. Examples remove ambiguity completely.
WITHOUT EXAMPLE:
// Convert a product name to a URL slug
WITH EXAMPLE:
// Convert a product name to a URL slug
// Example: "Running Shoes (Size 10)" → "running-shoes-size-10"
// Example: "T-Shirt & Jeans" → "t-shirt-jeans"
function slugify(name) {
→ Copilot handles special characters and ampersands correctly
because the examples made the requirements explicit
Negative Constraints in Prompts
Telling Copilot what NOT to do is as useful as telling it what to do. Negative constraints prevent common mistakes.
WITHOUT CONSTRAINT:
// Sort users by age
WITH NEGATIVE CONSTRAINTS:
// Sort users by age in ascending order
// Do not use a third-party library — use JavaScript's built-in sort
// Do not modify the original array, return a new array
RESULT: Copilot avoids Lodash, sorts ascending (not descending),
and uses [...users].sort() to return a copy
Prompting in Chat vs. Inline Comments
Comments and Chat serve different purposes. Use comments for code generation inside a specific file. Use Chat for larger tasks, explanations, and refactoring across multiple concepts.
USE COMMENT WHEN: ✓ You want code to appear in the file at the cursor position ✓ You are writing a single function or expression ✓ The task is clear and can fit in 3–5 lines of description USE CHAT WHEN: ✓ You want an explanation before generating code ✓ The task spans multiple files or requires context ✓ You are iterating on a design through back-and-forth conversation ✓ You want to compare several approaches before choosing one
Prompt Patterns That Work Consistently
These four prompt patterns produce reliable results across any language:
PATTERN 1 — FUNCTION DESCRIPTION PATTERN: // [Verb] [object] [condition]. Return [output]. // Throws [error] if [bad condition]. PATTERN 2 — STEP LIST PATTERN: // 1. [Step one] // 2. [Step two] // 3. [Step three] // Returns [final output]. PATTERN 3 — INPUT-OUTPUT EXAMPLE PATTERN: // Input: [example input] // Output: [expected output] // Edge: [edge case behavior] PATTERN 4 — SPECIFICATION BLOCK PATTERN: /** * [Function name]: [one sentence description] * @param [name] - [type and purpose] * @returns [type] - [description] * @throws [ErrorType] - [when this happens] */
Prompt Iteration: Getting Better Over Time
Your first prompt does not always produce the best result. When a suggestion misses, refine the prompt rather than rewriting it from scratch. Add one more piece of information — a constraint, an example, or a return type — and trigger a new suggestion.
FIRST ATTEMPT: // Find the most popular product → Copilot: returns the first product (wrong) ADD CONSTRAINT: // Find the product with the highest total order count // across all orders in the database → Copilot: generates a query with COUNT and GROUP BY (correct)
Writing good prompts is a skill that improves with practice. The underlying principle is simple: the more precisely you describe what you need, the more accurately Copilot delivers it. Over time, you develop an instinct for which details matter most — and your suggestions improve consistently as a result.
