Visual Studio 2015 : Introduction
Introduction
The power of Visual Studio is now not only limited to development and coding but it offers a one stop solution to all the requirements needed while coding, development, code analysis or deployment.
In this section of the series we’ll cover how development with Visual Studio 2015 can increase your productivity to n times and enables you to write more cleaner and optimized code.
Code Assistance
In earlier versions of Visual studio, you must have seen that whenever you write a buggy code, the code editor provides suggestion with the help of a tool tip. This feature have improved a lot and is shown as a light bulb icon in Visual Studio code editor.This option provides you the real time suggestions while coding in Visual Studio code editor to improve code quality or fix the coding issues. It helps you in identifying syntax errors, provides useful code hints and assist you with static code analysis.I am using a sample code to explain the enhancements, for that I have created a console application in my Visual Studio and named it VisualStudio2015ConsoleApplication.
Syntax Error Suggestions
Suppose there is syntax error in your code like I purposely did in the below image,
https://codeteddycom.files.wordpress.com/2016/05/1_thumb.png?w=1028&h=370
The light bulb icon immediately shows up when you click your mouse over the erroneous variable having red line and displays an issue summary, an error code with a link to documentation. It also displays a possible list of code fixes and refactoring’s. In above mentioned example we are writing an add method taking two parameters "a" and "b", but we are trying to return a result as a+bh.
Now since “bh” is not declared anywhere in the method or passed as a parameter, the light biulb icon shows up and provides certain possible options or suggestions about how this variable can be taken care of.It suggests to generate a variable named “bh” , create a field or property as well.
If you hover on the error line you’ll be shown a light bulb icon showing error and potential fixes.
https://codeteddycom.files.wordpress.com/2016/05/4_thumb.jpg?w=900&h=290
Note that you can also use Ctrl+. to see the error using your keyboard. If you click on Show potential fixes, you’ll get the same options as shown in the first image. Alternatively if by any chance you doubt light bulb icon and build your console application, you’ll again be shown the same error as follow.
https://codeteddycom.files.wordpress.com/2016/05/5_thumb.jpg?w=900&h=486
The syntax error assistance displays a light bulb icon, a description of the error, and a link to show possible fixes as well. When you click on the error code i.e. CS0103 you’ll be redirected to the documentation of the error code. It also offers to preview changes once you go for any of the suggestions provided by light bulb icon. So if I click on Preview changes it shows me the preview of the option that I have chosen as shown below.
https://codeteddycom.files.wordpress.com/2016/05/2_thumb.jpg?w=882&h=726
Now we don’t have to go to code and explicitly define that variable. Just click on Apply button and Visual Studio takes care of everything. Therefore the first option that I chose is now reflected in my code.
https://codeteddycom.files.wordpress.com/2016/05/3_thumb.jpg?w=900&h=626
Remember that we used to do these on the fly modifications to improve productivity using ReSharper. We saw that we got the same error on compiling the application which proves that light bulb icon’s code suggestion can help us write error free code before even compiling the application and getting to know about the actual error after a compile.Therefore we don’t have to wait to compile the application to know about compile time error. You can test different scenarios to explore syntax error suggestions given by light bulb icon in our day to day programming.
Code Suggestions
Let’s take another scenario. Suppose we define an interface named ICalculator and add a class named Calculator and inherit the calculator.cs class from that interface.
Interface
1: interface ICalculator
2: {
3: int Add(int a, int b);
4: int Subtract(int a, int b);
5: int Multiply(int a, int b);
6: float Divide(float a, float b);
7: }
Class
1: public class Calculator : ICalculator
2: {
3: }
You’ll see that there will be a red error line under the ICalculator interface named in calculator class.You can get to see the light bulb icon in the same way as shown in previous example i.e. hover or click on error.Here you’ll see that light buld icon is assisting us with some additional conceptual information that the interface that we are using is contains several methods that needs to be implemented in Calculator class.
https://codeteddycom.files.wordpress.com/2016/05/7_thumb.png?w=1028&h=385
Therefore we see that light bulb not only assists us in finding syntax error but also suggests conceptual or logical resolution of mistakes we do in programming. When you show on “Show potential fixes link” it will show all the possible fixes for this error in a detailed user friendly manner with an option to resolve and fix it with preview as well.
https://codeteddycom.files.wordpress.com/2016/05/8_thumb.png?w=1028&h=590
In the above image you can see that the code assistance is providing option to either implicitly and explicitly implement interface ICalculator, and if we analyze we can clearly say that these are the only possible options that a developer may opt for in this scenario.moreover it shows the error doe link referring to its description. If you choose first option and choose preview changes link, you’ll see following preview and you can choose to apply that if it is what you need.
https://codeteddycom.files.wordpress.com/2016/05/9_thumb.jpg?w=822&h=726
So click apply and we get following class with all the interface methods having default implementations.
1: public class Calculator : ICalculator
2: {
3: public int Add(int a, int b)
4: {
5: throw new NotImplementedException();
6: }
7:
8: public float Divide(float a, float b)
9: {
10: throw new NotImplementedException();
11: }
12:
13: public int Multiply(int a, int b)
14: {
15: throw new NotImplementedException();
16: }
17:
18: public int Subtract(int a, int b)
19: {
20: throw new NotImplementedException();
21: }
22: }
Likewise light bulb icon provides numerous code suggestion options while development and coding following which we can increase the productivity of writing code without unnecessarily compiling the application and writing the code manually.
Refactoring Suggestions
Light bulb icon is not only limited to code suggestions and syntax error suggestions but also comes with a great capability of refactoring techniques. When I added the calculator class, the class was added with few default namespaces as shown below.
https://codeteddycom.files.wordpress.com/2016/05/10_thumb.png?w=900&h=540
In the above scenario as we can see there are few namespaces added by default in the class that are currently not used. When you hover the mouse over those namespaces, light bulb icon shows up with some refactoring suggestions as shown below.
https://codeteddycom.files.wordpress.com/2016/05/11_thumb.png?w=1028&h=332
The above image shows the suggestion of Light bulb icon asking to remove “unnecessary usings”. We see here that Visual Studio is smart enough to know what refactoring is required in the code and accordingly can suggest a developer to optimize the code.If you apply the suggestion it will remove the unnecessary “usings” from your code. You can also select to fix all occurrences of this issue in the current document, the project, or the solution. If we just want to make this local change, we can select Remove Unnecessary Usings here, and the unused usings are removed.
https://codeteddycom.files.wordpress.com/2016/05/12_thumb.png?w=1028&h=332
Quick Suggestions and Refactoring
Now when you go to my Calculator.cs class and define the Add method as follows.
1: public int Add(int a, int b)
2: {
3: int c = a + b;
4: return c;
5: }
It is the correct way of defining an add method, but on the second thought what if I want to refactor or optimize this method. Visual Studio 2015 provides us facility to do quick refactoring of code with suggestions using an option enabled in context menu of the editor. Just right click on “c” and you’ll get to see an option at the top of the context menu saying “Quick Actions and Refactorings…”.
https://codeteddycom.files.wordpress.com/2016/05/13_thumb.png?w=678&h=726
Note that in above code block, Visual Studio didn’t suggest any thing implicitly and due to some syntax error but we have an option to choose and ask for Visual Studio’s help explicitly to know if a particular written code could be enhanced, optimized, refactored more or not.There could be cases that choosing this option too does not show any suggestion to improve code which means your code is already refactored and optimized. But in the above mentioned scenario if we select the “Quick Actions and Refactorings…” option, VS gives us two options to further optimize the code.
https://codeteddycom.files.wordpress.com/2016/05/14_thumb.png?w=900&h=261
or,
https://codeteddycom.files.wordpress.com/2016/05/15_thumb.png?w=900&h=393
If we have a glance over both the options, the first says to skip using temporary variable and just return (a+b) which is a good suggestion by the way and second option says to extract a method out of the code and put (a+b) in any other method and return from there.Now in these situations its choice of developer on what option he chooses.I choose first option and apply the changes that it showed me in preview and I got following code which looks better that the earlier one.
1: public int Add(int a, int b)
2: {
3: return a + b;
4: }
Note that these are the small examples that I am taking to just explain the power of Visual Studio 2015. There could be complex and tricky scenarios where you may actually need a lot of help through these features.
Conclusion
We took few representative examples from a large set of possible scenarios where Code assistant of Visual Studio 2015 can help you. You can explore the IDE and play around to see more such situations where you feel the change from earlier versions of Visual Studio to this one.In my next section I’ll talk about Live Static Code Analysis.