When I first used GitHub Copilot and other AI tools, I often got bad results. I saw others get good ones and wondered why. For example, I asked Copilot to solve a LeetCode problem, but it gave wrong suggestions or nothing. I learned I was not using it right. By adding context, examples, and clear steps in comments and code, I got better results. This is called prompt engineering. In this post, I share tips to help you use GitHub Copilot well.
Let me start with the basics for new users.
What is GitHub Copilot?
GitHub Copilot is an AI tool that helps with coding. It is made by GitHub and uses OpenAI’s Codex model, trained to make code from plain words. It suggests code based on your comments and current code. You can add it as an extension in these tools:
- Visual Studio
- Visual Studio Code
- Neovim
- JetBrains IDEs like IntelliJ, PyCharm, or WebStorm
Can GitHub Copilot Write Code Alone?
We call it an AI pair programmer because it needs your help. It follows what you tell it and does not decide on its own. Copilot uses your code and comments to suggest lines or functions. You can turn comments into code, complete patterns, or see options.
How Does GitHub Copilot Work?
Copilot takes context from comments and code to suggest code fast. It uses OpenAI Codex to turn plain words into code.
What is Prompt Engineering?
Prompt engineering means giving clear instructions to an AI to get the right output. A prompt is text or code that starts the AI’s response. It is like giving a topic for an essay, such as writing about a challenge or a book.
Think of it this way: When learning to code, I did a task to tell a robot how to make a sandwich. Computers need exact steps. For example:
- Open the bread bag.
- Take out two slices.
- Put them on the counter.
- Spread peanut butter on one.
Without details, the robot fails. Copilot is the same. It needs clear steps for good code.
Now, here are best practices for prompt engineering with Copilot.
Best Practices for Prompt Engineering with GitHub Copilot
Start with Big Picture, Then Details
Give an overview in a comment at the top, then add specific comments below.
For a to-do app: At the top, write: “Make a to-do app in Next.js for adding, editing, and deleting items.” Then add comments for each part, like the list, button, input, add function, edit, and delete. Copilot fills in the code.
I used this for a house drawing in p5.js: “Draw a white house with brown roof, red door, and red chimney.” Then comments for each part, and Copilot made the code.
[Image description: GIF of writing comments and Copilot making p5.js code for the house step by step.]
Give Exact Details
Details help Copilot make right suggestions. For fetching API data, say what data, how to handle it, and the endpoint.
Bad example (vague):
// Get data from API
function getData() {
// code here
}
Copilot may give empty code.
Good example (detailed in Python):
# Fetch user data from jsonplaceholder.typicode.com for given user ID, handle errors, and return as JSON
import requests
def get_user_data(user_id):
try:
response = requests.get(f"https://jsonplaceholder.typicode.com/users/{user_id}")
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return None
This guides Copilot better.
Include Examples
Show examples in your style. This helps the AI understand your needs. Use few-shot learning: Give a few code samples, and it makes similar ones.
For example, in C#, if you want math functions:
// Example: Add two numbers
public int Add(int a, int b) {
return a + b;
}
// Example: Subtract two numbers
public int Subtract(int a, int b) {
return a - b;
}
// Now, multiply two numbers
Copilot will suggest:
public int Multiply(int a, int b) {
return a * b;
}
In Python, for list operations:
# Example: Sum a list
def sum_list(nums):
return sum(nums)
# Example: Find max in list
def max_list(nums):
return max(nums)
# Now, find min in list
Copilot adds:
def min_list(nums):
return min(nums)
Use Plain Words in Comments
Write comments like talking to a friend. Use full sentences. Instead of “sort array,” say “Sort this number array from small to big without changing the original.”
Example in Python:
# Sort this list of numbers from small to big, return a new list
numbers = [5, 3, 8, 1]
sorted_numbers = sorted(numbers)
Break into Small Steps
Big tasks can confuse the AI. Split them. Comment each step, let Copilot do one at a time.
Change Prompts if Needed
If the suggestion is wrong, add more details or examples and try again.
Use File Context
Copilot sees the whole file. Keep related code close for better suggestions.
Add this tip: Role-play in prompts. Tell Copilot to act as a senior developer. Example: “# Act as a Python expert. Write a function to read a CSV file and print the first 5 rows.”
In C#: “// As a C# pro, create a class for a simple bank account with deposit and withdraw methods.”
Conclusion
Prompt engineering gets better with practice. Use clear context, details, and examples to guide Copilot. Start small, try things, and improve. Share your tips in comments.
A Beginner’s Guide to Prompt Engineering with GitHub Copilot
When I first used GitHub Copilot and other AI tools, I often got bad results. I saw others get good ones and wondered why. For example, I asked Copilot to solve a LeetCode problem, but it gave wrong suggestions or nothing. I learned I was not using it right. By adding context, examples, and clear steps in comments and code, I got better results. This is called prompt engineering. In this post, I share tips to help you use GitHub Copilot well.
Let me start with the basics for new users.
What is GitHub Copilot?
GitHub Copilot is an AI tool that helps with coding. It is made by GitHub and uses OpenAI’s Codex model, trained to make code from plain words. It suggests code based on your comments and current code. You can add it as an extension in these tools:
- Visual Studio
- Visual Studio Code
- Neovim
- JetBrains IDEs like IntelliJ, PyCharm, or WebStorm
Can GitHub Copilot Write Code Alone?
We call it an AI pair programmer because it needs your help. It follows what you tell it and does not decide on its own. Copilot uses your code and comments to suggest lines or functions. You can turn comments into code, complete patterns, or see options.
How Does GitHub Copilot Work?
Copilot takes context from comments and code to suggest code fast. It uses OpenAI Codex to turn plain words into code.
What is Prompt Engineering?
Prompt engineering means giving clear instructions to an AI to get the right output. A prompt is text or code that starts the AI’s response. It is like giving a topic for an essay, such as writing about a challenge or a book.
Think of it this way: When learning to code, I did a task to tell a robot how to make a sandwich. Computers need exact steps. For example:
- Open the bread bag.
- Take out two slices.
- Put them on the counter.
- Spread peanut butter on one.
Without details, the robot fails. Copilot is the same. It needs clear steps for good code.
Now, here are best practices for prompt engineering with Copilot.
Best Practices for Prompt Engineering with GitHub Copilot
Start with Big Picture, Then Details
Give an overview in a comment at the top, then add specific comments below.
For a to-do app: At the top, write: “Make a to-do app in Next.js for adding, editing, and deleting items.” Then add comments for each part, like the list, button, input, add function, edit, and delete. Copilot fills in the code.
I used this for a house drawing in p5.js: “Draw a white house with brown roof, red door, and red chimney.” Then comments for each part, and Copilot made the code.
[Image description: GIF of writing comments and Copilot making p5.js code for the house step by step.]
Give Exact Details
Details help Copilot make right suggestions. For fetching API data, say what data, how to handle it, and the endpoint.
Bad example (vague):
// Get data from API
function getData() {
// code here
}
Copilot may give empty code.
Good example (detailed in Python):
# Fetch user data from jsonplaceholder.typicode.com for given user ID, handle errors, and return as JSON
import requests
def get_user_data(user_id):
try:
response = requests.get(f"https://jsonplaceholder.typicode.com/users/{user_id}")
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return None
This guides Copilot better.
Include Examples
Show examples in your style. This helps the AI understand your needs. Use few-shot learning: Give a few code samples, and it makes similar ones.
For example, in C#, if you want math functions:
// Example: Add two numbers
public int Add(int a, int b) {
return a + b;
}
// Example: Subtract two numbers
public int Subtract(int a, int b) {
return a - b;
}
// Now, multiply two numbers
Copilot will suggest:
public int Multiply(int a, int b) {
return a * b;
}
In Python, for list operations:
# Example: Sum a list
def sum_list(nums):
return sum(nums)
# Example: Find max in list
def max_list(nums):
return max(nums)
# Now, find min in list
Copilot adds:
def min_list(nums):
return min(nums)
Use Plain Words in Comments
Write comments like talking to a friend. Use full sentences. Instead of “sort array,” say “Sort this number array from small to big without changing the original.”
Example in Python:
# Sort this list of numbers from small to big, return a new list
numbers = [5, 3, 8, 1]
sorted_numbers = sorted(numbers)
Break into Small Steps
Big tasks can confuse the AI. Split them. Comment each step, let Copilot do one at a time.
Change Prompts if Needed
If the suggestion is wrong, add more details or examples and try again.
Use File Context
Copilot sees the whole file. Keep related code close for better suggestions.
Add this tip: Role-play in prompts. Tell Copilot to act as a senior developer. Example: “# Act as a Python expert. Write a function to read a CSV file and print the first 5 rows.”
In C#: “// As a C# pro, create a class for a simple bank account with deposit and withdraw methods.”
Conclusion
Prompt engineering gets better with practice. Use clear context, details, and examples to guide Copilot. Start small, try things, and improve. Share your tips in comments.
