Category Archives: ReSharper

Slides from ANUG VS Launch Event

I spoke last week about ReSharper 5 at ANUG’s Visual Studio 2010 launch event.

Here are the slides from my presentation. The slides are in danish and probably won’t make too much sense as most of my presentation was done demoing stuff – but they should give the gist of it.

Slides

If you have any questions on my presentation, feel free to shoot me a mail here on the blog.

Black / Blue Visual Studio 2010 + ReSharper 5 Theme

I have been using black background in Visual Studio for as long I can remember. I started out using Rob Conery’s black / orange TextMate theme, but last year I created my own black theme with a blueish style. Today at the ANUG code dojo we tweaked it to actually look alright with the changes in Visual Studio and especially ReSharper.

You can download the theme here if you want a nice black theme.

It looks like this:

theme_thumb

Selection:

image_thumb

Subtle highlights of active identifier:

image_thumb

Enjoy :-)

Slides from Miracle Open World

Last week I gave two talks at MOW2010. Was an awesome conference and the 80% talks + 80% networking concept really held true. Hope to be going again next year – as speaker or otherwise. Here is the slides from my two talks.

Increasing productivity with ReSharper

This talk is about optimizing the mechanical part of your work. See how a keyboard-centric focus can speed up your work and how to navigate codebases easily independent of size. Visual Studio 2010 has introduced more advanced keyboard features, but ReSharper is still king, so it will be the main focus of the talk. While this session will contain a lot of fast-paced flashy keyboard shortcuts, it will also contain basic techniques and advice for you to get started with your own keyboard.

Slides

Practical ASP.NET MVC 2

ASP.NET MVC is the new kid on the Microsoft block. This talk will give you a short introduction to the framework and the new features in ASP.NET MVC 2. After the introduction, we will dig into some practical experiences and common situations of actually implementing a system using ASP.NET MVC. Detours will include other alternative open-source web frameworks and maybe even some JavaScript.

Slides

ReSharper Series – Part 6: Find Usages

This is the 6th post in my ReSharper Series – this time we are going to look at how to find usages of particular code members in your code. While plain old text search can be useful sometimes, a structured search is really awesome for getting a good overview. In addition, the standard ReSharper setup on this feature is rather bad compared to what is possible, so a bit of UI tweaking (very simple) is also available.

Find Usages

Find usages works on almost all code elements, be it classes, methods or variables, invoking it by pressing Shift+F12 [IntelliJ: Alt+F7] will open a result window, which show the places in your project / solution where the element is used. Using the ASP.NET MVC source, in the Controller class, invoking Find Usages on the ModelState property:

image_thumb_5

This gives us the following results window:

image_thumb_12

Usages are presented on a namespace level, showing the line of code that includes the given element. Double-clicking or hitting Enter on a line jumps to the file.

Pimp My Search Results

While this results window is useful, you would often like slightly more information. In the options row of the Find Usages window, there is a group by drop down, which is set to Namespace as default – and most people I have seen using ReSharper never change this.

image_thumb_7

There are a lot of grouping options available, as shown above – I usually prefer the Namespace and Type option, giving me the same Namespace overview that I had before, but allowing me to see the names of the actual classes that use the element.

Another useful option is the Show Preview option, also found on the options row of the Find Usages window. This is disabled by default, but enabling it will show a preview of the code surrounding the selected line in the search results.

image_thumb_9

Since I usually have my Find Usages window docked in the bottom (auto-hiding though) of my screen, I prefer setting the Show Preview to Right.

With these small tweaks, the Find Usages window now look like this:

image_thumb_11

Go To Usage

Sometimes you are not interested in looking at the results of your search in the Find Usages window – you have your cursor at some code element and you want to navigate directly to a usage. Hitting Alt+Shift+F12 [IntelliJ: Ctrl+Alt+F7] will bring up a code-completion-like-menu to quickly jump to a usage. Invoking this on the ModelState gives us the following result:

image_thumb_4

Selecting an item from the list jumps directly to usage. In a later post, we will explore more of these quick-navigation options from directly within the code editor.

ReSharper Series – Part 5: Generating Code

Welcome to the 5th part of my ReSharper Series – in this post we are going to take a look at how you can easily generate a lot of the fluff code that surrounds your real functionality.

While C# is a great language, there is a quite of bit of the code that you write every day that feels somewhat crufty. It is these standard things that we do a thousand times, like writing constructors, properties, backing fields to said properties (better with automatic properties – but you still often need a backing field). Luckily, ReSharper can help ease your pain – or at least let you focus on writing that core functionality – and not worry about the cost of adding another class (in terms of typing).

