{"id":34219,"date":"2024-05-30T19:15:25","date_gmt":"2024-05-30T19:15:25","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=34219"},"modified":"2024-06-04T19:17:34","modified_gmt":"2024-06-04T19:17:34","slug":"c-refactoring-tools-in-visual-studio","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/c-refactoring-tools-in-visual-studio\/","title":{"rendered":"C++ Refactoring tools in Visual Studio"},"content":{"rendered":"<p><span data-contrast=\"auto\">Code refactoring is the process of restructuring existing code, while its outward functionality remains unchanged. By refactoring, you can simplify complex code structures, reduce code redundancy, and enhance code reusability. Visual Studio offers many tools to help refactor your C++ code. This article will delve into these tools, using the <\/span><a href=\"https:\/\/github.com\/bulletphysics\/bullet3\"><span data-contrast=\"none\">bullet3<\/span><\/a><span data-contrast=\"auto\"> open-source code as an example.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><a class=\"Hyperlink SCXW118026120 BCX8\" href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/ide\/refactoring\/rename?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"FieldRange SCXW118026120 BCX8\"><span class=\"TextRun Underlined SCXW118026120 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun CommentStart SCXW118026120 BCX8\" data-ccp-charstyle=\"Hyperlink\">Rename<\/span><\/span><\/span><\/a><span class=\"EOP SCXW118026120 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">The renaming tool is useful when changing a symbol name that appears in multiple locations within your code. Manual renaming can lead to errors if one or more instances of your function or variable are overlooked. However, the renaming tool ensures all instances are renamed accurately and seamlessly.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Consider the <\/span><code class=\"language-default\">ExampleEntryPhysicsServer<\/code> <span data-contrast=\"auto\">struct in the \u2018InProcessExampleBrowser.cpp\u2019 file. Suppose we want to rename the <\/span><code class=\"language-default\">m_menuLevel<\/code> <span data-contrast=\"auto\">variable to<\/span> <code class=\"language-default\">m_menuHierarchyLevel<\/code> <span data-contrast=\"auto\">to make it more descriptive for clarity. Visual Studio\u2019s <\/span><b><span data-contrast=\"auto\">Rename <\/span><\/b><span data-contrast=\"auto\">refactoring tool can accomplish this, by right-clicking on the variable name, selecting Rename, and then typing the new name. Visual Studio will automatically update all instances of <\/span><code class=\"language-default\">m_menuLevel<\/code> <span data-contrast=\"auto\">to<\/span> <code class=\"language-default\">m_menuHierarchyLevel<\/code><span data-contrast=\"auto\">, eliminating the need for manual renaming.<\/span><\/p>\n<p><img decoding=\"async\" class=\"aligncenter \" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/renameInt_example.gif\" alt=\"Gif showing a Rename example.\" width=\"1813\" height=\"746\" \/><\/p>\n<h3><a class=\"Hyperlink SCXW215663263 BCX8\" href=\"https:\/\/devblogs.microsoft.com\/cppblog\/use-ai-powered-rename-suggestions-to-rename-your-c-variables-in-visual-studio\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW215663263 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW215663263 BCX8\" data-ccp-charstyle=\"Hyperlink\">AI-Powered Rename Suggestions<\/span><\/span><\/a><span class=\"EOP SCXW215663263 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><a href=\"https:\/\/github.com\/features\/copilot\"><span data-contrast=\"none\">GitHub Copilot<\/span><\/a><span data-contrast=\"auto\">&#8216; s AI-Powered Rename Suggestions is a new feature in Visual Studio 2022 that generates dynamic name suggestions for any C++ identifier, aiding in achieving a balance between descriptiveness and brevity. This allows you to dedicate your efforts towards evaluating the proposed names, rather than generating new ones.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"> You can make this feature visible by selecting \u201cEnable rename suggestions\u201d <\/span><b><span data-contrast=\"auto\">Tools <\/span><\/b><span data-contrast=\"auto\">&gt; <\/span><b><span data-contrast=\"auto\">Options <\/span><\/b><span data-contrast=\"auto\">&gt;<\/span><b><span data-contrast=\"auto\"> GitHub<\/span><\/b><span data-contrast=\"auto\"> &gt; <\/span><b><span data-contrast=\"auto\">Copilot chat<\/span><\/b><span data-contrast=\"auto\"> &gt; <\/span><b><span data-contrast=\"auto\">Preview features<\/span><\/b><span data-contrast=\"auto\">.\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Let\u2019s use the AI-Powered Rename Suggestions to rename<\/span> <code class=\"language-default\">m_name<\/code> <span data-contrast=\"auto\">to a more descriptive one. First, right-click on the variable and choose Rename. In the Rename window that appears, click on the new sparkle icon to generate name suggestions. Copilot chat will then offer suggestions based on how the identifier is used and the naming rules in your code. Let\u2019s select <\/span><code class=\"language-default\">m_entryName<\/code><span data-contrast=\"auto\">, and then preview and apply the change.<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/AIPoweredRename_example.gif\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34239\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/AIPoweredRename_example.gif\" alt=\"Image AIPoweredRename example\" width=\"1272\" height=\"644\" \/><\/a><\/p>\n<h3><a class=\"Hyperlink SCXW214882095 BCX8\" href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/ide\/refactoring\/change-signature?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW214882095 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW214882095 BCX8\" data-ccp-charstyle=\"Hyperlink\">Change Signature<\/span><\/span><\/a><span class=\"TextRun SCXW214882095 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW214882095 BCX8\" data-ccp-parastyle=\"heading 2\">\u00a0<\/span><\/span><span class=\"EOP SCXW214882095 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span class=\"TextRun SCXW97679382 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW97679382 BCX8\">The <\/span><\/span><strong><span class=\"TextRun SCXW97679382 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun CommentStart SCXW97679382 BCX8\">Change Signature<\/span><\/span><\/strong><span class=\"TextRun SCXW97679382 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW97679382 BCX8\"> feature helps you <\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">modify<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\"> a function\u2019s parameters in a safe and efficient manner<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">.<\/span> <span class=\"NormalTextRun SCXW97679382 BCX8\">When<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\"> we want to provide <\/span><span class=\"NormalTextRun CommentStart SCXW97679382 BCX8\">a <\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">new parameter for a <\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">description<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\"> to the first instance of <\/span><\/span><code class=\"language-default\">ExampleEntryPhysicsServer<\/code><span class=\"TextRun SCXW97679382 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW97679382 BCX8\">\u00a0<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">constructor<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">, <\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">we will<\/span> <span class=\"NormalTextRun SCXW97679382 BCX8\">r<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">ight-click on <\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">its <\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">name and select the <\/span><span class=\"NormalTextRun CommentStart SCXW97679382 BCX8\">\u2018Quick Actions and <\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">Refactorings<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">&#8230;\u2019 option or use <\/span><span class=\"NormalTextRun ContextualSpellingAndGrammarErrorV2Themed SCXW97679382 BCX8\">the Ctrl<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">+. keyboard shortcut. Select the <\/span><\/span><strong><span class=\"TextRun SCXW97679382 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW97679382 BCX8\">Change Signature<\/span><\/span><\/strong><span class=\"TextRun SCXW97679382 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW97679382 BCX8\"> option to open a new window. Here, you can add, remove, or reorder parameters. <\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">We\u2019ll<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\"> add a <\/span><span class=\"NormalTextRun CommentStart CommentHighlightPipeRestV2 CommentHighlightRest SCXW97679382 BCX8\">constant string pointe<\/span><span class=\"NormalTextRun CommentHighlightPipeRestV2 SCXW97679382 BCX8\">r <\/span><span class=\"NormalTextRun SCXW97679382 BCX8\"><code class=\"language-default\">m_description<\/code><\/span><span class=\"NormalTextRun SCXW97679382 BCX8\">,<\/span><span class=\"NormalTextRun SCXW97679382 BCX8\"> by typing the type and name. Visual Studio will automatically update all function calls accordingly.<\/span><\/span><span class=\"EOP SCXW97679382 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><img decoding=\"async\" class=\"aligncenter \" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/changeSignature_example.gif\" alt=\"Gif displaying Change Signature example.\" width=\"1969\" height=\"912\" \/><\/p>\n<p><span class=\"TextRun SCXW176844331 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun AdvancedProofingIssueV2Themed SCXW176844331 BCX8\">Similar to<\/span><span class=\"NormalTextRun SCXW176844331 BCX8\"> renaming, changing functions one by one can result in errors and this tool can do it automatically to <\/span><span class=\"NormalTextRun SCXW176844331 BCX8\">eliminate<\/span><span class=\"NormalTextRun SCXW176844331 BCX8\"> those errors.<\/span><\/span><\/p>\n<h3><a class=\"Hyperlink SCXW165288718 BCX8\" href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/ide\/refactoring\/extract-function?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW165288718 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW165288718 BCX8\" data-ccp-charstyle=\"Hyperlink\">Extract Function<\/span><\/span><\/a><span class=\"EOP SCXW165288718 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><b><span data-contrast=\"auto\">Extract Function<\/span><\/b><span data-contrast=\"auto\"> is a great way to break down complex functions into smaller, more manageable parts, and allows for potential reusability of a block of code if similar functionality is needed elsewhere in the program. In this code, let\u2019s extract the block of code from the <\/span><code class=\"language-default\">ExampleBrowserThreadFunc<\/code> <span data-contrast=\"auto\">function that calculates and updates <\/span><code class=\"language-default\">deltaTimeInSeconds<\/code> <span data-contrast=\"auto\">and then decides whether to update graphics or reset the clock based on <code class=\"language-default\">deltaTimeInSeconds<\/code>. This block of code is a logical unit that performs a specific task, so it makes sense to extract it into a separate function. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">To do so, select the block of code that we want to extract, then right-click on the editor, then select the \u2018Quick Actions and Refactorings&#8230;\u2019 option. This will display the <\/span><b><span data-contrast=\"auto\">Extract Function<\/span><\/b><span data-contrast=\"auto\"> tool. Choosing that option will open a new window that will allow you to choose a new function name and placement. A new function will be set up at the chosen location, with a matching prototype in the header file. The initial code will be revised to invoke this function.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/extractFunction_example.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-34233 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/extractFunction_example.gif\" alt=\"Gif displaying an example of Extract Function.\" width=\"2232\" height=\"1440\" \/><\/a><\/p>\n<p><span class=\"TextRun SCXW228434137 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW228434137 BCX8\">This helps to avoid code duplication and makes your code easier to read and <\/span><span class=\"NormalTextRun SCXW228434137 BCX8\">maintain<\/span><span class=\"NormalTextRun SCXW228434137 BCX8\">.<\/span><span class=\"NormalTextRun SCXW228434137 BCX8\">\u00a0<\/span><\/span><span class=\"EOP SCXW228434137 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><a class=\"Hyperlink SCXW241120183 BCX8\" href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/ide\/refactoring\/implement-pure-virtuals?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW241120183 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW241120183 BCX8\" data-ccp-charstyle=\"Hyperlink\">Implement Pure Virtuals<\/span><\/span><\/a><span class=\"EOP SCXW241120183 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">When dealing with a base class that has pure virtual functions, Visual Studio can help implement these functions in all derived classes. In this source code, all pure virtual functions of the base class are implemented. However, for the sake of an example, I modified the <\/span><code class=\"language-default\">CommonRigidBodyBase<\/code> <span data-contrast=\"auto\">struct to include a pure virtual function <\/span><code class=\"language-default\">virtual void somePureVirtualFunction () = 0;<\/code><span data-contrast=\"auto\">. Now we will use <\/span><b><span data-contrast=\"auto\">Implement Pure Virtuals<\/span><\/b><span data-contrast=\"auto\"> to provide an implementation for <\/span><code class=\"language-default\">somePureVirtualFunction ()<\/code> <span data-contrast=\"auto\">in the <\/span><code class=\"language-default\">BasicExample<\/code> <span data-contrast=\"auto\">class.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">First, right-click on the derived class and select the \u2018Quick Actions and Refactorings&#8230;\u2019 option from the context menu or use Ctrl+. keyboard shortcut. Then select the Implement all Pure Virtuals for class <\/span><code class=\"language-default\">BasicExample<\/code> <span data-contrast=\"auto\">option, and the pure virtual method will be added automatically.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/implementPureVirtuals_example.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-34234 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/implementPureVirtuals_example.gif\" alt=\"Gif displaying an example of Implement Pure Virtuals.\" width=\"1272\" height=\"599\" \/><\/a><\/p>\n<p><span class=\"TextRun SCXW88640298 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW88640298 BCX8\">This saves more time especially if you have a lot of virtual functions, because instead of doing it one by one, this tool will implement all pure virtual functions automatically.<\/span><\/span><span class=\"EOP SCXW88640298 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><a class=\"Hyperlink SCXW253038575 BCX8\" href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/ide\/refactoring\/create-declaration-definition?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW253038575 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW253038575 BCX8\" data-ccp-charstyle=\"Hyperlink\">Create Declaration \/ Definition<\/span><\/span><\/a><span class=\"EOP SCXW253038575 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">This feature enables you to easily generate a function\u2019s declaration or definition. Let\u2019s consider adding a new function, <\/span><code class=\"language-default\">printExampleDetails<\/code> <span data-contrast=\"auto\">that takes a pointer to an <\/span><code class=\"language-default\">ExampleEntriesPhysicsServer<\/code> <span data-contrast=\"auto\">object and prints the details of all the examples registered in the server. We can use <\/span><b><span data-contrast=\"auto\">Create Declaration\/Definition<\/span><\/b><span data-contrast=\"auto\"> tool, to seamlessly declare it.\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Select the block of code, then right-click on the editor, then select the \u2019Quick Actions and Refactorings&#8230;\u2019 option from the context menu or use Ctrl+. keyboard shortcut. This will display the <strong>Create Declaration\/Definition<\/strong>\u00a0option, which will automatically create the declaration for that function. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/createDeclaration_example.gif\" alt=\"Gif displaying an example of Create Declaration \/ Definition. \" width=\"2943\" height=\"888\" \/><\/p>\n<h3><a class=\"Hyperlink SCXW143789873 BCX8\" href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/ide\/refactoring\/move-definition-location?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW143789873 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW143789873 BCX8\" data-ccp-charstyle=\"Hyperlink\">Move Definition Location<\/span><\/span><\/a><span class=\"EOP SCXW143789873 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span class=\"TextRun SCXW113817524 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW113817524 BCX8\">Now that we have seen how to declare a function, we can move a function\u2019s definition to its corresponding header file. In the \u2018ExampleEntries.cpp\u2019 file, we have a\u00a0<\/span><\/span><code class=\"language-default\">ExampleEntriesAll::registerExampleEntry<\/code><span class=\"TextRun SCXW113817524 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW113817524 BCX8\">\u00a0function. For code reusability, <\/span><span class=\"NormalTextRun SCXW113817524 BCX8\">let\u2019s<\/span><span class=\"NormalTextRun SCXW113817524 BCX8\"> move the definition of this function to a header file.<\/span><\/span><span class=\"EOP SCXW113817524 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">To move function location, right-click on the function, then select the \u2018Quick Actions and Refactorings&#8230;\u2019 option from the context menu or use Ctrl+. keyboard shortcut. Then choose the <\/span><b><span data-contrast=\"auto\">Move Definition Location<\/span><\/b><span data-contrast=\"auto\"> option, to move the function to the corresponding header file.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/moveFunctionDefinition-_example.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-34236 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/moveFunctionDefinition-_example.gif\" alt=\"Gif displaying an example of Move Function Definition.\" width=\"1623\" height=\"667\" \/><\/a><\/p>\n<p><span data-contrast=\"auto\">This feature can be very useful when you want to organize your code by moving function definitions to appropriate header files without having to do it manually.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><a class=\"Hyperlink SCXW27942668 BCX8\" href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/ide\/refactoring\/convert-to-raw-string-literal?view=msvc-170\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW27942668 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW27942668 BCX8\" data-ccp-charstyle=\"Hyperlink\">Convert to Raw String Literal<\/span><\/span><\/a><span class=\"EOP SCXW27942668 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p>Next, let\u2019s look at a string with special characters or escape sequences. For example, if you have a string\u00a0<code>Torus (Shape Match)<\/code>\u00a0and you want to add quotation marks around\u00a0<code>Torus<\/code>, you would normally need to use escape characters, like this:\u00a0<code>\\\"Torus\\\" (Shape Match)<\/code>. However, this can make the string harder to read, especially if there are many special characters. To solve this problem, we can convert them to raw string literals by prefixing the string with\u00a0<code>R<\/code>\u00a0and enclosing it in parentheses, like this:\u00a0<code>R\"(\"Torus\" (Shape Match))\"<\/code>. This tells the program to interpret all characters within the parentheses as literal characters, not special characters.<\/p>\n<p>To convert to <strong>Raw String Literal<\/strong>, right-click on the string and then select the \u2018Quick Actions and Refactorings\u2026\u2019 option from the context menu or use the Ctrl+. keyboard shortcut. Then, selecting the \u2018Convert to Raw String Literal\u2019 option will complete the process. Visual Studio will preserve the exact content of the string.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/rawStringLiteral_example.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-34237 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/rawStringLiteral_example.gif\" alt=\"Gif displaying an example of Raw String Literal.\" width=\"1236\" height=\"438\" \/><\/a><\/p>\n<h3><a class=\"Hyperlink SCXW157291678 BCX8\" href=\"https:\/\/devblogs.microsoft.com\/cppblog\/convert-macros-to-constexpr\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"FieldRange SCXW157291678 BCX8\"><span class=\"TextRun Underlined SCXW157291678 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun CommentStart CommentHighlightPipeRestV2 CommentHighlightRest SCXW157291678 BCX8\" data-ccp-charstyle=\"Hyperlink\">Convert Macros to Constexpr<\/span><\/span><\/span><\/a><span class=\"EOP CommentHighlightPipeRestV2 SCXW157291678 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">Macros can be challenging to read and debug but converting them to constexpr functions can enhance code readability and maintainability. This feature identifies macros that can be converted to constexpr, and then offers a quick way to convert them to modern C++ constexpr expressions.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">To convert <\/span><code class=\"language-default\">#define ARRAY_SIZE_Y 5<\/code> <span data-contrast=\"auto\">to a constexpr, you can hover over the suggestion indicator (&#8230;) below the macro or select the macro and then chose \u2019Quick Actions and Refactorings&#8230;\u2019 option from the context menu (Ctrl+. keyboard shortcut). This will display the <\/span><b><span data-contrast=\"auto\">Convert Macros to Constexpr<\/span><\/b><span data-contrast=\"auto\"> option, which will complete the conversion to <\/span><code class=\"language-default\">constexpr auto ARRAY_SIZE_Y = 5;<\/code><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/macroToConstexpr_example.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-34235 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/macroToConstexpr_example.gif\" alt=\"Gif showing an example of Convert Macro to Constexpr.\" width=\"1227\" height=\"627\" \/><\/a><\/p>\n<p><span data-contrast=\"auto\">This feature can process constants and basic expressions contained in function-like macros. Due to performance optimization, some macros may not display the \u2018\u2026\u2019 indicator, however, the conversion option remains readily available in the lightbulb menu. <\/span><span data-contrast=\"auto\">To configure <\/span><b><span data-contrast=\"auto\">Convert Macros to Constexpr<\/span><\/b><span data-contrast=\"auto\">, navigate <\/span><b><span data-contrast=\"auto\">Tools <\/span><\/b><span data-contrast=\"auto\">&gt; <\/span><b><span data-contrast=\"auto\">Options <\/span><\/b><span data-contrast=\"auto\">&gt; <\/span><b><span data-contrast=\"auto\">Text Editor<\/span><\/b><span data-contrast=\"auto\"> &gt; <\/span><b><span data-contrast=\"auto\">C\/C++<\/span><\/b><span data-contrast=\"auto\"> &gt; <\/span><b><span data-contrast=\"auto\">View <\/span><\/b><span data-contrast=\"auto\">&gt; <\/span><b><span data-contrast=\"auto\">Macros Convertible to constexpr<\/span><\/b><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">Convert Macros to Constexpr <\/span><\/b><span data-contrast=\"auto\">is a feature within our fix-it capabilities. In our forthcoming blog post, we will delve into additional fix-it features that are currently available in Visual Studio.<\/span><\/p>\n<h3><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\"><a class=\"Hyperlink SCXW18606533 BCX8\" href=\"https:\/\/devblogs.microsoft.com\/cppblog\/quick-tips-for-using-copilot-chat-with-c-in-visual-studio\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW18606533 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW18606533 BCX8\" data-ccp-charstyle=\"Hyperlink\">GitHub Copilot for Refactoring<\/span><\/span><\/a><span class=\"EOP SCXW18606533 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span>\u00a0<\/span><\/h3>\n<p><a href=\"https:\/\/github.com\/features\/copilot\"><span data-contrast=\"none\">GitHub Copilot<\/span><\/a><span data-contrast=\"auto\">, an AI-powered coding assistant, can help C++ developers refactor their code. It offers a streamlined process for refactoring, making it an efficient and effective tool for code optimization.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">To utilize <\/span><b><span data-contrast=\"auto\">GitHub Copilot for refactoring<\/span><\/b><span data-contrast=\"auto\">, begin by identifying the segment of code you wish to refactor. This could be a function, a class, or even a single line of code. Once identified, right-click on the selected code and request assistance from Copilot to refactor it. The command can be tailored to your specific needs. Upon generating a suggestion for the refactoring, it\u2019s crucial to review this carefully. Make any necessary adjustments to ensure it aligns with your coding standards and project requirements. Once you\u2019re satisfied with the proposed refactoring, proceed to implement the changes in your code.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">For instance, suppose you\u2019re working with a complex function in C++, like <\/span><code class=\"language-default\">btCreateInProcessExampleBrowser<\/code> <span data-contrast=\"auto\">in \u2018InProcessExampleBrowser.cpp\u2019. To simplify it, you\u2019d highlight the function and ask GitHub Copilot for a simplification. Copilot suggested using auto for type inference, nullptr instead of 0, static_cast for safer type conversion, and limiting the scope of loop variables. These changes improve readability, safety, and maintainability. You can select preview, to compare and review the differences between the original and proposed code. If you\u2019re satisfied with the changes, you can accept them to incorporate the refactored code directly into your existing codebase.<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/copilotRefactoring_example.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-34231 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/05\/copilotRefactoring_example.gif\" alt=\"Gif displaying an example of refactoring using Copilot. \" width=\"1160\" height=\"707\" \/><\/a><\/p>\n<h3><span class=\"TextRun SCXW239644614 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW239644614 BCX8\" data-ccp-parastyle=\"heading 2\">Send us your feedback<\/span><\/span><span class=\"EOP SCXW239644614 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">We encourage you to continue exploring the C++ refactoring features in <\/span><a href=\"https:\/\/visualstudio.microsoft.com\/vs\/\"><span data-contrast=\"none\">Visual Studio<\/span><\/a><span data-contrast=\"auto\">. Your insights and experiences are invaluable to us, therefore, please share your feedback and suggestions in the comments section below or through the <\/span><a href=\"https:\/\/developercommunity.visualstudio.com\/cpp\"><span data-contrast=\"none\">Developer Community<\/span><\/a><span data-contrast=\"auto\">. You can also reach out to us on Twitter (<\/span><a href=\"https:\/\/twitter.com\/visualc\"><span data-contrast=\"none\">@VisualC<\/span><\/a><span data-contrast=\"auto\">) or via email at <\/span><a href=\"mailto:visualcpp@microsoft.com\"><span data-contrast=\"none\">visualcpp@microsoft.com<\/span><\/a><span data-contrast=\"auto\">. We are always eager to hear from you.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Furthermore, if there are additional refactoring features that you find necessary or would frequently use, we would greatly appreciate your input. Your suggestions help us understand your needs better and continually improve our product.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code refactoring is the process of restructuring existing code, while its outward functionality remains unchanged. By refactoring, you can simplify complex code structures, reduce code redundancy, and enhance code reusability. Visual Studio offers many tools to help refactor your C++ code. This article will delve into these tools, using the bullet3 open-source code as an [&hellip;]<\/p>\n","protected":false},"author":96741,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-34219","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Code refactoring is the process of restructuring existing code, while its outward functionality remains unchanged. By refactoring, you can simplify complex code structures, reduce code redundancy, and enhance code reusability. Visual Studio offers many tools to help refactor your C++ code. This article will delve into these tools, using the bullet3 open-source code as an [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/34219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/96741"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=34219"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/34219\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=34219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=34219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=34219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}