Programmers spend a lot of time writing code. Tools like “coder” software can help us with syntax suggestions, code snippets, debugging suggestions, etc. But what if there was a tool that used artificial intelligence (AI) to help us write more code? That’s what GitHub Copilot is all about.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
Meet GitHub Copilot – your AI pair programmer. https://t.co/eWPueAXTFt pic.twitter.com/NPua5K2vFS
— GitHub (@github) June 29, 2021
I was amazed at the idea of AI helping programmers write code (or even do all the heavy lifting), so I visited the site. GitHub Copilot.
All content aside, I scrolled to the bottom of the web to sign up for a trial of GitHub Copilot.
What is GitHub Copilot?
GitHub Copilot is an AI tool that gives you code recommendations based on the comments and context of the file you’re editing.
Copilot is the result of a partnership between GitHub and OpenAI, which is heavily supported by Microsoft. It is powered by a brand new AI system called Codex, based on the GPT-3 model.
GPT-3 stands for the third generation of Generative Pre-training Transformer – a language model capable of generating text strings from simple prompts. The Codex is derived from this model, which is not only capable of generating text, but also capable of generating code in some of the most popular languages.
Copilot has been trained with billions of lines of code from public repositories on GitHub, so your code may have improved this AI engine in some way (we’ll get into that later).
While it supports most programming languages, it currently works best with Python, JavaScript, TypeScript, Ruby, and Go.
Install Github Copilot
Copilot is extremely easy to set up. In case you have access to the technical preview just download the VS Code extension by searching for it on the tab Extension and enable it.
It then asks you to log into your GitHub account, and confirm you have access to the preview.
Currently, the only way to use Copilot is on VS Code and it may remain the same for a while according to Copilot’s page.
Most of the following examples will use Python, as it is one of the languages where this AI engine works well.
How Copilot Works
GitHub Copilot generates a variety of suggestions for you based on the context of the file you’re editing. Basically, it gives you recommendations based on the requirement you made in the file and the code you wrote earlier.
When Copilot has a code suggestion, it will ask you to use it. Let’s test Copilot by creating a function that calculates the mean of a data set. The only thing I will give Copilot is a description and name of the function.
As you can see, the gray text is suggested by Copilot and I can accept it by pressing Tab. But if I don’t like the first suggestion, I can browse through the others with Ctrl + ]or view a range of solutions from the side panel using Ctrl + Return.
Impressive isn’t it? But let’s pose another challenge. Now, Copilot must create a main function that allows the user to enter space-separated numbers. It will split these numbers up and pass the resulting list to the compute_average function, before printing the results.
Finally, I will tell Copilot to call main using __name__ == ‘__main__’.
And that’s how GitHub Copilot writes a function based solely on the commands I gave it. Of course, the code is not perfect. For example, the compute_average function could be reduced to sum (dataset) / len (dataset), but the overall result is pretty good.
Test Copilot with simple challenges
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.
Another wallet about the leap year function.
Or a simple palindrome checker.
Another cool thing about Copilot is that it can also provide suggestions in comments and docstrings. In the above example, it completed the palindrome.
Finally, is a simple password generator. I have provided a long description and the modules I want to use.
Copilot is very good at suggesting simple, compact solutions from our requirements.
Now let’s test how this tool performs in more complex challenges.
Use Copilot for complex challenges
First, use Copilot to solve common algorithmic problems. For example, iterative binary search.
If you don’t know how to code, don’t worry. This is one of the downsides of using this type of tool. You can implement the code provided by Copilot without really understanding what it means.
We’ll see more downsides later, but you should take this into account in case you have access to the preview.
Also, the above solution is great (can be extracted from the DSA GitHub repository). It’s readable code that only requires a little bit of analysis.
But you can’t always rely on Copilot’s recommendations. Typically, you’ll need to review recommendations a few times before integrating them into your code.
Conclusion
It’s impressive to see an AI-powered tool generate code. GitHub’s intention is not to replace programmers but to help them improve their productivity while coding, especially with repetitive tasks, such as writing docstrings in functions or class.
After using Copilot for a while, I noticed some issues, but overall it provides good (not perfect) code suggestions. I tested the solutions for a number of common scenarios and I am quite pleased with the results. I think Copilot should not be used by beginners.
The project is relatively new, so it’s not ideal when building a serious project, but in the future it could emerge as one of the most used code generation tools.
In addition, you can also create a simple website in 15 minutes using Github here.