Welcome to day 27 of my 31 Days of ReSharper.
Today’s topic is Extract Method, arguably the second most useful refactoring of all time. (Rename, of course, is the first. I’ll cover it in a couple of days.)
Method name is pretty obvious.
Declare static is automatically grayed out if the expression uses any instance fields, methods, or properties. I’m not sure why this is. When you’re calling a method, it makes sense, but fields and properties could just get passed into the newly-extracted method, if ReSharper allowed it.
Return value seems to serve no purpose whatsoever. I’ve never seen it enabled. And it either says “No return value”, or it’s blank (even though ReSharper clearly does know the return type — it’s shown correctly in the “Signature preview” box). I don’t know if it’s just buggy, or if it was a deliberate design decision to put a blank combo box here.
Parameters. ReSharper decides which parameters you need, and fills them in. While the grid looks like the one in the Change Signature dialog, it’s nowhere near as full-featured: here all you can do is rename and reorder. You can’t change parameters’ types or anything like that.
ReSharper suggests names for the parameters it decides you need, but that’s a one-time thing when the dialog opens. You have no way of dropping down a list of other suggestions.
If you uncheck a parameter, it won’t be passed; instead ReSharper will generate a local variable and initialize it to three dots,
..., which guarantees that the compiler will gently suggest that you fill in something else.
Extracting with extra parameters
I’ve often wished that ReSharper’s (and Delphi’s, too, for that matter) Extract Method dialog had an option to add more parameters. But I was thinking about it this evening, and realized that — with ReSharper, at least — it’s unnecessary.
Suppose I’ve got some setup code for a test, and it does this:
_foo = new Foo(42);
And suppose I want to pull that line of code into a new method,
InitializeFoo(), that takes an
int parameter. What I wanted was for the Extract Method dialog to show me the code it was going to write, and for it to let me highlight the
42 and say “hey, tack on another parameter for this expression.”
Well, duh. They’ve already got a feature for doing exactly that! I have to actually hit OK to extract the method first, but then I can highlight the
42 in the code editor and do an Introduce Parameter. And it’ll automatically update the call site to pass in the literal
42, so everything just works.