18 July 2016

DadenU Day: Using Resharper


Joe Robbins, Junior Developer writes:

For our recent Daden U day, I decided I would look into Resharper, a suite of tools for improving productivity in Visual Studio. The wealth of features on offer is a bit too great for me to cover exhaustively in this post, but I will be talking about some of the ones that stood out for me, along with my general opinion on the whole package.

I began by watching the video above which gave an overview of the tasks that ReSharper can assist a programmer with. I was impressed immediately, and began to question why some of these features didn’t come with Visual Studio as standard.

Setting up Refactor couldn’t be more straightforward, just sign up for a free trial, download the installer, fire up Visual Studio and you’re good to go.

I then realised that it would be difficult to try out ReSharper in full, as doing so required a pre-existing project. However, I did cobble together some minimalistic C# classes and began playing around with them.

So here’s my list of the top four tasks that ReSharper makes that much easier, although note that this was only based on a few hours of experimentation and I may well discover even better ones over the remainder of my free trial.

Generating constructors

If you’ve spent any significant time doing object oriented programming then you’ll be able to appreciate my first pick. Whenever I create a new class, I tend to start by defining the various properties that need to be included, and then I’ll move onto the constructor. The issue being that most constructors are simply boring, just initialising the properties from the passed arguments.

But thanks to ReSharper, this boredom is a thing of the past.

Simply hit Alt+Enter to bring up the ReSharper menu and select "Generate Code".

Choose “Constructor”.

Tick off the properties that we wish to initialise in our constructor and hit “Finish”.

And there we go, our whole constructor created without a single keystroke.

Surrounding code

Another small touch that could add up to plenty of time saved is the ability to surround code with things such as while loops, or try/catch blocks. ReSharper’s got you covered on that front.

Just highlight the code you wish to surround and press Alt+Enter to bring up the ReSharper menu. Go to "Surround with…" and chose the construct that you want to wrap around the highlighted block.

And ReSharper does the rest, leaving me free to fill in my while condition without having to write any of those pesky braces manually.

Extracting Class from Parameters

Many of us have been in the situation where the parameter list for a method we’re writing just keeps growing and growing. Our programmer conscience nags at us to turn the argument list into its own class, but that’s far too much work.

Let’s say we have a method with a few parameters, like the one here. We can press Ctrl+Shift+R to bring up the ReSharper refactoring menu, and select "Extract Class From Parameters".

We give a name for our new parameter class and describe which parameters we want to include within it.

ReSharper does the rest, producing our new class, along with constructors, getters and setters. It also alters the parameters for our original method to take account of this change.

Move Type to Another File

Sometimes when we code, we will put a small class in the same file as pre-existing one, assuming it will stay small and won’t need to be used elsewhere in the project. Often, however, this is not the case, and when this happens it is good organisational practice to move the second class into its own file.

Simply place the caret next to the name of type we wish to move and press Ctrl+Shift+R to bring up the refactoring menu, then select “Move To Another File”.

Give a name to the new file we’re creating.

Then when we look at our project, we can see that we have a new file which contains the definition of our class.

Conclusion

On the whole, I was very impressed with the power and ease-of-use provided by ReSharper and still feel that I'm only scratching the surface. And that’s before mentioning that the premium version of the plugin comes with tools for performance and memory profiling, unit testing assistance and more.

So do I feel that ReSharper is worth the money (£239 for a year as a new business customer, more info here: https://www.jetbrains.com/resharper/buy/#edition=commercial)? At the moment it is too early to tell, come back to me after I’ve finish my 30-day free trial. What I can tell you is that even during my brief time spent with these tools, I could see how they would fit into my workflow, saving me plenty of time over the course of a project. I wouldn’t be surprised if all that saved time began to cover the cost of getting a licence for this very promising toolbox.


A PDF version of this article with screenshots is available at: http://www.daden.co.uk/docs/ReSharper-DadenUDayBlog.pdf

No comments:

Post a Comment