Using GitHub Co-Pilot For Code Guidance

Introduction

With the growing trend in AI these days, there has been a major focus on tools that can help the developer code better, provide guidance, and also write some tests, etc. One of the main tools in the C# and Microsoft Visual Studio world is GitHub Copilot. This seamlessly integrates into our development environment (Visual Studio, Visual Studio code, etc.) and gives some code guidance based on comments and other code snippets we add. Looks interesting.

So, let's give it a try.

Setting up GitHub Copilot

I started by signing up for the free trial version at (https://github.com/features/copilot)

Using GitHub Co-pilot for code guidance

The below gives a brief description of what GitHub Copilot is, at this URL.

Using GitHub Co-pilot for code guidance

Once you have signed up, you are now ready to pair your development environment with the GitHub Copilot license you have signed up for.

Using GitHub Co-pilot for code guidance

In my case, I choose Visual Studio. It then takes us to a page with details on how to add the extension to our Visual Studio environment.

Using GitHub Co-pilot for code guidance

The steps are simple to follow. Just ensure you have the right version. In my case greater than 17.2.

Using GitHub Co-pilot for code guidance

Writing code with help from GitHub

Now that we have installed the GitHub Pilot extension, we can create a C# console application and test the utility of this tool.

Using GitHub Co-pilot for code guidance

Using GitHub Co-pilot for code guidance

I first added a comment to read a text file. I got the below suggestions.

Using GitHub Co-pilot for code guidance

Looks great. I then entered the name of a function signature and got the below suggestion.

Using GitHub Co-pilot for code guidance

Finally, I entered a comment to add a unit test for this method.

Using GitHub Co-pilot for code guidance

I needed to do some modifications as I was running all the code from the Program.cs file.

using NUnit.Framework;
using NUnit.Framework.Internal;
// Read a text file 
string text = File.ReadAllText(@ "C:\Temp\Testfile.txt");
// Display the file contents to the console. Variable text is a string
Console.WriteLine("Contents of WriteText.txt = {0}", text);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
bool WhichNumberisgreater(int number1, int number2) {
    if (number1 > number2) {
        return true;
    } else {
        return false;
    }
}
Console.WriteLine(WhichNumberisgreater(10, 5));
// Unit test for the method
void WhichNumberisgreaterTest() {
    // Arrange
    int number1 = 10;
    int number2 = 5;
    bool expected = true;
    // Act
    bool actual = WhichNumberisgreater(number1, number2);
    // Assert
    Assert.AreEqual(expected, actual);
}
WhichNumberisgreaterTest();

Summary

The GitHub Copilot tool looks great and will certainly be a great help to developers to write better code. Again, as I mentioned in my previous article on ChatGPT, this will be a helper not a replacement for developers. This tool can however increase our productivity very much.