Automated refactoring to add parameter names to method calls
Asked Answered
P

2

26

I am in the middle of a big refactoring.

I have dozens of methods, which are called via positional parameters. Now I would like to have them called via named parameters. The methods exist in several, non-inherited classes and have the same name and their signatures differ. Example:

Definitions

public class Foo
{
    public static Foo Create(int count, string name)
    {
        ...
    }
}

public class Bar
{
    public static Bar Create(string description, bool yesNo, float factor)
    {
        ...
    }
}

And the following calls I would like to replace, from

public void CreateSomeObjects()
{
    var foo = Foo.Create(123, "foo");
    var bar = Bar.Create("bar", true, 1.23);
}

to

public void CreateSomeObjects()
{
    var foo = Foo.Create(count: 123, name: "foo");
    var bar = Bar.Create(description: "bar", yesNo: true, factor: 1.23);
}

I use Visual Studio Premium 2013 and Resharper. Any ideas how to achieve this? (I just need a hint, no complete solution.)

Pentose answered 4/9, 2014 at 7:7 Comment(4)
Unfortunately not. My current approach is to rewrite this with Roslyn. As I am just at the beginning, I won't be posting code for now.Pentose
CodeRush seems to be able to insert named parameters (it's a context menu that you manually need to click, so no automated solution).Stamin
R# 9 (possibly earlier too) has a context action to introduce parameter names, however it must be invoked per call site. Did you want to do this for all call sites for a given method?Taxonomy
I have R#9 but I don't see this option. Where do I find it? And yes, I wanted to convert all call sites.Pentose
P
16

Not sure how practical this is but you can do the following using ReSharper:

  1. Use "Find Usages" to get a list of all method call locations.
  2. For each usage, double-click to go to the method.
  3. Then in the code editor, click on a parameter value and ReSharper should show its action indicator (a light bulb or hammer in the left margin).
  4. Click the action indicator to show the action context menu.
  5. Select the "Add argument name" action to add parameter names to all parameters.
  6. Repeat.
Pugliese answered 25/9, 2015 at 13:37 Comment(2)
This is a lot easier than it sounds. I just turned up the level on named arguments to warning, then on the first argument of each function, hit ctl+., enter, and it's done.Cimabue
Note that the "Add argument name" action seems to be only available for constant argument values (null or constant value). You can then select to add names to all arguments of the invocation.Overblouse
C
11

Years after this question was asked, I found it while Googling (as I'm in the same situation).

I don't have access to ReSharper, but for me, the excellent Roslynator was the solution.

You can install it through the VS2017/2019 Extensions and Updates dialog (search for "Roslynator"), or through the VS Marketplace. There is also a version which only contains the refactorings, if for some reason you don't want the analysers installed.

Once you've installed the extension and restarted Visual Studio, you can right-click the first parameter of any method and select Quick Actions and Refactorings..., then click Add argument name 'XXX' (including trailing arguments) to add in the names (see screen grabs below).

Quick Actions and Refactorings

Add argument name

It's not completely automated (although being Roslyn-based I presume it could be), but I've already saved a lot of time with it.

Crinkle answered 23/9, 2019 at 5:50 Comment(2)
It doesn't seem to show up for constructors? But I might just keep it installed anyway. Edit: the first parameter was a string. Can't be clicked in the string. Click outside the double quotes. Works great, thanks.Crumpet
Roslynator is wonderful. You select all the arguments on a method call, go to Quick Actions (Alt+Enter) and select "Add argument name".Clercq

© 2022 - 2024 — McMap. All rights reserved.