Optimizing your code with GitHub Copilot for Visual Studio
Published Apr 09 2024 12:02 AM 2,662 Views
Microsoft

In our previous post, we talked about so-called Slash commands in GitHub Copilot, and how they can be used as a kind of "magic spell" to ask Copilot to execute actions within Visual Studio. We did mention the /optimize command, amongst many others. In the new short video I just posted, my colleague Bruno Capuano is with us again, and showing a demo of the /optimize command, and how he uses it to refactor a piece of code.

 

 

Refactoring with GitHub Copilot

In the example, Bruno takes classic "for" loop using a numerical index and refactors it to use a "foreach" loop. In general, I also prefer to use "foreach" loops in .NET as I feel that they are more readable than the more classic "for" syntax. That being said, the "for" syntax is usually faster, so use this wisely!

 

Here is the original "for" loop:

 

 

 

for (int i = 0; i < chatHistory.Count; i++)
{
    var message = chatHistory[i];
    var msg = new ChatMessage();
    msg.role = message.Role.ToString().ToLower();
    msg.content = message.Content;
    root.messages.Add(msg);
}

 

 

 

 

At this point, Bruno selects the whole loop in order to give the context to Copilot. Then he types "Alt-/" which opens the inline chat dialog.

 

LBugnion_0-1712605259714.png

 

To instruct Copilot to refactor the code, Bruno then types a Slash '/' which opens the dialog we saw in the previous post. From the menu, he selects the Optimize command, and sends the command to Copilot.

 

Here, Copilot proposes multiple optimizations.

  • The "ChatMessage" instance can be initialized right as it is constructed, which is more efficient.
  • The "foreach" loop is used.

And so, the result becomes:

 

 

 

foreach (var message in chatHistory)
{
    var msg = new ChatMessage
    {
        role = message.Role.ToString().ToLower(),
        content = message.Content
    };
    root.messages.Add(msg);
}

 

 

 

 

Try it for yourself and learn from Copilot!

Of course, the best way to understand how any tool works is to try it out by yourself. I find extremely interesting to open any production project I own, and to try the /optimize command on it. Sometimes the changes are minimal, and I can pat my own shoulder and think I did a good job writing the original code. But sometimes Copilot proposes some clever changes, and it often happens that I learn from the changes, for example a new syntax I didn't know existed.

 

Don't forget to validate

Of course, as I have said multiple times in my previous posts, it's very important to verify the output of GitHub Copilot, and make sure that it didn't introduce errors or regressions. This is where unit tests will help you feel safer about the changes, just like any time you refactor any piece of code. 

 

We have additional resources to learn about GitHub Copilot for Visual Studio in this collection and encourage you to check them out. And of course, check this blog often for additional content!

Co-Authors
Version history
Last update:
‎Apr 09 2024 05:33 AM
Updated by: