I'm thrilled to share a guest post today from a respected former colleague of mine, Madhuri Mittal. Madhuri is the voice behind the insightful software engineering blog, SEChannel, where she regularly shares her thoughts on all things related to the software development world. In this post, she turns her experienced eye towards GitHub CoPilot, the AI-powered programming tool that's been making waves in the developer community. But just how revolutionary is it, and what can it bring to your programming toolbox? Let's dive into Madhuri's insights to find out more.
Over to you, Madhuri!
GitHub Copilot is the new AI powered programming tool, which can be used to assist with various programming tasks within the development environment you are using. The AI tool gets integrated within your code editor and provides the assistance throughout, without leaving the code editor window. Rightly named, it functions like a co-programmer pairing in real time- creating, enhancing and documenting code.
It is based on OpenAI Codex, a powerful artificial intelligence system that can generate code from natural language prompts. OpenAI Codex is trained on billions of lines of code from public GitHub repositories and other sources, so it can understand the context and syntax of different programming languages. Copilot uses OpenAI Codex to suggest code snippets. Open AI Codex can understand various programming languages, frameworks, and domains, and can even answer questions about code. It is a powerful tool for developers, learners, and anyone who wants to create software with ease and fun.
The above image was created with DALL-E, prompt to DALL-E was:
“AI pair programming with Github copilot anime cartoon“
Copilot can help you with various coding tasks, such as writing functions, tests, loops, queries, algorithms, and more. It can also generate comments, documentation, and examples for your code. It is not a replacement for human programmers, but rather a tool that augments their capabilities and creativity. It is still in technical preview and may not always produce correct or optimal code. You should always review and test the code generated by CoPilot before using it in your projects. You should also follow the best practices and guidelines for your programming language and framework, as well as the license and terms of service of CoPilot.
I explored it with Visual Studio 2022 and C# code, however Copilot works with various code editors and programming languages. As for setup, it works as an extension for Visual Studio and Visual Studio Code. At the time of this writing, you have to install it with Visual Studio 2022 version 17.4.4 or later and follow some steps. GitHub and Microsoft have several articles on how to set it up with Visual Studio, however at the end of this article I have listed links which will help you set up GitHub CoPilot with Visual Studio.
Briefly here are the steps:
Sign up for the GitHub Copilot technical preview waitlist at https://copilot.github.com/. You will receive an email from GitHub when you get access to the preview.
Install Visual Studio 2022 version 17.4.4 or later from https://visualstudio.microsoft.com/downloads/.
In Visual Studio 2022, go to Extensions > Manage Extensions and search for GitHub Copilot. Download and install the extension.
Restart Visual Studio 2022 and authorize the GitHub Copilot extension with your GitHub account.
Accept the telemetry terms to join the GitHub Copilot technical preview.
Created by DALL-E Prompt used:
“Step by step instructions to install represented as digital art“
Once you have done these steps, you can start using Copilot to write code faster and smarter. You can use natural language comments or method names to trigger suggestions, and press Tab to accept a suggestion. You can also use the Copilot panel to see alternative suggestions or tweak the settings. Copilot will suggest code and entire functions based on your context and comments. You can press Tab to accept a suggestion, or keep typing to ignore it.
The most useful feature of GitHub Copilot is Ask Copilot- which you can initiate by Right clicking on the code editor to show the context menu which will present the Ask Copilot context menu option.
This is where it gives you the experience of ChatGPT embedded within your IDE and Code editor helping you add, refine and document code.
GitHub Copilot is not a replacement for human programmers, but rather a tool that augments their capabilities and creativity. It is still in technical preview and may not always produce correct or optimal code. You should always review and test the code generated by GitHub CoPilot before using it in your projects. You should also follow the best practices and guidelines for your programming language and framework, as well as the license and terms of service of GitHub CoPilot.
For example some prompts you can use are:
“Create a Startup class for this project“
“Refactor Program.cs to use the Startup class“
“Add a xunit based unit test for my StreamController class“
“Create a new class Sale which has properties Item and Amount“
This creates the Sale class with the properties with relevant types.
As you can see in the image on the left you get an option to either Create new file or Insert at Caret. Create a new files adds the class to the project.
Insert at Caret will insert the code where the cursor is on the editor. You can keep enhancing and improving the same code.
Then I asked “Add a property Active to the class Sale” and it produced the output as in the above image.
It automatically inferred Active as a boolean and allows to insert to the class. While this is a simple and straight forward feature it can save typing time by creating classes, interfaces and other code, and add them to the project or existing code. This takes the drudgery out of programming by automating them and saving time for harder and more complex programming.
Here are some of the other tasks I was able to accomplish:
Created multiple API controller classes and added constructors with dependency injection and added Get methods.
Added XUnit based test code for the controller classes.
Asked the copilot to explain code, framework classes and create documentation.
Improved code to make it more succinct.
Wrapped a code around try-catch blocked.
Asked to explain the meaning of a code block.
Given a block of code, wrap it around a try-catch block.
Another task I asked Copilot to do was code review the below class:
My prompt was:
“Do a code review of the class StreamController“
Here is the class:
[ApiController]
[Route("[controller]")]
public class StreamController : Controller
{
private readonly OpenAIAPI _openAIApi;
private readonly Conversation _conversation;
public StreamController(OpenAIAPI openAIApi)
{
_openAIApi = openAIApi;
_conversation = _openAIApi.Chat.CreateConversation();
}
[HttpGet("{inputParam}")]
public async IAsyncEnumerable<string> Get(string inputParam)
{
_conversation.AppendUserInput(inputParam);
var sb = new StringBuilder();
await foreach (var res in _conversation.StreamResponseEnumerableFromChatbotAsync())
{
sb.Append(res.ToString());
}
yield return sb.ToString();
}
}
}
Here are the code review comments by Copilot:
I also explored using natural language commenting for it to suggest a code snippet. For example as shown in the screenshot below I typed a comment in the code as here below:
// This method makes a Post call to OpenAI API and returns the response
The Copilot typed up the rest of the code for me line by line as I accepted it by pressing Tab.
Some tasks I could not accomplish were like adding a Unit test project to an existing Solution. While it can generate unit test code for individual classes and methods it cannot add a scaffolded unit test project to the existing solution. I was hoping that it can create a unit test project and add the generated classes to it. I was also not able to refactor an existing Program.cs to create a new Startup, add that to the project and refactor it appropriately to use that Startup class without me intervening manually to add the services to the service collection and other start up code. While it can understand the context of the program and create code and classes based on that it cannot accomplish multiple steps of refactor or creating a basic templated project and adding generated code to it.
It’s hard to say whether some of these tasks are possible or not in the future based on how the models are trained, however I am hoping there will be many more automations to come which will eliminate the drudgery out of repetitive and mundane coding tasks and make them more fun, faster and easier to accomplish. It is also an extremely useful tool to learn programming and understand frameworks, SDKs and APIs you are using to build your solutions.
The fact that you can remain within your code editor and find all the answers to the questions without having to switch to google search, ChatGPT, Stack Overflow etc. is a tremendous time saver – plus the added benefit of generating code and adding to the ongoing code base. There is already a lot of interest around Copilot, many companies have made this a part of their day today programming.
These are some of my initial experiments with it. I used a simple and brand new project to explore the tool and some of it’s features. It would be interesting to see how this can be used to review and optimize existing complex code. I am still in the process of exploring it further, and give the right natural language comments and the right prompts to Ask Copilot.
I highly recommend you use this for your programming projects and efforts.
Happy programming!
If you enjoyed this post, please follow Madhuri’s blog on SEChannel. Link to the Original Post on her blog can be found here: Exploring GitHub CoPilot. And of course, if you would like to be notified of more content like this, please consider subscribing to Prompts.Finance using the link below, it is free!