Pair Programming with Git Copilot

  • Sreekanth A PSreekanth A P
  • Software Development
  • 8 months ago
Pair Programming with Git Copilot

What is pair programming?

Pair programming is an Agile software development technique originating from Extreme programming (XP) in which two developers team together on one computer. The two people work together to design, code and test user stories. Ideally, the two people would be equally skilled and would each have equal time at the keyboard.

 

What is GitHub Copilot? 

GitHub Copilot is an AI pair programmer that offers autocomplete-style suggestions as you code. You can receive suggestions from GitHub Copilot either by starting to write the code you want to use, or by writing a natural language comment describing what you want the code to do. GitHub Copilot analyses the context in the file you are editing, as well as related files, and offers suggestions from within your text editor. GitHub Copilot is powered by a generative AI model developed by GitHub, OpenAI, and Microsoft.

GitHub Copilot is trained on all languages that appear in public repositories. For each language, the quality of suggestions you receive may depend on the volume and diversity of training data for that language. For example, JavaScript is well-represented in public repositories and is one of GitHub Copilot's best supported languages. Languages with less representation in public repositories may produce fewer or less robust suggestions.

 

Supported IDEs:

GitHub Copilot is available as an extension in Visual Studio Code, Visual Studio, Vim, Neovim, the JetBrains suite of IDEs, and Azure Data Studio

 

How does GitHub Copilot work? 

It can suggest complete lines of code or entire functions by analysing how you code. GitHub Copilot can assemble code from user comments and predicts your code by just reading the function name you have declared. It allows you to cycle through alternative suggestions and manually edit the suggested code. Its autofill repetitive code, or create unit tests for your methods.

Illustration of Copilot Prompt Processing Flow

  • Secure Prompt Transmission: When you use GitHub Copilot, your prompts are transmitted securely via HTTPS. This ensures that your sensitive data remains confidential as it travels to GitHub Copilot’s servers.
  • Contextual Understanding: GitHub Copilot analyses the code context both before and after the cursor position. It considers the filename and file type to provide relevant code suggestions. It also takes into account other open tabs to ensure consistency within the project.
  • Content Filtering: Before generating code, Copilot filters out personal data to protect user privacy. It also detects and prevents hate speech or inappropriate content.
  • Feedback Loop Initiation: Imagine a chef who never tastes their own cooking – it just wouldn’t work! Similarly, GitHub Copilot thrives on feedback. When you accept its code suggestions, it’s like giving it a high-five and saying, “Good job!” But here’s the fun part: Copilot learns from your tweaks and rejections, levelling up its coding skills.
  • Prompt History Retention: Copilot remembers your prompts, context, and interactions. It’s like a memory bank, helping Copilot provide relevant suggestions based on what you’re working on.
  • Repeat for Subsequent Prompts: Think of it as a dance – your prompt, Copilot grooves. As you two boogies together, Copilot learns from your cues, predicting your needs and improving its code generation.

 

Here’s an example of GitHub Copilot generating an irrelevant solution:

 

Example of GitHub Copilot generating an irrelevant solution

When we adjusted our prompt, we were able to generate more accurate results: 

Example of GitHub Copilot generating an irrelevant solution after adjusting prompt

Best practices for prompt crafting with GitHub Copilot

 

1. Set the stage with a high-level goal. 

This is most helpful if you have a blank file or empty codebase. In other words, if GitHub Copilot has zero context of what you want to build or accomplish, setting the stage for the AI pair programmer can be really useful. It helps to prime GitHub Copilot with a big picture description of what you want it to generate—before you jump in with the details.

When prompting GitHub Copilot, think of the process as having a conversation with someone: How should I break down the problem so we can solve it together? How would I approach pair programming with this person?

For example, when building a markdown editor in Next.jst, we could write a comment like this

/*

Create a basic markdown editor in Next.js with the following features:

- Use react hooks

- Create state for markdown with default text "type markdown here"

- A text area where users can write markdown

- Show a live preview of the markdown text as I type

- Support for basic markdown syntax like headers, bold, italics

- Use React markdown npm package

- The markdown text and resulting HTML should be saved in the component's state and updated in real time

*/

This will prompt GitHub Copilot to generate the following code and produce a very simple, unstyled but functional markdown editor in less than 30 seconds. We can use the remaining time to style the component:

Basic markdown editor in Next.js

2. Make your ask simple and specific. Aim to receive a short output from GitHub Copilot. 

Once you communicate your main goal to the AI pair programmer, articulate the logic and steps it needs to follow for achieving that goal. GitHub Copilot better understands your goal when you break things down. (Imagine you’re writing a recipe. You’d break the cooking process down into discrete steps–not write a paragraph describing the dish you want to make.)

Let GitHub Copilot generate the code after each step, rather than asking it to generate a bunch of code all at once.

Here’s an example of us providing GitHub Copilot with step-by-step instructions for reversing a function:

Example of us providing GitHub Copilot with step-by-step instructions for reversing a function

How good is GitHub Copilot?

Copilot helps developers to quickly discover alternative ways to solve problems, write tests, and explore new APIs without having to tediously tailor a search for answers on sites like Stack Overflow and across the internet.

The Good

  • Good at Boilerplate
  • It Helps to Get Started
  • It’s Easy to Use

The Bad

  • You Spend More Time Reviewing Code
  • Sometimes It Gets Confused

 

Does GitHub Copilot write perfect code?

No. GitHub Copilot tries to understand your intent and to generate the best code it can, but the code it suggests may not always work or even make sense. “GitHub Copilot draws context from the code you’re working on, suggesting whole lines or entire functions,”. The algorithm consistently improves by recording whether each suggestion is accepted or not.

In order to make the most out of it, it is suggested to divide the code into smaller functions, provide meaningful function names, parameters, and docstrings.

 

Will your private code be shared with other users?

No. They claim to use telemetry data, including information about which suggestions users accept or reject, to improve the model.

 

Ownership

GitHub assigns rights to the person writing the code, so the user becomes responsible for the suggestions it generates and code created with Copilot. It belongs to you, and you obtain the sole ownership of the code.

 

Will GitHub Copilot affect developer jobs?

Copilot autocompletes code snippets, suggests new lines of code, and can even write whole functions based on the description provided. It adapts to the way you write code and helps you complete the work faster. The technology will enable existing engineers to be more productive, reducing manual tasks and helping them focus on interesting work. It only suggests a piece of code, it still needs to be reviewed and tested manually like you normally do with every other code.

 

Got a similar project idea?

Connect with us & let’s start the journey!

Questions about our products and services?

We're here to support you.

Staff augmentation is a flexible workforce strategy companies adopt to meet specific project needs or address skill gaps.

Begin your journey!
Need more help?