Advanced GitHub Copilot Techniques
This document collects concise, practical techniques for getting better results from GitHub Copilot when writing, refactoring, and reviewing code.
1) Crafting effective prompts
- Start with a short goal: "Implement X that does Y for Z".
- Specify language, framework, and constraints: "TypeScript, Node 18, no external deps."
- Add desired API surface: function names, signatures, and comments.
Example:
// Implement: validateUser(input)
// - Node 18
// - Accepts {email, password}
// - Returns {valid: boolean, errors?: string[]}
2) Use stepwise and chain-of-thought style prompts
- Ask for a plan first, then request implementation.
Example:
// Step 1: outline algorithm in 3 steps
// Step 2: implement the function from the outline
3) Prompt templates for common tasks
- Implement feature: provide signature, examples, edge cases.
- Refactor: provide original code and ask for clearer naming and tests.
- Tests: provide function contract and ask for unit tests.
Template (implementation):
"Implement <function> with signature <sig>. Requirements: <reqs>. Examples: <cases>."
4) Provide context files and surrounding code
- Copy-paste small related files (or key snippets) so Copilot preserves style and types.
5) Use inline comments and TODOs
- Write TODOs describing intent and expected behavior; Copilot fills the body.
Example:
// TODO: return items sorted by priority, stable sort
function sortItems(items) { }
6) Verify and iterate
- Ask Copilot to explain its changes or produce tests.
- Run generated code, fix failing tests, then prompt for fixes.
7) Refactoring safely
- Request small, focused refactors.
- Ask for a diff only and unit tests to validate behavior.
Example prompt:
Refactor `processOrder(order)` to extract validation and calculation into helpers. Return a git-style diff and new unit tests.
8) Working with multi-file projects
- Provide key files and describe where new code should live.
- Ask for file paths in outputs so you can paste them directly.
9) Security and dependency guidance
- Ask Copilot to list security considerations and unsafe patterns for produced code.
10) Performance and complexity
- Specify performance targets (O(n), memory limits) in prompts.
11) Using Copilot for docs and examples
- Generate README sections, usage examples, and API docs from code comments or signatures.
12) Prompt examples (quick)
- Generate unit tests for
<function>using Jest. - Add TypeScript types for this JavaScript module.
- Convert this callback API to Promise-based and update callers.
13) Pro tips
- Keep prompts minimal but explicit.
- Use small reproducible examples when possible.
- Iterate: ask for tests, then fixes, then improvements.
This file is a living reference—add templates and snippets that match your team's stack and style.