GitHub Copilot is a program that gives you accurate and precise auto-complete suggestions for your code. These are based on GitHub’s Artificial Intelligence model.
This means that the Copilot program tries to understand what you are trying to write and suggest the best possible code for your intentions. This is a great way to save time, as well as learn new functions and modules.
Here is the link to download GitHub Copilot: GitHub Copilot
What can GitHub Copilot do?
The main idea behind GitHub Copilot is to help you write code faster and better by giving you auto-complete suggestions that seem to be based on your intentions. For instance, if you are trying to write a function that returns a string, you can see the string that is returned by the function in the auto-complete suggestions.
Notice how the last sentence that I wrote was actually generated by the Copilot program. This is because the Copilot program is able to understand what you are trying to write and suggest the best possible code for your intentions.
This is how auto-suggestions are presented to the user:
Now that we have seen how GitHub Copilot works with Markdown writing, let’s look at how to use it in our code. Currently, GitHub Copilot gives the best suggestions for these programming languages:
Personally, I have used GitHub Copilot when programming in Python and it worked really well. I am really happy with the results. Let’s have a look at some case scenarios where GitHub Copilot was useful to me.
Using Copilot in Python
One of the main things that I noticed was that it was really quick. Whenever I had a question like, “How do I reverse a string?”, I was able to find the answer in the auto-complete suggestions.
Let’s have a look at an example:
- I’d first write a comment using the # symbol telling Copilot what I wanted to achieve, like this:
# Reverse a string string = "Hello World!"
- Next up, Copilot would suggest a code snippet which indeed reversed my string.
# Reverse a string string = "Hello World!" reversed_string = string[::-1] print(reversed_string) # prints "!dlroW olleH"
- Finally, I would run the code and see that the string was indeed reversed.
This might seem like a small example, but I believe that it can help new programmers save some time and learn new functions and modules.
Let’s take a look at another example. For instance, if I needed to open a file and read text from a file which is stored in our Desktop in Python.
- I’d first write the comment:
# Open a file and read text from a file which is stored in our Desktop folder (C Drive)
- Next up, Copilot would suggest a code snippet which opens the file and reads the text.
# Open a file and read text from a file which is stored in our Desktop folder (C Drive) file = open("C:\\Users\\User\\Desktop\\file.txt", "r") text = file.read() print(text)
If I mention that we are actually using Linux’s folder structure:
# Open a file and read text from a file which is stored in our Desktop folder (C Drive) in Linux file = open("/home/user/Desktop/file.txt", "r") text = file.read() print(text)
Overall, it seems to work best when there is some code that is already written, so that Copilot can understand the purpose of your code and suggest personalised code based on your writing style.
I am very impressed after testing Copilot out. I’ve been hearing news about how fast Articial Intelligence is advancing but I never really got to test it myself. It was extremely fun to test GitHub Copilot out, it is probably the closest experience I have had to using Articial Intelligence directly.
I found it useful personally because it saved me some time which I would usually spend trying to figure out certain functions by reading documentation. We could say that I have become more productive thanks to Copilot.
However, at the same time, it doesn’t always generate good suggestions. I felt like it could get in the way sometimes by generating too many (bad) suggestions.
Fortunately though, it is possible to toggle the auto-complete suggestions on and off. If I decide to toggle the auto-complete suggestions off, I would press a certain key combination to only turn them on when needed. This way, I can focus on my code and only use it when I really need it.
For my work style, this toggle-off by default feature is really helpful and it is the best way to use Copilot for me.
I hope this article helped you to understand how to use GitHub Copilot and how to use it in your code.
Read more about Github Copilot on their website