Improve your code quality with GitHub Copilot in Visual Studio

Cynthia Zanoni

Laurent Bugnion

In our previous post, we discussed GitHub Copilot’s Slash Commands, which allow you to trigger specific actions within Visual Studio with simple text-based inputs. Now, let’s explore the /optimize command and its potential to improve code quality in Visual Studio.

Refactoring with /optimize

In a recent exploration by Bruno Capuano, we see the transformative capabilities of GitHub Copilot’s /optimize command. Bruno demonstrates its prowess by refactoring a code snippet, showcasing how simple text-based inputs can yield significant improvements in code structure and performance.

One of the prime examples showcased by Bruno involves the conversion of a traditional for loop utilizing numerical indices into a more readable and intuitive foreach loop. While foreach loops in .NET might seem more verbose, they often offer better readability, a crucial aspect in maintaining code quality and ease of understanding.

Here’s a glimpse of the original for loop snippet:

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;

To provide context to Copilot, Bruno selects the entire loop. He then initiates the inline chat dialog by typing “Alt-/”.

GitHub Copilot Chat dialog showing selected code with instructions to enhance code quality using the /optimize command in Visual Studio's chat interface

To guide Copilot in refactoring the code, Bruno types a Slash ‘/’, which opens the previously discussed dialog. He chooses the Optimize command from the menu and sends the command to Copilot.

Copilot responds with several optimization suggestions:

  • The ChatMessage instance can be initialized during construction, enhancing efficiency.
  • A foreach loop is utilized.

The refactored code appears as follows:

foreach (var message in chatHistory)
    var msg = new ChatMessage
        role = message.Role.ToString().ToLower(),
        content = message.Content

Learn from Copilot: Give it a Try!

The most effective way to comprehend a tool’s functionality is to use it firsthand. I find it fascinating to apply the /optimize command to my production projects. At times, the modifications are minor, affirming the quality of my original code. However, there are instances when Copilot suggests ingenious alterations, often introducing me to new syntaxes.

Validating the output of GitHub Copilot is also crucial to ensure it hasn’t introduced any errors or regressions. Unit tests can serve as a safety net for these changes, just as they do during the refactoring of any piece of code.

Additional Resources

We offer a wealth of resources to further your understanding of GitHub Copilot for Visual Studio. We encourage you to explore this collection, you can watch the full video here and regularly visit this blog for more content.

Feedback usabilla icon