ReSharper, day 23: Introduce Variable (and, introducing Ctrl+Shift+R to refactor)

Welcome to day 23 of my 31 Days of ReSharper.

Today, and for the rest of the 31 days, I’ll be talking about ReSharper’s support for refactoring, which is much improved over the handful of refactorings that ship with Visual Studio 2005.

Refactor This with Ctrl+Shift+R

ReSharper has a targeted context menu for refactoring. It contains only the refactorings that make sense at the current cursor position, or based on the current selection. You invoke it with Ctrl+Shift+R.

Here’s an example of what it looks like:

ReSharper's Refactor This context menu, showing refactorings that are available in the current context

This menu contains other refactoring commands, and some of them have their own keystrokes defined. (They’re full-fledged Visual Studio commands, so you can add or change the keymappings.) You may learn some of those other keystrokes, if there are refactorings that you use often. But I really like the low cost of entry. All you need to remember is Ctrl+Shift+R.

Introduce Variable

Introduce Variable creates a new local variable, and initializes it to the selected expression. Then it replaces the selected expression with a reference to the new variable.

ReSharper's Introduce Variable refactoring dialog, accessed through Ctrl+Shift+R

Here’s a rundown of the options:

  • Variable type. I don’t think I’ve ever seen this combo box enabled; it’s always locked in on the exact type of the expression. If you want to use a base type instead, you’ll have to introduce the variable first, and then change the type.

  • Variable name. ReSharper’s variable name suggestions kick in here, of course. One suggestion is filled in, and you can see the others by dropping down the combo box.

  • Replace all occurrences of expression. Since you’re introducing a local variable, this only replaces occurrences within the current method. If the same expression shows up more than once, then you probably want to check this option most of the time; but it can change your code’s behavior, through side effects and aliasing and whatnot, which is probably why this option isn’t checked by default.

  • Introduce constant. This could have been a separate refactoring, but they rolled it into Introduce Variable. This checkbox is only enabled if the expression can be evaluated at compile time.

Ditching the blues

When you bring up this dialog, if the expression shows up more than once in your method, ReSharper will highlight all the usages in blue. It will also show blue tick marks in the color bar.

A ReSharper 'feature' leaves expressions highlighted in blue even after you cancel out of the Introduce Variable dialog

If you Cancel out of the Introduce Variable dialog, the blue highlight doesn’t go away, and neither do the blue stripes in the color bar. I reported this as a bug, and they said nope, not a bug: this is on purpose, so you can look at all the places that expression occurred. This is the first time I’ve ever heard of using a Cancel button to find usages. (Guys, maybe there should be another button besides OK and Cancel? Maybe a “Show Usages” button?)

Update: I didn’t realize it when I first wrote this, but the blue highlight also sticks around after you click OK. So no matter what you do, if you bring up this dialog, you need to remember to press Esc after you close it. One extra, needless step, every single time. Isn’t ReSharper supposed to be there to make things easier, and make common tasks take less time? Grumble.

So anyway. It took me a couple of months, but I eventually found an easy way to dismiss all the blue: just press Esc. This is a good thing to know, because otherwise, it’s pretty stubborn: until you press Esc, that highlight is going to stick around until you either close the file or delete the code.

Note: Tomorrow’s article isn’t quite done yet, and I’ll be out of town this weekend. So tomorrow’s article will get posted on Sunday.

Leave a Reply

Your email address will not be published. Required fields are marked *