In this part of the article series on learning Visual Studio 2015, we’ll cover topic named renaming in Visual Studio 2015. Yes, Visual Studio 2015 provides a great capability of refactoring/renaming the code. It helps the developer to optimise and refactor the code as per the development or best practices need. This feature enables a developer to follow best practices by giving refactoring suggestions as well as helping in fixing and optimising the code. Renaming the variables, methods, properties, classes or even projects has always been a challenge to a developer when working on a large code base.Another challenge that most of the developers face is w.r.t. code comments and writing an optimised method. Visual Studio 2015 helps in code refactoring and renaming as well in a very easy and friendly way.
- Day #1: Code Assistance in Visual Studio 2015
- Day #2: Code Analyzers in Visual Studio 2015
- Day #3: Renaming Assistance in Visual Studio 2015
- Day #4: Code Refactoring in Visual Studio 2015
- Day #5: Debugging Improvements in Visual Studio 2015 (Breakpoint Configuration and New Error List)
- Day #6: Debugging Improvements in Visual Studio 2015 (Tool Window Support for LINQ and Lambda)
- Day #7: PerfTip in Visual Studio 2015
- Day #8: New Diagnostic Tool Window in Visual Studio 2015
There are a lot of features that this capability of renaming covers in visual Studio 2015.Change preview,inline and modeless rename windows, renaming on the fly, detecting and resolving conflicts, renaming code comments are few of them.let us discuss each one in detail via practical examples. I am using Visual Studio 2015 Enterprise edition and have created a console application named VS2015ConsoleApplication having an interface named IProducts and a class named MyProducts that implements the interface.IProducts contains two methods, one to return the product based on product code and another to return the complete list of products. Product.cs is another class containing Product entity. This class acts as a transfer object or entity. We’ll use Main method of Program.cs class to invoke methods of MyProducts class.
Our code is ready. Program file’s main method calls both the interface methods from MyProducts class to test the functionality.
We’ll use this code base to learn code renaming features.The renaming experience may vary w.r.t. what actual operation is being performed. For example in MyProducts class, we are using _productList as a variable containing list of all products, and this variable is widely used throughout the class.
Let us try to rename this variable to a new name called _allProducts.
Notice that as soon as _productsList is changed to _allProduct, a dotted box is shown around the changed variable name and a light bulb icon appears at the left. One can see one more change, that all the instances where _productList was used have a red line below them. This means that all the instances should be changed accordingly. You can see here the power of Visual Studio. Visual studio is smart enough to understand and communicate that the change that a developer is making has to be reflected other places too. Let us see what Light bulb icon says. Let us click on light bulb icon appeared at the left margin.
The light bulb icon shows some suggestions that we learnt in Day #1 and Day #2 of this article series. There is one new suggestion that Light bulb icon is showing now and that is regarding “Rename”. In the suggestion window Light bulb icon shows all the _productList instances highlighted and says to rename all the instances to new name i.e _allProducts. When you preview the changes by clicking “Preview Changes ” link shown at the bottom of suggestion window, it shows the resultant changes that you’ll get once the variable is renamed.
In the preview, you can clearly see the changes that will take place once the variable is renamed. Let us click on apply changes, and you’ll see the method has a newly changed variable now at all the instances.
Now let’s run the application to check if the changes had an impact on the functionality of the application.Press F5.
It is clearly seen that application has no build error and produces the same result as earlier. There is one thing that a developer needs to take care of, sometimes renaming may be risky and tricky in a large code base, so while renaming it is suggested to have a glance over preview i.e. given by suggestion window.
Let us take another scenario.Open the Program.cs file and perform rename on myProducts object.
This time, we’ll do renaming through a context menu. Right click on myProducts object name and you’ll see the context menu open. There is an option of rename on the opened context menu.
In front of Rename option, there is a shortcut available too Ctrl+R, Ctrl+R. When you select Rename, you’ll notice that the renaming experience here is different , all the instances of the renaming get highlighted.There is a new window that appears at the right corner of code window having few other options. Now if you rename the object name, the other highlighted variables get renamed on the fly while typing. To close the Rename window you can click cross button on that window. Notice that this option of renaming through context menu is much faster that the earlier one, and enables you to rename on the fly in a single step with live preview while typing. Once the variable is renamed and you close the window, all the highlighting will be gone and you get a clean renamed variable at all the occurrences.
I changed the object name from myProducts to products.
Let us take another scenario where we try to rename a variable to another name that is already assigned to any other variable in the same method or try to rename a property of a class having another property having the same name as of new name. Let us open Product class for example. Product class contains the Product properties, let us try to rename ProductName property. Right click on ProductName property and open Rename window by clicking Rename from the context menu.
We’ll change the name of ProductName property to ProductCode.We already have ProductCode property available in this class. Let us see what happens.Notice that when you start typing the new name of ProductName property to ProductCode, the rename window shows error with Red colour, and the already exiting ProductCode property also shows a red box around it.
Here Visual Studio 2015 smartly tells that the new property name already exists in the class and this change may result in having conflicts. Now you know that this change may cause your application to break, so just press escape and the rename window will be gone with your new name reverted to old one.
Therefore, Visual Studio helps to detect any renaming conflicts and suggest to resolve them.
Rename Overloads, Strings, Code Comments
Let us take one more scenario. Add an overloaded method named GetProduct to IProduct interface and implement that method in MyProducts class.
Now try to rename the GetProduct method in MyProducts class. Put the cursor in between the GetProduct method name and press Ctrl+R, Ctrl+R.The rename window will be opened as shown below.
You see here the Rename window opens having few more options as shown below.
If you select the checkbox to include overloads, the overloads of that method which we are renaming also gets renamed, in this case if we choose this option the overloaded method of GetProduct() also gets renamed.
If we also choose the second option then the code comments also get renamed when we rename the method to the new name. The name if exists in code comments also gets renamed to the new name.
The renaming option also allows renaming related strings for that name, i.e. the third option. You can also preview the changes before renaming. preview changes window shows you all the occurrences that will remain and provides you with an option again to review and select un-select the change you want to take place at the particular instance.Here I am trying to change GetProduct name to FetchProduct . Let us check Preview changes check box and press Apply button.
A preview window will get opened. Notice that not only for the particular file but this window accumulates all the possible areas where this change may take place and affect code like in MyProduct.cs file, IProducts.cs interface and program.cs file. it says that when the method name is changed, it will reflect in these files as well. Now it is the choice of a developer, whether to let that change happen or not. So preview window gives an option to the developer to check or un-check the checkbox for corresponding affected file for that change to take place or not. The code suggestion and code assistance mechanism of Visual Studio is so string that it takes care of all these modifications and automation very smartly.
In this case, I didn’t un-select any of the files and let that change happen. it automatically renamed GetProduct to Fetch Product to all the areas shown including Comments, Interface and Program file as well.
In a similar way, you can also rename the parameters passed in a method, this will also include all the possible renaming options like rename in comments and methods.Like shown in the following image, if I try to rename the parameter of FetchProduct method, it highlights all the possible renaming changes.
Put the cursor between productCode and press Ctrl+R, Ctrl+R, the rename window will appear and all instances of parameter get highlighted, even the comment too.Change the name to pCode and click on apply.
We see the parameter name is changed along with the name in the comment too.
In this article, we covered Renaming assistance provided by Visual Studio 2015. We learnt about the renaming experience in various ways that includes following bullet points.
- Renaming assistance through light bulb actions
- Change preview with smart suggestions.
- Rename window and its renaming options.
- On the fly , live renaming experience. Rename as you type.
- Conflict detection and resolution while renaming.
- Renaming code comments.
In the next section of this series , I’ll cover code refactoring in Visual Studio 2015.