ReSharper, day 24: Introduce Field

Welcome to day 24 of my 31 Days of ReSharper.

Yesterday I talked about ReSharper’s Introduce Variable refactoring. Today’s topic is Introduce Field.

Adding a field is a lot like adding a variable, but there are extra options:

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

It does the usual variable name suggestions (which follow the naming conventions you specify in ReSharper Options).

Like Introduce Local Variable, it has a Replace all occurrences of expression checkbox. But because Introduce Field affects an entire class, the blue highlights, and the blue stripes in the color bar, become more useful in seeing how many places the expression will be replaced (although the fact that they won’t go away is still annoying).

Introduce Field also has that goofy behavior where they use Cancel to mean Highlight All, instead of using it to mean Cancel. If you want the thing that’s really Cancel, you need to click Cancel and then press Esc. (And if you want the thing that’s really OK, you need to click OK and then press Esc. Eww.)

Not much more to be said about the basic settings. The options speak for themselves fairly well.

Trying to write tests

If you write unit tests, you’ll notice that none of the “Initialize in” options are much good in a test-fixture class. “Current member” might be useful on occasion, but it’s not usually what you want. (And “Constructor” and “Field declaration” shouldn’t even be available if you’re working in a [TestFixture] class, IMHO. They’re just begging for inter-test dependencies.)

I sent in a feature request to add “Initialize in [SetUp] method” to ReSharper, but they closed it as “Won’t Fix”, claiming that it could be implemented as a plug-in. If that’s true, they have got a long way to go on the documentation for the ReSharper plug-in API, because they don’t have any refactoring samples at all. It doesn’t even look like they’re planning to write any; they don’t have a section for them. So if anyone wants to write a ReSharper plug-in that adds a radio-button choice to an existing dialog box, and uses that new choice to change the behavior of an existing refactoring, I’ll just wish you the best of luck right now.

It’s not the end of the world; you can still do an Introduce Field properly. It’s just not as nice as most things in ReSharper. You have to manually scroll to the SetUp method, and stuff like that. (grin)

If you want to use Introduce Field in a test fixture, here’s how to make it work, until and unless they get around to writing those plug-in docs:

  1. Use Introduce Field, and select “Current member”.

  2. Cut the assignment that ReSharper just generated, and paste it into your [SetUp] method.

Not that hard, really. Just irritating. They’ve spoiled me by making everything so easy, that it’s frustrating when they take something I do all the time and flat-out refuse to make it as easy as it should be.

Leave a Reply

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