Bernie Cook's Blog

Azure, C#, .NET, Architecture & Related Tech News

Put ReSharper In Your Toolbox

Leave a comment

ReSharperReSharper, the productivity tool which extends the Visual Studio IDE, is one development tool that I would certainly not want to live without. It’s one thing to have a strong skill set and another to compliment it with a great set of tools.

I’d seen ReSharper in action a number of times back in early 2012 but was never sold on it – I mean Visual Studio is an amazingly rich IDE, how could you ever hope to make it better? Well JetBrains (the minds behind ReSharper) have done just that. ReSharper is designed as an extension to Visual Studio providing an array of productivity benefits for both teams and individual developers.

I’m conscious that by now I’m starting to sound like a salesman, but do bear with me. What I’ve done is bundle together a few ReSharper features in this post. Hopefully it’s enough of a introduction to convince you of it’s necessary place in your toolbox.

Example Quick Links

  1. The “New & Improved” Code View Scrollbar
  2. Naming Convention
  3. Remove Unused Assemblies
  4. Refactoring Heaven
  5. For Becomes LINQ
  6. Unit Test Runner
  7. Configuration Sharing

Examples

Example # 1 – The “New & Improved” Code View Scrollbar

One of the most obvious benefits is the visual status guide contained within the vertical scrollbar in the code view window.

You can see the strips of amber and green below, which provide a visual guide to hints, suggestions, warnings and errors within your code.

AccountController.cs
If you hover your mouse over these visual guides you’ll see a popup appear which tells you more about the particular item. Clicking on the guides auto-scrolls you to the highlighted point in code.
Tip
As shown above the green bar is providing a suggestion, clicking on it takes me to this line of code:
PersistCookie
Once there I can see an optional parameter name coloured in gray and if I hover my mouse over it the “Redundant argument name specification” tip will appear. Very handy indeed.

Question: How does ReSharper know what’s a hint, suggestion, warning or error?

Out of the box ReSharper has a number of default classifications, based on these classifications, it knows how to flag a particular portion of code.

You can change these settings if you like, simply go to the ReSharper main menu bar item and select the Options choice from the menu. This will open up a window with all of the ReSharper options available to you.

Plays Well With Other Extensions

If you look even closer though you’ll see a red dot down the bottom of the vertical scrollbar – see below for a closeup:
Cancelled
This is because I have a Visual Studio extension installed called “Spell Checker”  which points out any spelling mistakes. Despite the fact that ReSharper has nothing to do with Spell Checker it will still highlight any warnings raised by this extension.

This “at-a-glance” type support is incredibly useful and when I’m performing code reviews it is always the first thing I look to – but not the only thing mind you, just the first 🙂

Example # 2 – Naming Convention

I’ll admit, I’m a real nit-pick when it comes to ensuring variables are named consistently throughout a project. Take note of the blue underlined variables below:
Naming Conventions - Before
So I’ve constructed an example above with 3 poorly named variables. ReSharper catches these as I type, underling them in blue. Now for the best bit, if I want to avoid renaming/refactoring them myself (leaving the heavy lifting to ReSharper) then all I have to do is click on the variable name and choose the primary option from the newly available context menu from the side:
Naming Conventions - During
I get name a nice name suggestion and clicking on this option tells ReSharper to rename the variable as well as performing any refactoring necessary.
Naming Conventions - After
You can even change the naming conventions per language if you’re not happy. Just open the ReSharper Options window in Visual Studio and scroll down to C# > C# Naming Style  section, or another language of choice. I’ve included a screenshot of this features location to save you hunting around for it.

Example # 3 – Remove Unused Assemblies

How much does it bug you when you start a new Visual Studio project and expand the “References” folder only to find an abundance of assemblies, some of which the chosen template doesn’t even use! ReSharper to the rescue. Right-click that References folder and select the Remove Unused References option.

This is what I get when I perform this action on a default MVC internet application – a lot of unused assembly references:
Remove Unused Assemblies
Resharper scans the project to find those references not in use, then presents you with a list of those that it can determine you’re not using. Clicking the “Next >” button will remove them and you’ll be on your way. Excellent!

I must warn you that if you’re referencing an assembly via a string reference, or via some other manner that an automated scan won’t be able to easily identify, then do be careful that you don’t remove it unnecessarily and find out the hard way at runtime.

Example # 4 – Refactoring Heaven

I won’t say a lot here other than if you can think of any, and I mean any, type of refactoring possible then ReSharper will be able to deliver. Just right-click that variable, method, property, class … whatever and select the Refactor option and voila:
Refactor
This is just from right-clicking “_viewName” variable referenced above, many more than the default IDE provides.

Example # 5 – For Becomes LINQ

Putting the topic of premature optimisation to the side for a minute I wanted to illustrate how ReSharper can take a FOR/FOREACH statement ad suggest an equivalent LINQ-expression – please excuse the simplicity of this example.
Foreach
You’ll notice the green underline on the foreach – from the ReSharper options available you’ll see the primary one is to convert this to a LINQ-expression. And the resulting output is:
LINQ-expression
Now this is a very basic example, but I’ve come across much larger iterators and ReSharper has still managed to find the comparable LINQ equivalent.

Example # 6 – Unit Test Runner

Yes it comes with a unit test runner as well, and works perfectly well with a number of unit test frameworks i.e. NUnit, MS Unit Testing Framework, etc.

This is something you could easily live without but I wanted to include it to show that ReSharper does more than just provide coding support.

Example # 7 – Configuration Sharing

Can you share your custom ReSharper with your team for a given solution? Most definitely, there is an option for that too.

While your solution is open in Visual Studio you can go to the ReSharper menu option and select to save the current settings to the open solution. This generates a file in your solution folder which allows you to disseminate common standards, code completion settings, etc. across anyone working on that solution.

It’s become quite common for companies to adopt the sharing of ReSharper solution setting files over coding standard documents, and for good reason.

Summary

Hopefully this sheds a bit more light on ReSharper and gets you thinking about whether or not you’d consider it a worthy addition to your toolset.

For an exhaustive list of all of ReSharper’s features check out the official website.

The above examples are all based on a default MVC 4 Internet Application using Visual Studio 2012.

Licensing

There are a number of licensing options (unfortunately it’s not free) but admittedly they’re reasonbly priced, and after using ReSharper for over a year I’d definitely say it’s worth the investment.

Advertisements

Author: Bernie

I currently live and work in the UK, just outside of London. I've been working in IT for 15+ years and in that time have solved many technical problems via blogs, forums, tutorials etc. and feel like I should be giving something back. This blog post is my way of contributing and I hope it proves just as useful to others as their contributions have to me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s