{"id":248901,"date":"2024-05-16T05:01:13","date_gmt":"2024-05-16T12:01:13","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/visualstudio\/?p=248901"},"modified":"2024-05-16T05:01:13","modified_gmt":"2024-05-16T12:01:13","slug":"improve-your-code-quality-with-github-copilot-in-visual-studio","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/improve-your-code-quality-with-github-copilot-in-visual-studio\/","title":{"rendered":"Improve your code quality with GitHub Copilot in Visual Studio"},"content":{"rendered":"<p>In <a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/mastering-slash-commands-with-github-copilot-in-visual-studio\/\">our previous post,<\/a> we discussed <a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/mastering-slash-commands-with-github-copilot-in-visual-studio\/\">GitHub Copilot\u2019s Slash Commands<\/a>, which allow you to trigger specific actions within Visual Studio with simple text-based inputs. Now, let&#8217;s explore the <code>\/optimize<\/code> command and its potential to improve code quality in Visual Studio.<\/p>\n<h2><strong>Refactoring with \/optimize<\/strong><\/h2>\n<p>In a recent exploration by Bruno Capuano, we see the transformative capabilities of GitHub Copilot&#8217;s <code>\/optimize<\/code> 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.<\/p>\n<p><iframe title=\"Optimizing your code with GitHub Copilot for Visual Studio #githubcopilot #visualstudio\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/4rvM33NjIDI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>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.<\/p>\n<p>Here&#8217;s a glimpse of the original for loop snippet:<\/p>\n<pre class=\"lia-code-sample line-numbers language-csharp\" tabindex=\"0\"><code class=\"language-csharp\"><span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\"><span class=\"token keyword\">int<\/span><\/span> i <span class=\"token operator\">=<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> chatHistory<span class=\"token punctuation\">.<\/span>Count<span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">++<\/span><span class=\"token punctuation\">)<\/span>\r\n<span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token class-name\"><span class=\"token keyword\">var<\/span><\/span> message <span class=\"token operator\">=<\/span> chatHistory<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token class-name\"><span class=\"token keyword\">var<\/span><\/span> msg <span class=\"token operator\">=<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token constructor-invocation class-name\">ChatMessage<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    msg<span class=\"token punctuation\">.<\/span>role <span class=\"token operator\">=<\/span> message<span class=\"token punctuation\">.<\/span>Role<span class=\"token punctuation\">.<\/span><span class=\"token function\">ToString<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">ToLower<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    msg<span class=\"token punctuation\">.<\/span>content <span class=\"token operator\">=<\/span> message<span class=\"token punctuation\">.<\/span>Content<span class=\"token punctuation\">;<\/span>\r\n    root<span class=\"token punctuation\">.<\/span>messages<span class=\"token punctuation\">.<\/span><span class=\"token function\">Add<\/span><span class=\"token punctuation\">(<\/span>msg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/code><\/pre>\n<p>To provide context to Copilot, Bruno selects the entire loop. He then initiates the inline chat dialog by typing \u201c<code>Alt-\/<\/code>\u201d.<\/p>\n<p><img decoding=\"async\" class=\"alignnone\" src=\"https:\/\/techcommunity.microsoft.com\/t5\/image\/serverpage\/image-id\/569327i008F3A2D30C7AF33\/image-size\/medium?v=v2&amp;px=400\" alt=\"GitHub Copilot Chat dialog showing selected code with instructions to enhance code quality using the \/optimize command in Visual Studio's chat interface\" width=\"400\" height=\"219\" \/><\/p>\n<p>To guide Copilot in refactoring the code, Bruno types a Slash \u2018<code>\/<\/code>\u2019, which opens the previously discussed dialog. He chooses the Optimize command from the menu and sends the command to Copilot.<\/p>\n<p>Copilot responds with several optimization suggestions:<\/p>\n<ul>\n<li>The <code>ChatMessage<\/code>\u00a0instance can be initialized during construction, enhancing efficiency.<\/li>\n<li>A <code>foreach<\/code>\u00a0loop is utilized.<\/li>\n<\/ul>\n<p>The refactored code appears as follows:<\/p>\n<pre class=\"lia-code-sample line-numbers language-csharp\" tabindex=\"0\"><code class=\"language-csharp\"><span class=\"token keyword\">foreach<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\"><span class=\"token keyword\">var<\/span><\/span> message <span class=\"token keyword\">in<\/span> chatHistory<span class=\"token punctuation\">)<\/span>\r\n<span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token class-name\"><span class=\"token keyword\">var<\/span><\/span> msg <span class=\"token operator\">=<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token constructor-invocation class-name\">ChatMessage<\/span>\r\n    <span class=\"token punctuation\">{<\/span>\r\n        role <span class=\"token operator\">=<\/span> message<span class=\"token punctuation\">.<\/span>Role<span class=\"token punctuation\">.<\/span><span class=\"token function\">ToString<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">ToLower<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>\r\n        content <span class=\"token operator\">=<\/span> message<span class=\"token punctuation\">.<\/span>Content\r\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span>\r\n    root<span class=\"token punctuation\">.<\/span>messages<span class=\"token punctuation\">.<\/span><span class=\"token function\">Add<\/span><span class=\"token punctuation\">(<\/span>msg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/code><\/pre>\n<h3><strong>Learn from Copilot: Give it a Try!<\/strong><\/h3>\n<p>The most effective way to comprehend a tool\u2019s functionality is to use it firsthand. I find it fascinating to apply the <code>\/optimize<\/code> 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.<\/p>\n<p>Validating the output of GitHub Copilot is also crucial to ensure it hasn&#8217;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.<\/p>\n<h3><strong>Additional Resources<\/strong><\/h3>\n<p>We offer a wealth of resources to further your understanding of GitHub Copilot for Visual Studio. We encourage you to explore <a href=\"https:\/\/learn.microsoft.com\/collections\/ng58uwrk6g53ox?WT.mc_id=academic-132376-lbugnion\">this collection<\/a>, you can watch the <a href=\"https:\/\/youtu.be\/z1ycDvspv8U\" target=\"_blank\" rel=\"noopener\">full video here<\/a> and regularly visit this blog for more content.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our previous post, we discussed GitHub Copilot\u2019s Slash Commands, which allow you to trigger specific actions within Visual Studio with simple text-based inputs. Now, let&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":24687,"featured_media":248902,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[6887,6888,6941,1388,6904,155],"tags":[1055,6946,4381,6924,6906,6944,287,12],"class_list":["post-248901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence","category-copilot","category-copilot-2024-series","category-extensibility","category-installation-and-updates","category-visual-studio","tag-ai","tag-cynthia-zanoni","tag-github","tag-github-copilot","tag-install","tag-laurent-bugnion","tag-tips-and-tricks","tag-visual-studio"],"acf":[],"blog_post_summary":"<p>In our previous post, we discussed GitHub Copilot\u2019s Slash Commands, which allow you to trigger specific actions within Visual Studio with simple text-based inputs. Now, let&#8217;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/248901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/24687"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=248901"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/248901\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/248902"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=248901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=248901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=248901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}