Friday, June 13, 2008

ReSharper 4.0

When I was in Seattle last, I was writing some code on my laptop in C# and fellow MVP Jan Yeh happen to notice my language of choice was C#. He asked me if I had every heard of ReSharper, I told him I had heard of it but hadn't used it. Jan highly recommended it, when I arrived back in the UK, I shortly received an email from Jan with details of the developers over at JetBrains to contact in order to get a license for ReSharper.

After contacting Britt at JetBrains I shortly received a response which included a MVP license so I downloaded the tool. Since doing this I've never looked back and I don't know how I ever lived without it, it is a must have tool for any developer.

The version I downloaded was 3.1. Version 3.1 had separate installs for VS 2005 and VS 2008. Also, under VS 2008 there was no support for C# 3.0 or LINQ. In the latest release which was released on 9th June (ver 4.0) there is one single install for both VS 2008 and VS 2005 which makes the whole install experience cleaner. In addition there is now support for C# 3.0 and LINQ.

You can get a 30-day trial of ReSharper here. I mentioned C# above, but in fact the tool works with VB as well, although I haven't tried it with VB.

So what does ReSharper do? In a nutshell ReSharper enhances the development experience for a .NET developer. There are so many features I recommend you try it to find out. But to wet your appetite, I will show a few cool features that I particularly like.

The above example shows a program that has declared a bunch of using declarations but two are not required and they are identified as redundant due to the gray fore colour, neat.

Placing your mouse cursor over the redundant using statement shows a tool tip:

Also placing your mouse cursor over the gray text, reveals an icon as per below. When you click this icon it will reveal a menu which in this case gives you a list of options, one of which allows you to remove the unused using statements. Sweet. In fact this icon is used extensively throughout the IDE with regards to add on features.

There is another nice feature that I'd like to talk about which is related to documentation in your code. Notice in the screenshot below both param elements are incorrect. They are identified as incorrect as the forecolour is set to red. This feature is really powerful as if you use a tool such as nDoc to generate your documentation for your libraries then the documentation will be wrong. This also works with complex types. If x was a complex custom type which didn't exist, you'd get the same red marking. This makes it even more powerful as the documentation will not be able to link to the required types that do not exist which often results in an error message being displayed - doesn't look good when your clients are trying to use your libraries.

Do note with this though, there is no warning output in VS (not sure if there is a configuration to do this).

Intellisense is greatly improved under ReSharper which is seem less in Visual Studio. For example. If I have a couple overloads of the method named Foo as follows:

I might be feeling lazy and instead of looking at the documentation for method Foo, I might want to see what overloads and parameters etc that are available to me through intellisense - this is quite common for developers.

As you know in Visual Studio, if you are feeling lazy, usually you would sequentially iterate through the overload list to view them all. ReSharper addresses this problem by giving you an exploded view of the method overloads with all parameters:

This is quite neat. As you can see, we can clearly see all overloads for method Foo. What we can't see is comments for all overloads at once. We have to highlight the overload we're interested in in order to see the comments. I'm guessing here, but probably JetBrains thought about this and if they were to output comments (documentation) for every overload at the same time you might quickly run out of screen space for very large classes with multiple overloads. We only have three in the above example, but some methods have many more than this with many parameters in some cases. I really like this feature.

Another cool feature related to intellisense is filtering of member types. So If an am querying an object with many members, properties, methods, events etc as I type, ReSharper filters out the list of items:

In the above screenshot, we can see the list of all members because we haven't yet typed anything.

In the screenshot below you can see we have typed the letter 'a', so here ReSharper filters all members that begin with the letter 'a'. Likewise if you were to type the next letter, it will filter by the first two characters and so on:

Another cool feature to mention. If you look at the screenshot of the intellisense which displayed a whole list of all members (like VS.NET has always done). Notice in the ReSharper version, some members are displayed in bold while others are not. The members displayed in bold are members that belong to the type in question as opposed to the members that are in regular style font are members that are derived from the type we are querying.

With the new release of ReSharper 4.0, C# 3.0 is supported:

Above we are using the new C# 3.0 feature which is known as collection initializers. If you would like to learn more about C# 3.0, see the latest C# specification here. I've written a couple of blogs on the new cool features available in C# 3.0 here.

As ReSharper is a Visual Studio plug-in, when installed a new menuitem appears under the mainmenu in VS:

There are many features in here many of which relate to the running of ReSharper, I'm not going to go into all of these but one in particular I thought I'd mention - which is in fact not on the main menu but defined in the context menu when right clicking a class in VS. This option is 'Cleanup Code...".

Clicking this option will load the Code Cleanup dialog:

As you can see from the dialog this process is profile based (new feature in 4.0). Clicking 'Run' will cleanup your code based on the profile rules. Clicking 'Edit Profiles' allow you to change the formatting to your custom requirements. This process does a whole bunch of stuff which enables you to adhere to coding standard easily. You can see how this is very powerful when you have a shop full of developers and you want the coding standard to stay intact. ReSharper is your answer to this. There are of course other tools such as fxCop which does a similar job although it doesn't clean your code up rather tells you want you are doing wrong!

There are so many more features in ReSharper in which I haven't talked about, I advise you to download and try today....


Ruairi said...

I am working in an environment with Resharper and I turned it off as it slowed my development down by "helpfully" adding curly braces and closing brackets everywhere which I would then type myself and end up with two of everything. However after reading this I'll trun it back on and give it a go again - I'm sure I'll get ued to doing less work in no time!!

Simon Hart said...

Ruairi: You will get used to it. I found that feature strange when I first started using ReSharper.

I don't think you can turn that specific feature off either. One thing I didn't mention is the support for NUnit, this is brilliant with ReSharper. Sadly though no support for MSTest.


Unknown said...

Hi Simon,

I know it's an old post but I never realised you made the jump. A belated welcome to the world of ReSharper. Hopefully you are on 4.5 now. Version 5 should be bringing some big improvements to nUnit.

Matt Davis

Simon Hart said...

Hi Matt,

Yes my company uses ReSharper, I can't work without it now. Ctrl + T is the most useful function I think (assuming you have VS shortcuts and not Java).

Not yet looked at v5, yes I'm on V4.5 but we use MSTest not nUnit due to TFS integration.