Foundational Prompt Engineering for Code Generation
Learn the art of writing clear, specific, and contextual prompts to generate accurate, reusable code with GitHub Copilot.
GitHub Copilot is a powerful coding assistant, but it’s only as effective as your instructions. In this lesson, you’ll learn to think like a prompt engineer: communicating your intent clearly, concisely, and with the right level of detail to get accurate results.
We’ll walk through this using your React Weather app and improve it using real-world prompts:
Make a clone of a GitHub repository of the weather dashboard app in your VS Code:
git clone https://github.com/Educative-Content/weather_dashboard
The art of effective prompting
When using GitHub Copilot to generate code, how you write your prompt makes all the difference. Think of Copilot as a developer sitting next to you. If your instructions are vague, the result will be messy. But if your instructions are clear, detailed, and framed within the right context, Copilot becomes surprisingly accurate.
Effective prompts typically follow three key principles:
Clarity
Clarity means writing your intent in plain language, without ambiguity. Avoid generic or abstract phrases like:
// weather code goes here
Copilot can’t read your mind. It needs to know what exactly you’re trying to achieve. Instead, try something like:
// Fetch current weather data from Open-Meteo API using lat/lon
This prompt tells Copilot what kind of task to complete, what API to use, and the required inputs. The clearer your prompt, the better Copilot can assist.
Tip: Treat your comments as if you’re explaining your task to another developer seeing the code for the first time.
Specificity
While clarity focuses on what you want done, specificity focuses on how you want it done. The more detailed your prompt, the closer Copilot will get to your expected output.
For example:
// Return an object with temperature, wind speed, humidity, and a weather description
This is specific. You’re not just telling Copilot to “fetch weather”, you’re telling it exactly what values to extract and how to structure the response. Copilot now has a mental picture of the format you expect.
Tip: Think in terms of outputs. Ask yourself, “What should the function return? What should it include?”
Context
Context gives Copilot the “why” and “when” of your code. Yes, Copilot can read your file and nearby code, but it works better when you supply useful context right where it matters.
Example:
// This function will be called when a city is added from input.// Use Open-Meteo API to fetch weather based on coordinates.
Now Copilot knows why you’re writing this function (triggered when a city is added), what data you have (coordinates), and what tool/API to use. It can combine all of that to suggest a well-aligned function. ...