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:
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.
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:
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 moreRelated 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 ...
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 ...
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...
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...
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...
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...
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...
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,...
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...
Introducing Azure Product Retirement Livestreams
The Azure Retirements team, in collaboration with key partner groups, is excited t...