Explain and Translate Your Code With GitHub Copilot
Published: 2022-08-21 18:49:00

## 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](https://code.visualstudio.com/).

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](https://githubnext.com/projects/copilot-labs/) to see how you can be part of the technical preview.

<hr />

### 1. Install the extensions for GitHub Copilot

Inside your Visual Studio Code IDE, install the following two extensions for Visual Studio Code:
- [GitHub Copilot](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot)
- [GitHub Copilot Labs](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot-labs)

![Prompt to configure GitHub Copilot with your GitHub account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxx2e200rcugn7xm16q9.png)
 
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](https://play.kotlinlang.org/byExample/04_functional/01_Higher-Order%20Functions):
{% gist https://gist.github.com/duncanlew/4eaf75805938118beb65b93a8092f4b4 %}

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x6ylzhnbi0pncc77kvvl.png)
 
After clicking on the GitHub icon, your sidebar should like look this:
![Explain section of the side bar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bb5vzmovo4z4gr6vy166.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozqg1t5le2xcjpb9k6ui.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvmchbj51hg7zj5agxuy.png)

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
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ry7e44556iuytfl7hgq.png)

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! 🚀

<hr />
If the content was helpful, feel free to support me [here](https://www.buymeacoffee.com/duncanlew):
<a href="https://www.buymeacoffee.com/duncanlew" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>