Something amazing happened to me recently! I am always learning, growing and exploring other resources. And recently I stumbled upon an amazing Visual Studio productivity tool called ReSharper. According to JetBrains, the creators of ReSharper, “ReSharper makes Microsoft Visual Studio a much better IDE. .NET developers worldwide wonder how they’ve ever lived without ReSharper’s code inspections, refactoring and navigation.”
From my first usage, I can claim that this assertion is completely correct. Within a few minutes of installation I had ReSharper improving my code in countless ways. It was simply phenomenal!
By the way, I am not an affiliate of any sort for this company. I just freaking love the tool and wanted to share the awesomeness with my fellow Software Testers.
What is ReSharper?
ReSharper is a tool that can be plugged into Visual Studio to help improve its IDE. In thsi post, we will discuss a ton of benefits that can come from ReSharper.
This is the site with their download link. You get a Free 30 day trial.
Why Should Automation Engineers Use ReSharper?
Personally, love this tool. It simply makes your code much better without having to spend hours refactoring. It helps to analyze your code during design time and helps you to fix tons of different errors and warnings. ReSharper removes redundant code, unused variables, and unused references. It helps to keep you following the Microsoft standards of using C# and at the same time makes your code easier to understand and maintain. It even helps to improve your logic so that it is easier to follow in the future.
After about 1 hr of refactoring, the code in my automation testing framework was drastically different! Only in good ways. A bunch of explicit casts was replaced to implicit casts with the click of a few buttons. Several complicated nested loops and statements were replaced with much more efficient logic. So many old references were deleted from my solution to make for a much lighter piece of architecture. The coolest part is that I am actually learning from this tool. Some C# Coding Standards and different ways to refactor logic are some of the most drastic lessons that I learned.
Basically, it’s saving my employer time and money while teaching me some cool information. Now my automation testing framework code is as sweet as Organic Raw Honey! Let’s really dig into some of the awesome features.
Best ReSharper Features
Keep in mind that I just started utilizing this tool. Therefore, there may be tons of other amazing features that I am not aware of. However, since the beginning, these are the ones that blew the pants off of me.
ReSharper has continuous code quality analysis for a bunch of different languages. I’m currently using C# so that’s the one I cared about.
- It has over 1700 different code inspections. It alerts you in the sidebar of the different errors at design time.
- Besides all of the typical errors and warnings that Visual Studio provides, ReSharper also assists you by giving you warnings about redundant casts, incorrectly formatted strings, never used variables and code that cannot be reached. This feature is so awesome because it helps you to clean up your code. It has helped me to cut down my code by 100s of lines by removing unused variables. Also, if you have conditions that will never be met, ReSharper will let you know so that you fix your logic.
- ReSharper can also give you Hints that can help you to improve your code.
[su_button url=”/?attachment_id=5686″ target=”blank” style=”soft” background=”#0f96f3″ color=”#ffffff” size=”5″ radius=”20″ icon_color=”#ffffff” text_shadow=”0px 0px 0px #000000″]Get This Code[/su_button]
Real Examples
Let’s dive deep into some real world examples with code so that you all can get a full understanding with some much required practice. Have you ever faced a situation where you needed to update the signature of the method and that method has 20 references? I sure have and it’s a pain in the gluteus maximus. ReSharper makes this process so easy. Take a look at this example:
- Here I have a very simple Person class that takes 1 argument for person’s name.
- I also created a console application that creates 2 instances of this class. The calls simply pass in some names for the Person class.
- Now, the very common scenario! What if I wanted to update my Person class to take in more than 1 parameter? Well, I can obviously override the method to create another one with more parameters. But what if that’s not what I wanted or needed for some other reasons? So, in this case, I am going to add an “age” and a “sex” to the class constructor.
- Notice how after compiling, I have 2 new errors. Makes sense right? Since I have 2 calls to this constructor and I just changed the method signature, each of those calls is going to complain. If I had 50 calls to this constructor, I would have 50 errors!
Resharper’s greatest contribution is in navigating errors. Without ReSharper, you would have to navigate to every single error and update the calls to have 2 new strings. What a pain in my muscular butt! However, with ReSharper it’s easy. These are the things you need to do:
- First, update your method to have 2 new arguments. Without building the code, you will notice a dashed highlight around the new method signature. Use “Alt + Enter” to open the ReSharper help menu.
- You should see an option from the refactoring menu to “Apply change signature refactoring…”. Press enter to open that menu.
- A “Change Signature” pop up wizard will open to guide you through the refactoring. Press “Next”.
- Now, a magical thing will happen where the wizard will guide you through every parameter to help you refactor your code. After you are finished, ReSharper will assign values to your constructor calls based on the options that you selected.
- There are multiple options for each parameter. They are basically self explanatory. However, for “age”, we will use a specific value of 25.
- Click “Next” after your settings look like the image above.
- For “sex”, let’s leave it as non-compilable so that we can resolve the issue ourselves. Click “Next”.
- Now build your code. In Visual Studio 2013, I can just press “F6” to build. In earlier version, this shortcut is different. Also, you can build the code by going to the “Build” tab and then clicking “Build Solution”.
- As expected, we will have 2 errors. 1 for each of the “sex” parameters that have no value. We can go to each error by double clicking on the error in the “Error List” pane of VS.
- Look at what ReSharper did. That genius added “TODO” statements in place of the argument values so that you don’t forget to replace the “TODOs” with actual values!
- Update the code to look like the image below. Also, build it after and notice how all of the compiler errors are gone.
- Congrats! ReSharper has just successfully refactored your method calls without you having to go through every error and updating it yourself. Use it in the future and you will save yourself tons of time.
Another Code Refactoring Example
So, continuing with this code base, we can further clean up.
- Notice all of the unused directives that are highlighted in gray. ReSharper is telling us that they can be removed.
- When you are ready to refactor and you want to go through each Warning 1-by-1, click on the Warning icon on the right side of your VS, in the upper right corner.
- It will take you to the first one and place a lighbulb next to the line of code. Also, ReSharper will place your cursor on that line as well.
- Just like before, press “Alt + Enter” to open the ReSharper menu and select from the options.
- Select “Remove unused directives in file…”. This step will clean up all of the using statements from your code that are not being used. Your code will now look so sleek and clean.
- By the way, ReSharper will also do the same thing with References that are not being used. You can remove them at the file level, project level or solution level with just a few clicks. This will make your code lighter and faster.
Code Refactoring – Sample 3
We can make 1 more awesome fix to the code using ReSharper’s big brain. ReSharper lets you know if there are variables that are never used.
This really saved my life recently. I was creating an automated test in Selenium WebDriver and copied over an assertion message. Because the message contained a lot of information, I stored it in a variable. I changed the variable name but never updated the variable that was being used in the assertion clause. As a result, I was checking 1 condition, but my assertion message was for a completely different check. I don’t know if I would have ever found this problem.
But ReSharper came to my rescue. It told me that I had a variable that I was never using. After a further look, I realized the problem and fixed it with a huge sigh of relief! The message looks like the image below. When you see these warnings, you can decide what to do with the variables. You can use them or delete them using ReSharper’s IDE. This helps you to clean up your code or find silly mistakes like the one that I made.
Wrapping Up!
I hope that I have been successful at conveying to you the awesomeness of ReSharper? I even showed it to 1 of our developers. This guy is really freaking smart, but also really freaking angry. He was actually impressed with how ReSharper helped us to refactor. Now that is a fact that has more weight than 10,000 words on my real examples of ReSharper features! So give it a try and let me know what you think. If you completely disagree with me, let me know why. If you love some other features, please share so that I can learn as well. The only drawback that I saw so far was that ReSharper makes my IDE slower. But, it’s worth that small performance degradation.
Talk to you all later. Peace, love and automation!
[su_button url=”/?attachment_id=5686″ target=”blank” style=”soft” background=”#0f96f3″ color=”#ffffff” size=”5″ radius=”20″ icon_color=”#ffffff” text_shadow=”0px 0px 0px #000000″]Get This Code[/su_button]
Author: Nikolay Advolodkin, Automation Engineer with a passion for Automated Software Testing.
Hi, Could you please update the links to examples? Because I receive an error: We cannot complete this request, remote data could not be fetched