Introduction
In this part of the article series on learning Visual Studio 2015, we’ll cover a topic named renaming in Visual Studio 2015. Yes, Visual Studio 2015 provides a great capability of refactoring/renaming the code. It helps a developer to optimize 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 optimizing the code. Renaming the variables, methods, properties, classes or even projects has always been a challenge to a developer when working on large code base. Another challenge that most of the developers face is w.r.t. code comments and writing an optimized method. Visual Studio 2015 helps in code refactoring and renaming as well in a very easy and friendly way.
Learning Visual Studio 2015 Series
Code Renaming
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 product based on product code and another to return complete list of products. Product.cs is another class containing Product
entity. This class acts as a transfer object or entity. We’ll use the Main
method of Program.cs class to invoke methods of MyProducts
class.
IProducts
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Threading.Tasks;
6:
7: namespace VS2015ConsoleApplication
8: {
9: interface IProducts
10:
{
11: Product GetProduct(string productCode);
12: List<product> GetProductList();
13: }
14: }
Product
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Threading.Tasks;
6:
7: namespace VS2015ConsoleApplication
8: {
9: public class Product
10: {
11: public string ProductName { get; set; }
12: public string ProductCode { get; set; }
13: public string ProductPrice { get; set; }
14: public string ProductType { get; set; }
15: public string ProductDescription { get; set; }
16:
17: }
18: }
MyProducts
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Threading.Tasks;
6:
7: namespace VS2015ConsoleApplication
8: {
9: public class MyProducts :IProducts
10: {
11: List<product> _productList = new List<product>();
12: public MyProducts()
13: {
14: _productList.Add(new Product {ProductCode="0001",ProductName="IPhone",
ProductPrice="60000",ProductType="Phone",ProductDescription="Apple IPhone" } );
15: _productList.Add(new Product { ProductCode = "0002", ProductName = "Canvas",
ProductPrice = "20000", ProductType = "Phone", ProductDescription = "Micromax phone" });
16: _productList.Add(new Product { ProductCode = "0003", ProductName = "IPad",
ProductPrice = "30000", ProductType = "Tab", ProductDescription = "Apple IPad" });
17: _productList.Add(new Product { ProductCode = "0004", ProductName = "Nexus",
ProductPrice = "30000", ProductType = "Phone", ProductDescription = "Google Phone" });
18: _productList.Add(new Product { ProductCode = "0005", ProductName = "S6",
ProductPrice = "40000", ProductType = "Phone", ProductDescription = "Samsung phone" });
19:
20: }
21:
22: public Product GetProduct(string productCode)
23: {
24: return _productList.Find(p => p.ProductCode == productCode);
25: }
26:
27: public List<product> GetProductList()
28: {
29: return _productList;
30: }
31: }
32: }
Program.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Threading.Tasks;
6:
7: namespace VS2015ConsoleApplication
8: {
9: class Program
10: {
11: static void Main()
12: {
13: var myProducts = new MyProducts();
14: Console.WriteLine( String.Format("Product with code 0002 is : {0}",
myProducts.GetProduct("0002").ProductName));
15: Console.WriteLine(Environment.NewLine);
16: var productList = myProducts.GetProductList();
17: Console.WriteLine("Following are all the products");
18:
19: foreach (var product in productList)
20: {
21: Console.WriteLine(product.ProductName);
22: }
23: Console.ReadLine();
24: }
25: }
26: }
Our code is ready. Program.cs 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 that 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, Lightbulb 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 that the method has a newly changed variable now at all the instances.
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Threading.Tasks;
6:
7: namespace VS2015ConsoleApplication
8: {
9: public class MyProducts :IProducts
10: {
11: List<product> _allProduct = new List<product>();
12: public MyProducts()
13: {
14: _allProduct.Add(new Product {ProductCode="0001",ProductName="IPhone",
ProductPrice="60000",ProductType="Phone",ProductDescription="Apple IPhone" } );
15: _allProduct.Add(new Product { ProductCode = "0002", ProductName = "Canvas",
ProductPrice = "20000", ProductType = "Phone", ProductDescription = "Micromax phone" });
16: _allProduct.Add(new Product { ProductCode = "0003", ProductName = "IPad",
ProductPrice = "30000", ProductType = "Tab", ProductDescription = "Apple IPad" });
17: _allProduct.Add(new Product { ProductCode = "0004", ProductName = "Nexus",
ProductPrice = "30000", ProductType = "Phone", ProductDescription = "Google Phone" });
18: _allProduct.Add(new Product { ProductCode = "0005", ProductName = "S6",
ProductPrice = "40000", ProductType = "Phone", ProductDescription = "Samsung phone" });
19:
20: }
21:
22: public Product GetProduct(string productCode)
23: {
24: return _allProduct.Find(p => p.ProductCode == productCode);
25: }
26:
27: public List<product> GetProductList()
28: {
29: return _allProduct;
30: }
31: }
32: }
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 the large code base, so while renaming it is suggested to have a glance over the 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 the 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 i.e. 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
.
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Threading.Tasks;
6:
7: namespace VS2015ConsoleApplication
8: {
9: class Program
10: {
11: static void Main()
12: {
13: var myProducts = new MyProducts();
14: Console.WriteLine( String.Format("Product with code 0002 is : {0}",
myProducts.GetProduct("0002").ProductName));
15: Console.WriteLine(Environment.NewLine);
16: var productList = myProducts.GetProductList();
17: Console.WriteLine("Following are all the products");
18:
19: foreach (var product in productList)
20: {
21: Console.WriteLine(product.ProductName);
22: }
23: Console.ReadLine();
24: }
25: }
26: }
Rename Conflicts
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 have a glance over 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 existing 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 the old one.
Therefore, Visual Studio helps to detect any renaming conflicts and suggests 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.
1: interface IProducts
2: {
3: Product GetProduct(string productCode);
4: Product GetProduct(string productCode,string productName);
5: List<product> GetProductList();
6: }
1: 2: 3: 4: 5: 6: public Product GetProduct(string productCode)
7: {
8: return _allProduct.Find(p => p.ProductCode == productCode);
9: }
10:
11: 12: 13: 14: 15: 16: 17: public Product GetProduct(string productCode, string productName)
18: {
19: return _allProduct.Find(p => p.ProductCode == productCode && p.ProductName==productName);
20: }
21:
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 those options, 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 string
s 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 in a particular instance. Here, I am trying to change GetProduct
method 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 a 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 a 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 strong 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. As 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. Change the name to pCode
and click on apply.
We see the parameter name is changed along with the name in the comment too.
Conclusion
In this article, we covered renaming assistance provided by Visual Studio 2015. We learnt about the renaming experience in various ways that includes the 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.
Read More