Programmers spend a lot of time writing code. Tools like code editors can help us along with syntax suggestions, snippets, debugging suggestions, and so on. But what if we had a tool that used artificial intelligence (AI) to help us write much more substantial portions of code? That’s what GitHub Copilot is all about.
I was recently scrolling through Twitter when I saw this tweet from the official GitHub account:
— GitHub (@github) June 29, 2021
I was amazed by the idea of AI helping me to write code (or even do all the heavy work), so I went ahead and visited the GitHub Copilot page.
Skipping all the content, I went to the bottom of the page searching for a way to test this out. I encountered a banner that was a call to action to sign up for GitHub Copilot technical preview.
After some days (or weeks) waiting, I got granted access to the technical preview, and now I can let AI code for me … or can I?
Read more to learn what GitHub copilot is, my experience with it, and how it’ll impact you … or why maybe not.
What is Copilot?
Simply put, GitHub Copilot is an AI tool that provides you code suggestions based on comments and the context of the file you’re editing.
Copilot is the result of a collaboration between GitHub and OpenAI, which is heavily backed by Microsoft. It’s powered by a brand new AI system named Codex, which is based on the GPT-3 model.
GPT-3 stands for the third generation of the Generative Pre-trained Transformer — a language model capable of generating sequences of text from simple prompts. Codex is derived from this model, which is capable not only of text, but also code generation in some of the most popular languages.
Copilot has been trained with billions of lines of code from publicly available repositories on GitHub, so your code has probably improved this AI tool in some way (we’ll get into details later).
Let’s see how GitHub Copilot works, and what it’s currently capable of.
GitHub Copilot in Action
Copilot is incredibly easy to install. In case you have access to the technical preview, just download the VS Code extension by searching for it on the Extension tab and activating it.
It then requires you to log in to your GitHub account, so it can confirm you have access to the technical preview.
For now, the only way to use Copilot is on VS Code, and it may remain the same for some time according to Copilot’s page.
Most of the following examples will be using Python, since it’s one of the languages this AI tool is really good with.
How Code Suggestions Work
GitHub Copilot generates multiple suggestions for you based on the context of the file you’re editing. Mainly, it gives you suggestions based on the comments you’ve made in the file, and the code you’ve written before.
Once Copilot has a code suggestion, it’ll ask you to use it. Let’s test out Copilot by creating a function that computes the average of a dataset. The only thing I’ll provide to Copilot is a comment and the name of the function.
As you can see, the text in gray is suggested by Copilot, and I can accept it by pressing Tab. But if I don’t like the first suggestion, I can walk through more suggestions with Ctrl + ], or see a bunch of solutions from a side panel with Ctrl + Return.
Impressive, it isn’t? But let’s set a different challenge. Now, Copilot has to create a
main function that lets the user enter some space-delimited numbers. It should split these numbers and pass the resulting list to the
compute_average function, before printing the result.
Lastly, I’m going to ask Copilot to call the main function using the execution entry point
__name__ == '__main__'.
And that’s how GitHub Copilot wrote a functional Python script based only on the commands I gave to it. Of course, the code isn’t perfect. For instance, the
compute_average function could be reduced to
sum(dataset) / len(dataset), but the overall result is pretty good.
Testing Copilot with Simple Problems
Let’s start with the function every developer must know: FizzBuzz. I’ll write the problem statement, name the function and let Copilot do the work
What about a leap year function? In this case, I’ll only provide a simple docstring.
Now, a simple palindrome checker.
Another neat thing about Copilot is that it can also provide suggestions in comments and docstrings. In the above example, it completed the definition of a palindrome!
Lastly, a simple password generator. I provided a long description, and the modules I wanted to use. Surprisingly, I got exactly what I wanted.
To conclude this section, Copilot is extremely good at suggesting simple, bite-sized solutions from our comments.
Now let’s test how this AI pair programmer performs in more complex environments.
Complex Copilot usage
First, let’s use Copilot to solve common algorithms problems. For example, an iterative binary search implementation.
Don’t worry if you don’t understand the code; at first, I didn’t either. Here comes one of the downsides of using this kind of tool. You may implement code provided by Copilot without actually understanding its meaning.
We’ll see more downsides later, but you should take this into account in case you have access to the technical preview.
Aside from this, the solution above is excellent (probably extracted from a DSA GitHub repository). It’s readable code that, with few moments of analysis, I managed to understand.
But you can’t always rely on Copilot suggestions. Most of the time you’ll need to review the suggestions a couple of times before integrating them into your codebase.
We’ve tested Copilot with simple problems and algorithms. It would be a good idea to use it in real-world solutions.
What is GitHub Copilot? An AI Pair Programmer for Everyone
Source: Site Point
Republished by Blog Post Promoter