Overview

2 First steps with AI-assisted coding

This chapter introduces practical, first steps with AI‑assisted coding by diving straight into GitHub Copilot. It sets expectations that the best way to learn is to experiment, and frames Copilot as an interactive “rubber duck” that not only listens but proposes solutions. The chapter explains at a high level what Copilot is—an AI pair‑programmer trained on large amounts of public code—why context is central to its usefulness, and how a continuous feedback loop shapes its suggestions. It also highlights that Copilot is available in popular IDEs, is a paid tool for most users, and that its assistance can quickly make developers more productive without replacing careful engineering judgment.

Readers learn how Copilot works end‑to‑end: prompts and surrounding code are sent with rich context (file contents, cursor position, open tabs, project structure), pass through safety and quality filters, are completed by the model, and then returned with additional outbound checks for toxicity, security, and code quality. The chapter covers key interaction modes—inline code completion, full code generation, language translation, debugging help, and documentation generation—plus the integrated chat experience that is aware of your workspace. It provides reusable prompt patterns for common tasks (finding errors, refactoring, generating boilerplate, adding tests, explaining code, and comparing options) and explains why techniques like Fill‑In‑the‑Middle improve relevance. Throughout, it underscores that results vary and must be reviewed, tested, and refined.

The chapter culminates in a hands‑on Python project that performs simple NLP to analyze word frequency from a public text. Using VS Code, a Python virtual environment, and Copilot, the reader incrementally builds the app: reading a file, normalizing and cleaning text, tokenizing, counting words, and visualizing the top terms with a bar chart in Matplotlib. Along the way, Copilot proposes completions from comments, helps refactor less efficient code, and even assists with performance comparisons between alternative implementations. The exercise reinforces good habits—isolated environments, explicit imports, adding an executable main entry point, installing dependencies—and the central lesson: Copilot accelerates routine work and exploration, but developers should validate, profile, and iterate to produce robust, maintainable code.

GitHub Copilot takes the code from your editor and analyzes it. It then sends suggestions back from the Codex model. Whether you choose to use those suggestions is fed back into the model for feedback.
The inbound flow to GitHub Copilot
Outbound flow from the LLM back to the code editor.
Copilot can suggest code completions as you type by analyzing comments or surrounding code for context.
Copilot can generate entire functions or segments of code based on comments or partial code snippets.
You can use Copilot to translate from one language to another. Here I have highlighted a section of Python code and asked Copilot to translate it into Go.
You can highlight a problematic section of code and get suggestions to fix it. You can either accept the answer and have the code inserted or discard it an attempt a fix of your own.
You can automatically generate comments and other code documentation with Copilot. This is one of my favorite features of the tool, as it saves a tremendous amount of time.
GitHub Copilot can perform many actions from a right click menu within your IDE.
Copilot has a traditional chat window interface within Visual Studio so you can carry out a “conversation” with the tool if needed.
Word-based tokenization breaks whole words and punctuation into chunks that can be processed and analyzed.
The workflow of our sample application
Visual Studio Code opened to a new folder.
Location of the Terminal Window
The Visual Studio Layout, showing Copilot is accessible in the activity bar, and the lower taskbar.
A terminal window showing an active Python environment.
A typical Copilot suggestion
The GitHub Copilot Chat welcome screen
Prompt entered in Copilot Chat
A response from Copilot Chat
The options are shown in the Copilot toolbar.
Output from our program
Our Matplotlib graph shows the top 20 words in the text file.

Summary

  • Be as clear and descriptive as possible. Copilot evaluates comments and function names for suggestions. For example, connect_to_database is better than get_data.
  • Use complete sentences and clear directions. For example, “Convert the text to lowercase” is better than “make lowercase.”
  • Provide sufficient context when selecting code. You improve the results by selecting more than just the line of code with a problem. Surrounding text gives the request more context to work with.
  • Don’t create large prompts. The idiom “Do one thing and do it well” applies to prompts. If they are too long and complex, they should be broken down to prevent errors.
  • You can always try different prompts. If the prompt you use in the chat isn’t giving you what you want, you can get creative and try different things. Make a note of the prompts that serve you well.
  • Chat with Copilot like a human. If you get stuck, try conversing like you would talk to a real person about the problem. This is what the model was designed for, and it works well.
  • Always review and test generated code. As demonstrated with the text cleaning functions, Copilot's first suggestion may not be the most efficient or secure solution.
  • Use Copilot's different interaction methods appropriately. Code completion works well for simple tasks, while chat is better for refactoring or understanding complex code.
  • Remember that context matters. Copilot analyzes your entire workspace, so keeping related files open and organized can improve its suggestions.
  • Don't be afraid to iterate. As shown in the text cleaning example, you can generate multiple solutions and test them to find the best one.
  • Consider performance implications. While Copilot generates working code, you may need to profile and optimize it for your specific needs, as demonstrated in the punctuation removal comparison.
  • Include proper error handling and edge cases. The example showed how adding UTF-8 encoding support could make the file reading more robust across different operating systems.

