All about C++ Refactoring in Visual Studio 2015 Preview

Gabriel Ha

Hello C++ World!

Update (1/6/2015): Check out all these features in action — as well as how they’ve been improved since Preview — in this video!

With Wednesday’s release of Visual Studio 2015 Preview, we are pleased to present you with new and improved coding productivity features, not the least of which is (finally) Refactoring for C++! Special thanks goes out to you folks who tried out the Visual Studio “14” CTPs and left us feedback; we tried to incorporate as many suggestions as we could in time for Preview, and we will continue to polish the features!

I wanted to take some time to call out the refactoring features, from the basics of how to invoke each feature, to neat little tricks that you might enjoy! The following features will be covered in this post:

  • Rename
  • Extract Function
  • Implement Pure Virtuals
  • Create Declaration/Definition
  • Move Function Definition
  • Convert to Raw-String Literal
Rename is unquestionably the most used and most needed refactoring tool. It’s so important that we put it on the top-level of the right-click menu. (You can also activate the feature by pressing Ctrl+R twice.) Settings are saved after each usage of the feature, but by default, you’ll go through two dialogs, with the second being a preview window. 

Image 3302 111414 1932 XxGabrielsP1

Image 1261 111414 1932 XxGabrielsP2

Tips and Tricks
  • To hide comments/strings from the preview window results, leave “Search comments/strings” unchecked.
  • To show all comments/strings in the preview window, but leave each entry unchecked by default, leave “Rename comments/strings” unchecked.
  • To show and check by default all comments/strings, check “Rename comments/strings.”
  • For more information (from the feature’s extension days), watch “Rename” Refactoring for Visual C++ on Channel 9.

Extract Function was our second-most requested feature. We’re still tweaking it a bit, so we’ve released it as an extension for the time being; please give us feedback on the feature! Once you’ve installed the extension, select a block of code, right-click, and find the feature under “Refactor…” (You can also invoke it using Ctrl+R, Ctrl+M.)

Image 5074 111414 1932 XxGabrielsP3

Tips and Tricks

  • The tool has some built-in error checking, like if you select a partial expression, but for best results, try to select valid, well-formed code.
  • The extracted function will match your current context. For example, if you extract code from a member function, the extracted function will be a member function as well (and a forward declaration will be created for you).

Implement Pure Virtuals stubs out all the pure virtuals from the base classes that a class inherits. Both multiple and recursive inheritances are supported. The feature can be invoked from the class definition.

Image 3821 111414 1932 XxGabrielsP4

Tips and Tricks

  • The feature encourages good coding practice, so it will create forward declarations in the header and the definition stubs in the source. If a namesake source doesn’t exist, it will be created.
  • Invoke the feature on an inherited base to implement pure virtuals only from that particular base.

    Image 2335 111414 1932 XxGabrielsP5

  • A single undo operation will remove all created forward declarations and definitions, but it will not remove a generated .cpp (if one was created).
  • For more information (from the feature’s CTP days), watch this video.

Aside: The previous three features rely on IntelliSense being available for the current Translation Unit (TU) where the features are invoked. If IntelliSense is not working (which you can usually infer by the lack of semantic colorization), or if your code has a lot of compile errors/red squiggles, the features will not work as reliably.

Create Declaration/Definition allows you to quickly create a function’s counterpart from either its forward declaration or its definition. To support good coding practices and to alleviate context switching, the Peek Definition feature shows you where the counterpart was created. Activate the feature via LightBulb.

Before:

Image 0272 111414 1932 XxGabrielsP6

After:

Image 3252 111414 1932 XxGabrielsP7

Tips and Tricks
  • The feature encourages good coding practice, so it will position the created function relative to neighboring functions. If no context is evident, it will find the namesake .h or .cpp to place the function, or create one if it doesn’t exist.
  • A green squiggle appears (along with a LightBulb when hovered over) when we detect that a function has been declared but not defined.
  • Want to create counterparts for multiple functions? Just select code so that it contains all the functions you need (your selection doesn’t even have to be precise!), right-click, go to Refactor…, and invoke the feature.

    Image 1108 111414 1932 XxGabrielsP8

  • If you don’t like where the feature placed the counterpart, use the copy-to-clipboard feature via the LightBulb, and then paste it where it is supposed to go.
  • For more information (from the feature’s CTP days), watch this video.
Move Definition Location allows you to quickly move the body of a function inline or out-of-line (usually between header and source). Its implementation and behavior is very similar to that of Create Declaration/Definition, sans LightBulb.

Image 5811 111414 1932 XxGabrielsP9

Tips and Tricks
  • Shares the following features with Create Declaration/Definition: relative positioning, Peek Definition, batch/mass operation
  • For more information (from the feature’s CTP days), watch this video.
Convert to Raw-String Literal allows you to convert any string (particularly useful for strings littered with escape sequences), to the much-easier-to-read Raw-String Literal introduced in C++11. Simply right-click anywhere inside a string (do not select the code) and invoke the feature from the Refactor menu.

Image 6472 111414 1932 XxGabrielsP10

Tips and Tricks
  • Only those escape sequences having textual implications are supported:
    • \n – new line
    • \t – tab
    • \’ – single quote
    • \” – double quote
    • \? – question mark
    • \\ – backslash
  • If your string contains any other escape sequence, it will give you a failure message with the first unsupported escape sequence found.
  • There is no feature to convert back to a non-raw-string literal — just use the undo key 😉

Please try out the features, and leave us your feedback, negative and positive. Thank you for your time, and for helping us make Visual Studio a better product!

Best regards,
Gabriel Ha
Visual C++ PM

0 comments

Discussion is closed.

Feedback usabilla icon