Suddenly, in a blink of an eye, we all started to talk about AI, with particular attention to Generative AI, which produces assets, including code. Not long ago, OpenAI released ChatGPT, which received the spotlight from the media and even interest from people outside IT because of its capacity to sustain a dialog with a human and bring very specialized knowledge to answer questions. Before we continue, as a disclaimer, AI did not generate this very article.
In my previous post, I discussed that site reliability engineers (SREs) and other professionals used AI capabilities long before ChatGPT. Today I want to talk about an exciting application of AI to aid software engineers, developers, and SREs in coding with AI-powered pair programming. GitHub Copilot is one notorious example and is sold as an "AI pair programmer." It generates code based on the comments around the code's intention, and it's been gaining traction among the developer's ranks. By the end of this post, you will understand what's Copilot and how to start using it.
The short answer is not really. Unless the problem you're trying to resolve with code is simple enough to require a few functionalities, you must have solid software development abilities to complete the work, even with Copilot in the game. Copilot is an emerging coding productivity tool, not a no-code or low-code solution. Also, would you sell software that you wouldn't be able to explain how it works? Worse, would you buy software that no one knows how it works?
GitHub Copilot utilizes OpenAI Codex under the hood. Like ChatGPT, Codex is a descendant of the GPT-3 (third-generation Generative Pre-trained Transformer), a neural network machine learning model trained with data from the Internet and capable of generating any natural language text. On top of that, they used billions of code lines from public repositories to teach Codex. In essence, it can read natural language and forge lines of code in many languages, including "JavaScript, Go, Perl, PHP, Ruby, Swift and TypeScript, and even Shell," but it has more proficiency in Python.
Codex possesses four proven coding capacities:
With Copilot, it's possible to generate a new block of code by inserting a programming language comment with the description of the code logic. For example, if I type this comment in a JavaScript file:
// A function that takes in a string and returns a string with the first letter capitalized
It may suggest the following code snippet:
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
Another usage is through code recommendations. For instance, it may suggest lines of code to complete an unfinished function or procedure.
Not at all. According to the GitHub's privacy statement, "neither your Prompts nor Suggestions will be shared or used as suggested code for other users of GitHub Copilot." Of course, publishing your code and the comments used to generate code through Copilot on a public repository may defeat this data privacy.
No, it will construct a reasonable with minimum accuracy solution, but it does not necessarily mean the generated code perfectly fits your project requirements. You will need to validate if the Copilot recommendations make sense in the context of your project. Also, there's no guarantee the code suggestions are flawless or follow strict security and quality policies.
Now that you have a basic understanding of Copilot let's enable it on your laptop. You will need a GitHub account and an integrated development environment (IDE) tool installed on your machine.
You can sign up for free at this link if you don't have a GitHub personal account. I'm assuming you have one ready for the rest of the below instructions.
Image 1: Copilot page
The first task is enabling the Copilot feature for your GitHub account; you can do that by selecting one of the pricing plans here. When I wrote this article, there was a free trial option. Next, you need to indicate whether or not you want the code suggestions coming from public code and if Copilot can use your snippets to improve the model (re-train it).
After you enable it for your GitHub account, the second task is to install Copilot into your preferred IDE. I'm using Microsoft Visual Studio Code (VS Code), but they provide a package for many other IDEs; you can see the list and specific installation procedure here. Also, if you want, you can download and install VS Code from this official website.
Image 2: Copilot extension
For the VS Code tool, the installation process is straightforward:
After you have the Copilot extension, it's time to test it. Just open a project folder or create a new one.
Then create a new file; you can name it after copilot-test.js. If you don't have the JavaScript extension, it will prompt you to install it. Let's craft a comment that will trigger Copilot to generate code. Enter the following statement:
// A function that reads a JSON file and returns the number of fields in the JSON object
Then, hit the CTRL+Enter to see possible solutions in a separate window. By the way, you can see all the keyboard shortcuts in this document for VS Code. It can bring up to 10 possible recommendations. Check the one that matches your style and click on the "Accept Solution" link at the end of the section.
Although it may not be the full-fledged AI-fueled programmer we see in the movies, GitHub Copilot helps developers with the initial algorithms and basic coding patterns. It might benefit individual developers and entire companies if seen as a software development productivity tool instead of a mythic autonomous programmer bot.
I hope you enjoyed this blog post, and if you liked it, please connect to me on LinkedIn or follow me on Twitter @ranami. Also, my book Becoming a Rockstar SRE is out.