Generating Class Members

We have already the power of Alt+Enter in one of the earlier posts – and this a tool that most people I have seen use for creating new code. It can do stuff like implementing missing methods and assigning creating backing fields for you from constructor arguments. If we use it on our balance argument that is unused (gray), ReSharper offers the following options:

image_thumb

However, we might want more – we had to write the constructor ourselves – and we have to create the property afterwards. Let’s introduce another shortcut – Alt+Ins. While Alt+Enter is a general purpose tool, Alt+Ins is focused on generating code. Use it anywhere in a class and you will be presented with the following options:

image_thumb_1

Generating a constructor will bring up a dialog allowing you to select which members / properties you want to initialize from the constructor. Read-only properties / Properties will give quick options to create read-only properties from whatever backing fields you already have in your class. Implementing missing members will create any members not yet implemented from interfaces or abstract base classes. Equality members will implement Equals and the equality operators – GetHashCode included, based on the properties / fields you choose. Formatting members will introduce a ToString method that contains the values of whatever properties / fields you want.

The last one I didn’t mention is the one I think is mostly underused – it is the Delegating members option. It is often useful in object oriented design to encapsulate another class and provide delegated methods for a number of the contained type’s methods. The perfect example is when implementing the decorator pattern – this pattern, by design, requires you to delegate all the methods the contained type. This is a pain to do by hand. However, let’s try using the delegating members option in my Account class, after adding a List<int> – like so:

image_thumb_3

This brings up the following dialog:

image_thumb_4

So basically, ReSharper lets me pick and choose any or all methods / properties that I want to directly delegate. For this class, maybe I need the Add and indexer methods, so I check them off and hit finish:

image_thumb_5

The code for delegating members is silly simple – and this is EXACTLY why we want to generate it in the first place. Generating delegating members let me focus on my intent of which options I want to expose from my class instead of the laborious task of typing out the code.

Generating Files

As a final note in this post, I would like to introduce another usage of Alt+Ins which I have grown quite fond of – in the solution explorer, pressing the shortcut will allow you to add new files in a more lightweight way than the usual add file dialog:

image_thumb_6

Later, when we look at live templates, we will also see how to add your own file templates to this menu.

ReSharper Series – Retrospective?

Now, I’ve written the first 4 (7 really) parts of my ReSharper Series – I was a bit curious if anyone is actually reading it – and if anyone is getting value from it? Personally I am learning quite a bit more than I already knew about ReSharper. So a few questions:

  • So, are the topics too basic? Too advanced? Too long posts?
  • Would it be better with screencasts? I feel it can be kind of hard to show the speed that you can obtain with ReSharper using simple screen shots.
  • Are there anything in particular you would like to hear about? Hear more about something that has already been mentioned?
  • Other suggestions?

I am going away on ski vacation next week and then to Copenhagen for a few days after that (for the MDIP meeting), so there’s probably not going to be any more posts until I get back in about 2 weeks.

Hope to get a few useful comments (or mails) on this post.

ReSharper Series – Part 4: Moving Code

Welcome to the 4th part of my ReSharper Series. Today we are going to look at one of my new favorite ways to use ReSharper – to move code around. We haven’t dipped into refactoring really yet, and today isn’t really going to be that much about it – this is moving code on a lower syntactic level – shortcuts to save that copy / paste. In addition, we are going to have a little bit of code navigation that plays well with this feature.

Furthermore, since many people use the VS binding mode in ReSharper, I am going to try and supply both binding sets in the future. I might even go back and change the previous posts at some point – today all the bindings I will show work for both binding modes though.

Navigating Methods

The first small feature is navigation between members. Basically what it gives you is a way to quickly navigate between methods in your class. If you are in a method like so:

image_thumb

Pressing Alt+Arrow Down will navigate to the next method, while using Alt+Arrow Up will send you to the method signature, like so:

image_thumb_1

When at a method signature already, you can jump up and down between method signatures in the same way.

image_thumb_4

This comes in handy when moving methods, since you need to be at the method signature to do so.

Moving Code

The shortcuts for moving code involve a lot of keys, but they are pretty easy to remember. You can move code over 2 axis, up/down and left/right. This is controlled using the arrow keys. To enable the move functionality, you need to hold down Alt, Ctrl and Shift.

Moving Methods

