ReSharper, day 26: Change Signature

Welcome to day 26 of my 31 Days of ReSharper.

Ever had three functions that all take the same parameters, but each one in a different order? If so, Change Signature is the feature for you.

ReSharper's Change Signature dialog box

ReSharper’s Change Signature dialog lets you change anything and everything about a method signature. There are other ReSharper features for changing a method signature in specific ways, such as Introduce Parameter. But this is where you go for stuff there isn’t a shortcut for. Here, you can:

  • Rename the method. If you select Delegate via overloading method, it will even put in a method with the old name, that does nothing but call the new one. In case that’s ever useful.

  • Change the return type.

  • Add a parameter. You must specify a default value, which is what will be passed at all the call sites. But sometimes you don’t want to pass a constant value every time, in which case, see the next section.

  • Remove a parameter. ReSharper will not check to make sure the parameter is unused, so this may result in non-compilable code. (They do have a feature for safely deleting parameters, among other things, which I’m saving to cover on Day 31.)

  • Rename a parameter. Alas, ReSharper does not provide variable-name suggestions here, even if you press Ctrl+Space.

  • Reorder parameters.

In all of these cases, ReSharper takes care of updating all the call sites.

Non-constant values for new parameters

When you add a new parameter, ReSharper wants to keep your code compilable, so it needs to modify all the call sites that already call this method. That means it needs something to pass. This is a required field; you have to specify something to pass from all the call sites.

So what happens if you don’t want to pass the exact same thing from every call site? You have a few options:

  • Don’t use Change Signature; manually add the parameter instead. Then the compiler will take you to all of the call sites — probably. (I’ve been bitten by this one when there turned out to be other overloads.) Not recommended in any but the simplest of cases; that’s why we have tools to do this stuff for us.

  • Use a default value, and remember to go back and change it. Also not recommended. Human memory is not that good. That’s why you’re using ReSharper in the first place. You should only use a default value like null if it really is the most reasonable thing to pass.

  • Use a nonsense expression, like asdfjkl, that won’t exist at any of the call sites. ReSharper will let you do this, but (no surprise) you’ll end up with non-compilable code. This is actually good, in this case: it guarantees that the compiler will take you to every call site, where you can make a case-by-case determination of what to pass in.

  • Use an expression that you hope will work most places. If you’re passing a Foo object, you can just type Foo as the default value, in the hopes that most of the call sites will be on classes that already have a Foo property. We’ve often had decent luck with this one.

  • Use Introduce Parameter instead. If you can use Introduce Parameter, it’s better than Change Signature, because Introduce Parameter will do the right thing. But it only works if you can express the new parameter in terms of things the method knows about.

Leave a Reply

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