Refactoring C ++ code in Visual Studio 2015

Original author: Gabriel Ha
  • Transfer
  • Tutorial
Hello C ++ World!

Last Friday Visual Studio 2015 Preview was released, which introduced new opportunities to increase development productivity, including refactoring C ++ code. To a large extent, the implementation of this functionality was influenced by the feedback from the community that was received during the testing of Visual Studio “14” CTPs, so thanks to everyone who participated.

In this article, we will consider such features of Visual Studio 2015 Preview on working with C ++ code as:
  • Rename
  • Extract Function
  • Generation of stubs of pure virtual methods (Implement Pure Virtuals)
  • Generation of method declarations / stubs (Create Declaration / Definition)
  • Moving Function Declarations
  • Convert to Raw-String Literal


Renaming is by far the most often needed tool. Thus, we placed it at the very top of the context menu with a right click on the class \ function \ variable. In addition, we can activate it by double-clicking the combination Ctrl + R. The tool itself is two-step - in the first window you specify the renaming settings, the second window - the preview.

image
image

Useful tips
  • You can disable the search for a renamed character in comments and string literals in the first window (checkbox “Search comments / strings”)
  • You can also enable viewing of characters found in comments and lines, but do not rename them (tick “Rename comments / strings”)
  • You can find more information about this feature in this video.


Function extraction was the second most requested feature. We are still in the process of working on it, so for now it is implemented as an extension . After installing it, highlight the code block that you want to separate into a separate function, then right-click and select “Extract Function / Method” in the “Refactor ...” menu.

image

Useful tips
  • The tool has some means of code validation, but still try to highlight a valid, coherent block of code for normal operation.
  • The recoverable function maintains context. This means that if you extract part of the code from a method of a class, the new function will be a method of the same class.


Generation of virtual method stubs allows you to create the bodies of all purely virtual methods in an inherited class. Multiple inheritance is supported. The tool is called from the context menu of the class declaration.

image

Useful tips
  • The tool generates method declarations in the header file, and the stubs themselves in the cpp file.
  • You can call the generation of stubs for only one of the base classes, from the context menu of the name of this class in the declaration of the inherited class:
  • The undo operation will delete all generated stubs, but will not delete the cpp file if it was created in the previous step
  • You can find more information about this tool in this video.


An important point : all of the above functions only work when IntelliSense is enabled.

Generating method declarations / stubs allows you to quickly generate a missing method declaration or stub.

image

image

Useful tips
  • Methods that have a body but are not defined in the header file will be emphasized by a wavy green line.
  • You can generate definitions / stubs for several methods at once (you need to select everything and select "Refactor-> Create Declaration / Definition" in the context menu)
  • You can find more information about this tool in this video.


Moving method declarations allows you to quickly move the method body from the header file to the cpp file or vice versa.

image

Useful tips
  • You can find more information about this tool in this video.


Converting to Raw-String allows you to convert any string to Raw-String, which greatly improves the readability of strings with escape sequences. The function is called from the context menu anywhere on the line.

image

Useful tips
  • Only the following characters are currently supported: \ n, \ t, \ ', \ ", \?
  • If there are other escape sequences in your string, you will get an error
  • There is no reverse conversion - use undo

Also popular now: