Monday, November 17, 2008

Resharper : The Most frequently used feature

A while a go, Rory asked me to blog about Resharper add-in for Visual Studio.NET and which shortcut keys I use the most. This one has been waiting on my draft list for a couple of weeks now. This post is about Resharper shortcuts I mostly use.

Resharper is one the most advanced and useful add-in available for Visual Studio .NET. I've started using the tool when they published their release for Visual Studi 2003. Back then, Resharper added lots of feature and brought productivity to the IDE which without them, your programming life would have been a nightmare.

The problem with Resharper though, is that it has become so big and featurefull, that is won't be possible to use it in large scale projects. We have a bloated Solution containing about 80 projects that started back in 2003 with only 10-12 projects. When I open the project in VS with Resharper running, the files with 2-3K LOC make Resharper run out of memory. 2-3K LOC might sound unrealistic to you, but automatically generated codes like DataSets or Forms code behinds are usually this large. So, it'd be a good idea if we could disable some Resharper features to gain some more speed, e.g. disabling code analysis in .Designer files, disabling XAML analysis, etc.

As said before, there are a lot of features in Resharper, which makes your programming experience a pleasant one. Here I’m only pointing to some of the ShortCut keys I mostly use.

Find Usage Window (Alt + F7)
I’m Sure you've tried to figure out how / where an interface is implemented, or where instance of a certain class is being used, or other similar scenarios. If you use plain VS, you have no other choice than to use "Find All References". That will work, but it takes time every time you do a search like this. This is due to the fact that VS does not keep an index of your application structure, but since Resharper does, you'll gain a lot of speed when doing these kind of operation.

Also, the "Find Usage Window" is more rich in Resharper than VS. You can sort and group the results based on Namespace, Usage, File, Folder, etc. and filter by Read, Writer, Invocation, etc. but in VS all you see is a flat search result. One of the great things about Find Usage function is that it actually finds the usage in Xaml code too!


Using Hints and Red / Yellow Bulb (Alt + Enter)
Resharper has a good way of letting you know that you can do something better, by displaying "Hints". Hints let you know if you've forgotten to import a namespace, if there's a refactoring available or suggest a context action. There are lots of things you can do in Resharper, just by pressing Alt + Enter! Some of them are :

  • Change scope of the Class or Method
  • Create overload for a method / Check if the variable is null
  • Create derived types
  • Import missing namespace / remove unused namespace
  • Implement an interface / abstract class
  • Change property to Automated property and vice versa
  • Invert if statement / Convert to conditional statement and vice versa
  • Join / Split declaration and assignment of the variable
  • Introduce variable / use var keyword
  • Generate body of the switch statement

…and a lot more. Depending on where the cursor is in the editor, where you click and the code you have written, you may get different options.

Goto Type / File (Ctrl + N, Ctrl + Shift + N)
If you're looking for a specific file in your large project or if you're using multiple class per file strategy, you might get lost easily. If you need to know where that file is located, or in which file you class resides, you can use these shortcuts to get you there. The "Goto Type" function has support for a feature called CammleHumps. Using this feature, if you're looking for OrderApprovalService, you can search for OAS instead and Resharper will find it for you.



Code Completion and Type Lookup (Ctrl + Space, Ctrl + Alt + Space)
When you’re working with large libraries you may easily get lost. Sometimes you know the name of type you’re going to use, but can’t remember the darn namespace! This shortcut will help you find the type you are looking for in ALL the namespaces in an easy to use fasion.


Code Generation (ALT + Ins, Ctrl + J)
The code generation feature in Resharper is just great. You can use the primary code generation function by ALT + Ins that will let you generate :

  • Constructors
  • Properties / Readonly Properties from backing fields
  • Implement Method / Property stubs for an Interface
  • Override base class Methods
  • Provide equality implementation (Equality Operators, IEquatable<T> interface, overriding Equals and GetHashCode methods)

While this is great, Resharper gives you the power to write your own snippets! A lot of people have already created snippet templates for Resharper to create Unit tests, Mocks, NHibernate, ActiveRecord and WPF stuff, etc.


You can download Resharper templates on Joey Beninghove’s blog, or see what other people use on StackOverflow page.

There is a document on JetBrain’s website that shows all the ShortCut keys you’ll ever need to know. Intense Resharper users might need to hang it on their wall. There are lots of other features I use like Unit Test Runner, Stack Trace browser, File Structure window, and ToDo List, but that’s for another post. Do you use productivity tools too? Which one? Which feature do you use the most?

Submit this story to DotNetKicks Shout it

No comments: