Table of Contents
What is GitHub Copilot
GitHub Copilot is dubbed “an AI pair programmer” to help us write code faster, using a generative AI model developed through a collaboration between GitHub, OpenAI, and Microsoft Azure AI.
These days, there are nothing short of AI-powered tools that can do similar things in this space. For example, Tabnine, Google’s Duet AI, Meta’s CodeCompose (currently used internally at Meta), and Code Llama.
Prerequisites
You need to obtain a paid license before you can use GitHub Copilot.
Then, install the GitHub Copilot extension in your favorite IDE. In this post, I’m using VS Code because GitHub also created the GitHub Copilot Labs extension that is not available in JetBrains IDEs (e.g., IntelliJ, PyCharm, etc.).
Tips & Tricks
Below is a compilation of cool tricks you can do with GitHub Copilot using VS Code.
Type Descriptive Code
The easiest way to get started is to continue with your typical coding. If you type descriptive variables or function names, GitHub Copilot can provide better code suggestions.
You can also cycle through the inline suggestions using shortcut keys in your favorite IDE.
Type Comments First
The challenge of typing very descriptive variables or function names is they get very lengthy, and you risk having your favorite code format tool complaining about the line length.
So, it helps to write them as comments first before you write your code. You can comment above the function you want to create or even at the top of the file.
The most significant advantage of this approach is if your multline comments are clear and precise, it sets proper guardrails for GitHub Copilot to provide better code suggestions based on the context you define (i.e., less hallucination).
View Multiple Suggestions
Sometimes, you want to browse all suggestions all at once.
In that case, identify the shortcut key listed under github.copilot.generate first.
Then, type the comment and press the shortcut key in the next new line to open up the suggestions in a new tab. You can browse the suggestions and accept the one you like.
Generate Test Cases
One of the best uses of GitHub Copilot is the ability to create test cases, especially if your function has excellent documentation.
Here’s another example where you want to generate multiple assertions.
Perhaps you don’t want to lump multiple assertions into one test case because if one of the assertions fails, the entire test case fails.
In this example, I’m using the pytest framework, which has a decorator that allows the parameterization of the test function’s argument(s). To refactor the existing test case, GitHub Copilot Labs’ custom brush converts a single test case into 5 test cases.
Internationalization
If you are building an app that users worldwide will use and want to serve the text based on regions, you can easily convert the text to different spoken languages.
A faster way to perform language conversion is to use GitHub Copilot Labs’ custom brush to convert multiple lines simultaneously.
Generate Realistic Test Data
Sometimes, you want to generate realistic data for your program or test cases.
You can also define the preferred data structure.
Perhaps you want to know what world leaders like to eat. As you can see, GitHub Copilot can determine the country leaders and associate the dishes relevant to that country.
Learn IDE’s Shortcut Keys
I have used IntelliJ IDEA as my primary IDE for almost 20 years. My brain is fully wired with that particular IDE’s shortcut keys. To my disappointment, GitHub doesn’t create the GitHub Copilot Labs extension for JetBrains products.
To challenge myself, I want to learn similar shortcut keys for VS Code. While I can search the web for it, the easiest way is to do it directly in the code editor so that it doesn’t take my attention away during code development.
Converse with “q: ” and “a: ”
You can ask GitHub Copilot anything using the q: and a: construct, even though it’s unrelated to your codebase.
You can use the same construct to ask GitHub Copilot to explain the codebase.
You can even argue with GitHub Copilot and defend Chuck Norris’ honor!
Fix Bug
You can attempt to fix unobvious bugs. This feature is far from perfect., but it’s worth trying out.
List Steps
Perhaps you inherited a project from a previous team and struggled to understand the written code. In this case, you can list the steps in the codebase.
Explain Code
Maybe you don’t want to clutter the existing codebase with unnecessary comments because you don’t own the codebase. In this case, you can use GitHub Copilot to explain the code instead.
Translate Codebase to Another Programming Language
You can also translate a block of highlighted code to another programming language.
Copilot Labs Settings
If you often use the Copilot Labs features, there are 2 settings worth enabling to provide quick access to them.
Show Brushes Lenses
When enabled, all brush functions (fix bug, list steps, custom, etc.) are made available when you move your cursor within the function or highlight the code.
Show Test Generation Lenses
When enabled, it displays “Suggest test” on the function that quickly brings you to the Copilot Labs’ Test Generation section.
Please note the Copilot Labs’ Test Generation currently works with Javascript and Typescript (as of 2023-09-03).
What Else
Do you know other cool tricks that are not listed here? If so, drop your comments below. I’m interested to learn from you.
Leave a Reply