Best prompts for GitHub Copilot
GitHub Copilot. Prompts that specify function purpose, input/output formats, and edge cases help Copilot generate accurate and relevant code snippets.RECOMMENDATION
| Use case | Best choice | Why | Runner-up |
|---|---|---|---|
| Writing new functions | Descriptive function signature + docstring prompt | Clearly defines purpose and expected behavior, guiding Copilot to generate precise code | Code comments with example inputs |
| Refactoring code | Prompt with existing code snippet + desired improvements | Contextualizes changes, enabling Copilot to suggest targeted refactorings | General refactor request without code context |
| Generating tests | Prompt specifying test framework and edge cases | Ensures Copilot creates relevant and comprehensive test cases | Simple test function prompt |
| Learning new APIs | Prompt with API usage description and example calls | Helps Copilot produce idiomatic code using the API | Generic API mention without examples |
Top picks explained
For writing new functions, use a prompt that includes a detailed function signature and a docstring describing its purpose and inputs/outputs because this guides GitHub Copilot to generate accurate and relevant code. When refactoring, provide the existing code snippet along with a clear description of the desired improvements to help Copilot understand context and produce targeted suggestions. For generating tests, specify the test framework and include edge cases in your prompt to ensure comprehensive test coverage. When learning new APIs, include usage descriptions and example calls to get idiomatic and correct code snippets.
In practice
Here is an example prompt for writing a new function with GitHub Copilot:
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = '''"""
Function: calculate_factorial
Description: Calculate the factorial of a non-negative integer n.
Input: integer n >= 0
Output: integer factorial of n
Example: calculate_factorial(5) -> 120
"""
def calculate_factorial(n):""" Pricing and limits
GitHub Copilot is a subscription-based service with a free trial period. It uses OpenAI gpt-4o models optimized for code generation. Pricing details and usage limits can be found on the official GitHub Copilot pricing page.
| Option | Free | Cost | Limits | Context |
|---|---|---|---|---|
| GitHub Copilot | 14-day free trial | $10/month individual | Limited to subscription usage | Code completion and generation in IDEs |
What to avoid
- Avoid vague prompts like "Write a function" without context, as Copilot may generate irrelevant or incomplete code.
- Do not rely on single-line comments only; detailed docstrings and examples improve output quality.
- Avoid prompts without specifying input/output formats or edge cases, which leads to generic or incorrect code.
Key Takeaways
- Use detailed natural language prompts with function descriptions and examples for best Copilot results.
- Include input/output specifications and edge cases to guide accurate code generation.
- Avoid vague or minimal prompts that lack context or requirements.