Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#

Code Refactoring in Visual Studio 2015

Rate me:
Please Sign up or sign in to vote.
4.93/5 (18 votes)
10 May 2016CPOL4 min read 26.4K   335   24   4
In this article I show how you can leverage the capability of Visual Studio 2015 in optimizing your code.

Introduction

Code Refactoring has always been a challenge for developers. This is one of the major skills that a developer should have to write optimized, clean and fast code. There were third party tools available to help you achieve this, but none have shown that capability that Visual Studio 2015 has come up with. Visual Studio has always offered code refactoring techniques in titbits, but the latest version of Visual Studio provides a unique experience altogether to achieve refactoring. There are many features that refactoring of code in Visual Studio provides. We’ll cover a few of them like inline temporary variable and introduce local. Refactoring w.r.t. inline temporary variable and introduce local is not only limited to C# but VB developers can also leverage this feature. We’ll cover the topic with one small method as an example and try to optimize it as far as we can. One thing is worth taking care of that code refactoring software and techniques are only meant for sharp developers. If you don’t have an idea what the new code will do and it looks strange to you, you should never try it.

Image 1

Learning Visual Studio 2015 Series

Case Study

I am taking an example of MyProducts class that we created in earlier section.

 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:         /// <summary>
23:         /// FetchProduct
24:         /// </summary>
25:         /// <param name="pCode"></param>
26:         /// <returns></returns>
27:         public Product FetchProduct(string pCode)
28:         {
29:             return _allProduct.Find(p => p.ProductCode == pCode);
30:         }
31:
32:         /// <summary>
33:         /// FetchProduct with productCode and productName
34:         /// </summary>
35:         /// <param name="productCode"></param>
36:         /// <param name="productName"></param>
37:         /// <returns></returns>
38:         public Product FetchProduct(string productCode, string productName)
39:         {
40:             return _allProduct.Find(p => p.ProductCode == productCode && p.ProductName==productName);
41:         }
42:
43:         public List<Product> GetProductList()
44:         {
45:             return _allProduct;
46:         }
47:     }
48: }

We’ll add one more method to this class. The objective of that method will be to return all products from the product list whose price is greater than 30000. I am trying to keep the logic and method very simple for the sake of understanding. I have kept the name of this method as FetchProduct(). Notice that we already had two methods with the same name. Now this will also work as an overload to the FetchProduct() method.

Image 2

The above mentioned method is very simple in nature and contains a LINQ query that fetches products having price greater than 30000. When you call this method from Program.cs and iterate over the elements we get following result.

Image 3

Program.cs code is as follows.

 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.FetchProduct("0002").ProductName));
15:             Console.WriteLine(Environment.NewLine);
16:             var productList = myProducts.FetchProduct();
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: }

So our method is working fine. The question is how much further we can optimize this method. When you click in between products variable, the light bulb icon will show up with some suggestions that you can apply to this line to optimize.

Image 4

Now inline temporary variables come in the picture. The light bulb icon says to remove this variable and bring that code to a single line. When you preview the changes that this code assistance is suggesting you get following preview window.

Image 5

It shows that the temporary products variable will be replaced with productList itself, therefore helping us to save number of lines as well as memory allocation for a variable. Click on apply changes and you’ll get the refactored code.

Image 6

Now can this code be further refactored. You can take help from light bulb icon. Either click inbetween the productList variable or right click on the productList variable and open Quick Actions from context menu.

Image 7

We see here, productList is also a temporary variable and it is suggested to remove that too. Let us preview changes and apply them to get more optimized code. Doing this the productList variable will be replaced by a single return statement, but you’ll notice an error here. If you remember, I said that code refactoring is for intelligent and sharp developers. We see here that while refactoring the code, LINQ query is not encapsulated in the bracket and the ToList() method is directly applied to "p" variable. We have to rectify this by putting brackets around LINQ query. This was one of the scenario, and you may face many of such. So you have to be sure about the change that you are about to do. Visual Studio only suggests, it does not code for you.

Image 8

Now we have a single return statement. Our code has reduced to just one line and we also saved memory by ignoring temporary variables. This certainly makes the code faster, but can we further optimize this method? Let’s take a shot and click on the return statement. You’ll see the light bulb icon again shows up with some suggestions.

Image 9

It says that the method could be converted to an expression bodied member. Let us preview the change.

Image 10

The preview says that the method is converted into an expression. Looking at this I do not find any issue. So we can certainly opt this option for the sake of refactoring. Press apply changes and we get following code.

Image 11

Now if you try to further optimize this method, you’ll not find much scope.

Conclusion

This was just a small example that I showed on how you can leverage the capability of Visual Studio 2015 in optimizing your code. In the next section of this series I’ll be covering topics like debugging features in Visual Studio 2015.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://codeteddy.com/
India India
Akhil Mittal is two times Microsoft MVP (Most Valuable Professional) firstly awarded in 2016 and continued in 2017 in Visual Studio and Technologies category, C# Corner MVP since 2013, Code Project MVP since 2014, a blogger, author and likes to write/read technical articles, blogs, and books. Akhil is a technical architect and loves to work on complex business problems and cutting-edge technologies. He has an experience of around 15 years in developing, designing, and architecting enterprises level applications primarily in Microsoft Technologies. He has diverse experience in working on cutting-edge technologies that include Microsoft Stack, AI, Machine Learning, and Cloud computing. Akhil is an MCP (Microsoft Certified Professional) in Web Applications and Dot Net Framework.
Visit Akhil Mittal’s personal blog CodeTeddy (CodeTeddy ) for some good and informative articles. Following are some tech certifications that Akhil cleared,
• AZ-304: Microsoft Azure Architect Design.
• AZ-303: Microsoft Azure Architect Technologies.
• AZ-900: Microsoft Azure Fundamentals.
• Microsoft MCTS (70-528) Certified Programmer.
• Microsoft MCTS (70-536) Certified Programmer.
• Microsoft MCTS (70-515) Certified Programmer.

LinkedIn: https://www.linkedin.com/in/akhilmittal/
This is a Collaborative Group

780 members

Comments and Discussions

 
GeneralMy vote of 5 Pin
Santhakumar M10-May-16 23:17
professionalSanthakumar M10-May-16 23:17 
GeneralRe: My vote of 5 Pin
Akhil Mittal11-May-16 1:27
professionalAkhil Mittal11-May-16 1:27 
GeneralMy vote of 5 Pin
Jacob S Mathew10-May-16 7:01
professionalJacob S Mathew10-May-16 7:01 
GeneralRe: My vote of 5 Pin
Akhil Mittal10-May-16 20:50
professionalAkhil Mittal10-May-16 20:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.