Loading...

Optimizing your code with GitHub Copilot for Visual Studio

Optimizing your code with GitHub Copilot for Visual Studio

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!

Published on:

Learn more
Azure Developer Community Blog articles
Azure Developer Community Blog articles

Azure Developer Community Blog articles

Share post:

Related posts

This Month in Azure Static Web Apps | 09/2024

    We are back with another edition of the Azure Static Web Apps Community! :party_popper:   September was yet another month ...

1 year ago

GitHub Copilot for Azure: 6 Must-Try Features

As developers, we are constantly seeking tools that streamline our workflows and boost productivity. … Enter GitHub Copilot for Azure, now in ...

1 year ago

Responsible AI Mitigation Layers

Generative AI is increasingly being used in various kinds of systems to augment humans and infuse intelligent behavior into existing and new a...

1 year ago

Streamline Your Azure Workflow: Introducing GitHub Copilot for Azure in VS Code

I'm excited to announce the public preview of GitHub Copilot for Azure - a new addition to your toolkit that seamlessly integrates with G...

1 year ago

Build Intelligent Apps Code-First with Prompty and Azure AI

      Building Generative AI applications can feel daunting for traditional app developers. What does the end-to-end applicati...

1 year ago

Certificación AI-900 (Fundamentos de IA) con Chicas en IA

La inteligencia artificial ha llegado para quedarse, ¡y más aún con la revolucionaria IA generativa! Para ayudar a los profesionales a especia...

1 year ago

Get certified with Learn Live GitHub series!

GitHub Universe is coming, and Microsoft and GitHub are partnering to offer a new special Learn Live series in Brazilian Portuguese, English a...

1 year ago

Certifícate con Learn Live GitHub en Español

Microsoft y GitHub se han unido para ofrecer una nueva serie especial de Learn Live en inglés y español: GitHub 2024. Del 10 al 24 de Octubre,...

1 year ago

Evaluating generative AI: Best practices for developers

As a developer working with generative AI, you've likely marveled at the impressive outputs your models can produce. But how do you ensure the...

1 year ago

Introducing Azure Product Retirement Livestreams

The Azure Retirements team, in collaboration with key partner groups, is excited t...

1 year ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.