Let us look at the first example. With our cursor placed on the method signature of the Withdraw method, we hold down Alt, Ctrl and Shift to enable movement. This will make the block of code we are moving turn a light cyan (with my color scheme at least):

image_thumb_5

Hitting the move up shortcut (Alt+Ctrl+Shift+Arrow Up) sends our method above the Deposit method like so:

image_thumb_6

We can also move methods down using Alt+Ctrl+Shift+Arrow Down. Quite a bit easier than copy pasting it – and useful for reordering methods quickly if combined with the method navigation above.

Moving Arguments

Now we can move quite a bit more than methods. Say we have a method call where we wanted to move the arguments. We can do so using Alt+Ctrl+Shift+Arrow Left and Alt+Ctrl+Shift+Arrow Right. Again, as we hold down Alt, Ctrl and Shift, the block we are about to move is highlighted:

image_thumb_7

And sending it left is easy:

image_thumb_8

This also works for actual method signatures, just be aware that this doesn’t actually refactor your method and change all the call sites for the method (although ReSharper can do this in it’s refactor menu – look for the Change Signature refactoring).

Moving Statements

When we are dealing with statements inside a method, it can some times be useful to reorder lines of code or move code in and out of control structures, so when we hold down Ctrl, Alt and Shift here it actually suggests that we can use all 4 directions.

image_thumb_9

Moving up and down lets us maintain our level of scope, in this case, pressing Alt+Ctrl+Shift+Arrow Down, we would send the method call into the else branch:

image_thumb_10

Repeating this would send it below the negativeText call. When we move left and right, we move in and out of scopes, thus if we press Alt+Ctrl+Shift+Arrow Left, we yank the statement all the way out of the entire if statement:

image_thumb_11

Again, if we move up and down here, we maintain our level of scope and thus do not re-enter the if statement unless we move the statement right.

image_thumb_12

Reordering Expressions

The last short example is reordering expressions, this makes it easy to reorder expressions and move parts of them around. Like here:

image_thumb_14

Moving Balance right would actually swap the two values.

image_thumb_15

Summary

What I have shown is some of the possibilities for moving code, but as always, play around with it – you can move quite a bit more – like fields and properties in your classes – possibly a lot more.

ReSharper Series – Part 3: Auto-completion / Intellisense

This is the 3. part of my ReSharper series. Today we are going to have a look at how ReSharper helps you complete your code / show options while programming. I know I didn’t know about some of these for a long time, maybe there’s something new for you as well?

Standard Symbol Completion

This is the basic intellisense that you are used to and the one that is automatically provided for you whenever you are typing. It contains everything that matches the prefix that you have written in the scope (class, object, etc) that is relevant – just like you are used to from normal Visual Studio intellisense.

image_thumb

The keyboard shortcuts for enabling this type of completion is either Ctrl+Space or Alt+Right Arrow, with Ctrl+Space being the usual one. Now as I mentioned this is enabled when you are typing, but say you have returned to a line of code you were writing and it doesn’t pop up on its own, this is the shortcut to be hitting.

Import Completion

Remember in part 1 when I mentioned an easier way to import namespaces than typing out the full name and doing a quick fix (Alt+Enter)? Well this is it. With this auto-completion mode, you can as ReSharper to complete the names of types that are not imported yet, thus not requiring to type the full type name and getting the automatic import for free.

Say I wanted to use a LinkedList in my CheckBalance method shown above, the standard auto-completion isn’t very helpful since I have not imported the collections namespace:

image_thumb_5

This doesn’t really help me much. However, if you enable symbol import by pressing Alt+Shift+Space [IntelliJ: Ctrl+Alt+Space], this is what you get:

image_thumb_6

ReSharper will complete the common prefix and suggest type names in other namespaces. When using generic types I usually hit < at this point and type out the generic constraint. While writing this post I noticed that it was behaving oddly when completing types with multiple generic types, but it might just be me. Anyway it is a really helpful in addition to the basic intellisense that we usually use.

Smart Completion

To be honest I hadn’t looked at this completion mode until recently – but ReSharper actually also has a completion mode that tried to be clever about the type context of the expression you are currently writing. Since ReSharper knows the scope of the code you’re writing and it has an idea about the current expression, what smart completion does it to filter the completion list to only show symbols that have the relevant type.

I find that I mostly use smart completion when I am writing method calls, especially if I am either messing in some code that I don’t know that well (since the completion list is often much smaller) or if I know that there is only one possible completion. In the latter case, ReSharper will do the full complete when enabling the completion and proceed to the next parameter instantly.

