Code Refactoring has always been a challenge for developers. This is one of the major skill that a developer should have to write an optimized , clean and fast code. There were third party tools available to help you achieve this, but none has shown that capability that Visual Studio 2015 has come up with.Visual Studio has always offered code refactoring techniques in tit-bit, but the latest version of Visual Studio i.e. 2015 provides a unique experience altogether to achieve refactoring. There are many features that refactoring of code in Visual Studio provides. We’ll cover 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 new code will do and it looks strange to you, you should never try it.
- 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
I am taking an example of MyProducts class that we created in the earlier section.
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 is FetchProduct(). Notice that we already had two methods with the same name. Now this will also work as an overload to FetchProduct() method.
The above-mentioned method is very simple in nature and contains LINQ query that fetches products having the price greater than 30000. When you call this method from Program.cs and iterate over the elements we get the following the result.
Program.cs code is as follows.
So, our method is working fine. the question is how much we can optimize this method further. 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.
Here come inline temporary variable 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.
It shows that the temporary products variable will be replaced with productList itself, therefore, helping us to save a number of lines as well as memory allocation for a variable. Click on apply changes and you’ll get the refactored code.
Now can this code be further refactored? you can take help from light bulb icon. Either click on between productList variable or right click on productList variable and open Quick Actions from the context menu.
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 the 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 ToList() method is directly applied to “p” variable. We have to rectify this by putting brackets around LINQ query. This was one of the scenarios, 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.
Now we have a single return statement. Our code has reduced to just one line and we saved few memory too by ignoring temporary variables.This certainly makes the code faster as well.But can we further optimize this method? Let’s take a shot and click on return statement. You’ll see the light bulb icon again shows up with some suggestions.
It says that the method could be converted to an expression bodied member.Let us preview the change.
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 the following code.
Now if you try to further optimize this method, you’ll not find much scope.
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.