DEV Community

Cover image for Explain and Translate Your Code With GitHub Copilot
Duncan Lew
Duncan Lew

Posted on • Updated on • Originally published at duncanlew.Medium

Explain and Translate Your Code With GitHub Copilot

Boost your understanding with natural language descriptions of your code

GitHub Copilot is the new kid in town with a unique set of features that even seasoned developers can use to their advantage. One of the main selling points of GitHub Copilot is its ability to provide suggested lines of code assisted with AI. A specific feature that doesn’t get much attention is called Copilot Explain.

Copilot Explain, as the name suggests, will translate code into natural language descriptions. This can be an immense help to developers who are unfamiliar with a certain project or programming language. This feature can also translate code from one programming language to another. This is a boon for instances when you find a code snippet solution in a different programming language and need to translate it to the target language of the repository.

Requirements to get started

GitHub Copilot is free to use for students and open-source contributors. In all other cases, you can try it out with a trial to see if it fits your needs. In order to get started with GitHub Copilot, you will need an IDE. For this example, we will be using Visual Studio Code.

As of writing, the feature Copilot Explain is in technical preview and requires you to sign up for the tester group. Check out GitHub Next to see how you can be part of the technical preview.


1. Install the extensions for GitHub Copilot

Inside your Visual Studio Code IDE, install the following two extensions for Visual Studio Code:

Prompt to configure GitHub Copilot with your GitHub account

A popup will prompt you to configure Copilot. Follow the setup wizard to connect your GitHub account to GitHub Copilot.

2. Highlight the code to be explained

For this example, we are going to simulate a situation in which you’re working with an unfamiliar repository that uses the Kotlin programming language. Kotlin is a very modern language introduced in 2011 and it might not necessarily be a language that all developers are familiar with.

The code sample that we are going to use is from the Kotlin example page:

If you aren’t familiar with the Kotlin syntax, some parts might not necessarily make sense immediately and you might want to have a quick way to understand what this piece of code does without taking a deep dive into the intricate workings of Kotlin itself.

Make sure to have the example code sample available in a file inside Visual Studio Code as example.kt. Highlight all the lines in this file.

3. Explain the code

If the Github Copilot extensions are installed correctly, you should have an extra GitHub icon available in your vertical activity bar:
Vertical activity bar with the GitHub Copilot Labs icon

After clicking on the GitHub icon, your sidebar should like look this:
Explain section of the side bar

If you haven’t highlighted your code yet, make sure to do it now and then click on the button named Ask Copilot. It will show the result as a translation of the code into plain English:
Result of explaining the code into plain English

So that’s how you can retrieve a description of what unfamiliar code snippet does in plain English! 🤓

4. Translate the code into a different programming language

If you found this code sample in Kotlin, and would like to translate it into a language that you are familiar with, GitHub Copilot can help you out! Let’s try to translate the code from Kotlin into Python. The GitHub Copilot activity bar has a second section called Language Translation:
Language translation section in the side bar

Click on the blue button named Ask Copilot to translate your code. The end result will look like this:
Result of translating the code from Kotlin to Python<br>

There you go! You just translated a code snippet from Kotlin to Python! 🔎

Takeaway

GitHub Copilot can be a revolutionary code completion tool by suggesting lines of code to the developer based on a comprehensive machine learning model. It certainly isn’t a tool to replace the developer. It’s a tool to elevate the development experience as if there is someone next to you helping you out from time to time with good suggestions. This can especially be useful for boilerplate and repetitive code.

The new features that we introduced in this article for explaining and translating code are still in technical preview and gives us a glimpse into what the next step could be in AI-assisted programming. Make sure to keep an eye on tools like GitHub Copilot to see where the industry is heading next! 🚀




If the content was helpful, feel free to support me here:

Buy Me A Coffee

Top comments (0)