If I have a method that takes a boolean for example, enabling smart completion will only suggest symbols that actually have the relevant type, like so:

image_thumb_8

The shortcut to use smart completion is Ctrl+Alt+Space [IntelliJ: Ctrl+Shift+Space]. I find that it is somewhat situational for me, but it is useful – I use the other two more though.

Hint: Out of personal opinion, I suggest that people using Visual Studio bindings remap these keys so that Import Completion become Alt+Ctrl+Space like in IntelliJ – I use it much more often and it’s so much easier to hit this combination. Smart Completion could then be Alt+Shift+Space.

Camel Humps

Søren mentioned camel humps when I was talking about basic navigation in the last post – and this also holds true for all the auto-completions shown above. If you type upper case letters while completing, it will match camel humps in the list. For example, if I wanted to use a LinkedResourceCollection as seen in the symbol completion example, here’s what I could do:

image_thumb_9

I find that this is mostly useful when you have types with long names that you use often.

Case Sensitivity

Another thing you can do if you want more control over what is filtered in the completion lists is to enable case sensitive prefix matching in the ReSharper settings under IntelliSense -> Completion Behavior.

image_thumb_10

This will force ReSharper to match the case as you are typing and it will only present symbols that actually match the case – this could potentially be useful if you don’t prefix your instance fields (personally I prefer prefixing with underscore).

Without case sensitivity:

image_thumb_13

With case sensitivity:

image_thumb_12 image_thumb_11

I haven’t played around with case sensitivity enabled so much, generally I just accept having a longer result list and not being forced to case my identifiers.

Summary

In this post I gave an introduction to the auto-completion modes of ReSharper, try them out and see what works for you, I especially recommend the import symbol completion if you are not already using it.

ReSharper Series – Part 2: Basic Navigation

At part 2 of the ReSharper series, we will attempt to enable more “no mouse survival”. We will take a look at navigating between files without using the mouse. This is also a great chance to mention that not all the stuff you will see here is exclusive ReSharper stuff, some times I will throw in some Visual Studio shortcuts – the essence is to increase productivity – not religiously using one product for everything. In addition I have been using the combination for so long that I am often confused what is what.

Opening Files

ReSharper offers many ways of navigating between files based on what you need – and we are going to look at a few basic ones today.

The first one is called Go to Type and is activated through Ctrl+T[IntelliJ: Ctrl+N]. This will bring up the window shown below. Basically it’s a quick search in all your classes.

image_thumb_4

Most often, navigating types is what you want, but sometimes it can be useful to navigate files instead, especially for configuration files, NHibernate mappings and other special files. ReSharper has a Go to File shortcut that brings up the following window – Ctrl+Shift+T [IntelliJ: Ctrl+Shift+N]:

image_thumb

Both of the search windows allow * wildcards, like in the below search where I wanted to find all the files that contain the word “Base”:

image_thumb_1

They also allow the use of the + wildcard to denote one or more characters…

image_thumb_5

… and the ? wildcard to denote zero or one character:

image_thumb_6

Closing Files

Closing tabs in the visual studio editor is as easy as pressing Ctrl+F4. Not a ReSharper shortcut – but nice to know.

Accessing the Solution Explorer

While ReSharper gives us a nice way of opening files by name, it can still be useful to bring out the good ol’ Solution Explorer once in a while. There’s two shortcuts for this. There is a standard Visual Studio shortcut which will bring up the Solution Explorer tool – Alt+Ctrl+L. This will open the tool in it’s current configuration – like you left it.

Lets imagine that I am browsing the MVC source – standing in the ControllerBase file in the System.Web.Mvc project. In my Solution Explorer I browse to the MvcFutures project to see something. After leaving the Solution Explorer, pressing Alt+Ctrl+L will bring me to my last location again:

image_thumb_2

However ReSharper also has a shortcut for bringing up the Solution Explorer – it is called Locate in Solution Explorer -** Alt+Shift+L**. Using this in the previous situation will actually track the current file and open the Solution Explorer with this file highlighted, as shown below:

image_thumb_3

I actually find both shortcuts useful in different situations – but play around with it and see what works for you.

Escaping Tools

Another quick Visual Studio tip – the Solution Explorer and other tools you might activate while coding are easy to deactivate again with a quick Esc. This will bring the focus back to the code editor.