FAQ

What is GitHub Copilot?GitHub Copilot is an AI coding assistant powered by OpenAI’s Codex model. It suggests code as you type, helps find and fix mistakes, and can generate full functions from comments or partial snippets. It works across many languages and is available in Visual Studio, Visual Studio Code, Vim/Neovim, JetBrains IDEs, and Azure Data Studio. Think of it as an intelligent pair programmer that understands your current file and project context.
How does GitHub Copilot work under the hood?Copilot sends your prompt and code context (nearby code, open tabs, filenames, project structure, language/framework) to its service. An inbound proxy and toxicity filter sanitize the request before the LLM generates suggestions. On the way back, results pass filters for code quality, security, and optionally matching public code. Your accept/reject feedback helps improve future suggestions. It also uses Fill-In-the-Middle (FIM) to consider code before and after your cursor for better completions.
Is GitHub Copilot free?No. Copilot is a paid subscription with a monthly fee. It may be free for verified students, teachers, and maintainers of popular open‑source projects (see the GitHub Copilot information page for current details).
How do I get started with Copilot in Visual Studio Code?- Install Visual Studio Code and sign in to GitHub. - Install the GitHub Copilot and Copilot Chat extensions. - Open a project folder and start coding; type comments or partial code to see inline suggestions. - Use the Copilot Chat panel to ask questions, request refactors, or generate code. - Accept, edit, or dismiss suggestions as you go.
What kinds of tasks can Copilot help with?- Code completion as you type - Code generation from comments or partial snippets - Translation between programming languages - Debugging assistance for errors and performance issues - Documentation and comment generation - Test generation and quick fixes via right‑click tool menus - Conversational help in the Copilot Chat window
How do I set up a Python environment like in the chapter?- Create and activate a virtual environment: python -m venv countwords then source countwords/bin/activate (use your OS equivalent on Windows). - Download input text (e.g., with wget). - Install dependencies: pip install matplotlib tk - For plotting, set the backend: import matplotlib; matplotlib.use('TkAgg'). Using a venv keeps project packages isolated and avoids version conflicts.
What are the steps to build the word-frequency analyzer with Copilot?- Read the text file into a string (consider specifying encoding, e.g., utf-8). - Clean text: lowercase and remove punctuation. - Tokenize: split into words. - Count word frequencies. - Visualize top N words with Matplotlib. You can drive generation with comments like “# tokenize the text into words” or use Copilot Chat to refactor and optimize.
How should I prompt Copilot for better results?- Use clear, goal‑oriented requests: “Generate code for {task}.” - Ask for analysis: “Find the error in the selected code.” - Request refactors: “Refactor for speed/security/readability.” - Learn from code: “Explain how this code works.” - Ask for tests: “Add tests for this code.” - Compare options: “Compare tradeoffs for A vs B.” - Provide hints in code (e.g., start with “async def” to steer toward coroutines). Expect some variability in outputs between runs.
How does context improve Copilot’s suggestions?Copilot leverages your IDE context: the current file, nearby code, open tabs, project structure, filenames, and language/framework. It also uses Fill‑In‑the‑Middle to consider code before and after your cursor, producing suggestions that fit your surrounding logic instead of generic snippets. Higher‑tier products may use broader repository context.
What are best practices and limitations when using Copilot?- Always review, test, and profile generated code; don’t assume it’s optimal or secure. - Validate cross‑platform concerns (e.g., file encodings, plotting backends). - Use Copilot for speed and ideas, but keep human judgment in the loop. - Be aware that your code is sent to the Copilot service for suggestions, and acceptance/rejection can inform future results. - Treat outputs as a starting point; refine to match your standards and constraints.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Coding with AI ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Coding with AI ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Coding with AI ebook for free