ReSharper Series – Part 1: The Power of Alt+Enter

Woo, finally hit part 1 of the ReSharper series and ready to start on the basics of ReSharper. Today we will look at the basic look and feel after you have installed ReSharper and fired up your Visual Studio. Learn one of the most basic commands.

The single thing you will probably use most in ReSharper is the quick-fix command – which is initiated using Alt+Enter. This is a context-based command which will suggest actions based on where your cursor is located. ReSharper provides different visual cues to alert you that an action is available. Lets look at a few examples of the versatility of this command.

Implementing Missing Methods

Say I am implementing the standard Account class and want to check something with the balance when doing a withdrawal. As I write the code shown below, ReSharper will pop up a red light-bulb on the left of my method as my cursor is on it. This is ReSharper’s way of telling me that an action is available.

image_thumb_1

When you see a yellow or red light-bulb, pressing Alt+Enter brings up the action menu:

image_thumb_2

In this case, ReSharper is offering to create the missing method for me. Picking the default option by hitting the Enter key creates the new method with a default implementation:

image_thumb_3

ReSharper will show these light yellow boxes for on our new method for things that we might want to change. They don’t go too well with my color scheme, but the idea is that ReSharper suggests a return value, a method name and types/names of the arguments. It is easy to navigate between the yellow boxes using either Enter or Tab.

After all the yellow boxes have been resolved (just pressing Enter to go with the default) the whole exception line will be selected and we’re ready to start implementing our method.

Errors like this will also often show up as red squigglies like the ones you know from Word.

Removing Dead Code

Another thing you will probably notice after installing ReSharper is that some of your text turns gray like this:

image_thumb_4

Gray text is ReSharper’s way of letting us know we have dead or unused code. We are not using the amount argument for anything in the method and thus it can safely be removed. Placing our cursor on amount, another light-bulb pops up and gives us the following context menu:

image_thumb_5

This is where we start benefiting from the fact that ReSharper knows the structure of our code. It is suggesting that we remove the parameter and update the usages. In this simple example, the only usage is the Withdraw method above, but this will actually work on much more advanced examples. Accepting the action with Enter removes the parameter and also changes the code in Withdraw method:

image_thumb_6

ReSharper also checks your import statements to see if you are importing namespaces that you are not using and suggests removing them like so:

image_thumb_13

Later in the series, we will look at some of ReSharper’s options for choosing namespaces to always import some often used namespaces and ignoring them when scanning for use.

Importing Namespaces

When using types in namespaces that you have not imported, ReSharper will pop up a blue square to suggest an import like so:

image_thumb_7

No more writing using statements by hand. Recently I’ve started using ReSharper’s auto-completion (will be visited one of the next parts) for this, but it’s still a very useful feature, again using Alt+Enter.

Hints and Suggestions

ReSharper also supports hints and suggestions, both are actually suggestions for changing something in your code, but hints are less obtrusive and won’t show up when navigating between suggestions and not show up on the scrollbar in the code editor.

image_thumb_8

If we look at this bit of code, we notice that the List<string> has a solid green underline – this is a hint. The new keyword has a green squiggly line, which denotes a suggestion. Looking at the scrollbar to the right in our code editor, there’s a small green line – this is ReSharper telling us that there is a suggestion at this point in our code. Hovering the mouse over the line tells us what the suggestion is. Warnings and Errors will show up as yellow and red lines. The hint is not shown as mentioned.

image_thumb_14

Let us deal with the hint first. This hint is actually for us to use the C# 3.0 var keyword instead of our List<string>.

image_thumb_9

I usually don’t like this hint so much, since I feel it sometimes reduces the readability of my code if I overuse the var keyword – luckily there is an option in the context menu to change the inspection options for the particular hint. Pressing Enter brings up the following dialog:

image_thumb_10

Here I can set the severity of not using the var keyword when possible. Since I don’t like it – I choose “Do not show” and press OK. Now the solid green line is gone. Proceeding to press Alt+Enter on the suggestion asks me if I want to use a collection initializer instead of calling the add method on the next line:

image_thumb_11

Accepting this removed the call to Add and uses the collection initializer – as expected.

image_thumb_12

Context Is King

This post shows some examples of how ReSharper uses the context of your code to suggest “intelligent” options. All examples in this post were resolved pressing Alt+Enter. Using this single key combination can save you a lot of writing and often even suggest things that you didn’t think of. But remember to consider what it’s suggestion (like the var keyword) instead of just doing everything blindly.