dotnet / dotnet/roslynator

Add Extract Parameter Object refactoring

Open
#552 0 comments 0 reactions 0 assignees View on GitHub
Area-Refactorings Feature Request
Dominant language
C#
Stars
3.5k
Forks
294
Avg merge
2h 30m
Merged PRs (30d)
4

Description

The [Parameter Object refactoring](https://refactoring.com/catalog/introduceParameterObject.html) is a common refactoring, described by Martin Fowler in his book *Refactoring, Improving the Design of Existing Code*. It would be a welcome addition to Roslynator to support this refactoring.

It would imagine the refactoring to possibly be applied on:

* All parameters of a method (in case all method parameters are normal in parameters)
* One a selection of parameters (possibly one or multiple) of a method

This refactoring would then do the following:

``` c#
// source (refactoring on all parameters)
public void Caller()
{
this.Callee(3, 4.0, new object());
}

public string Callee(int a, double b, object c) => $"A: {a}, B: {b}, C: {c}";

// result
public class
{
public int A { get; set; }
public double B { get; set; }
public object C { get; set; }
}

public void Caller()
{
this.Callee(new { A = 3, B = 4.0, C = new object() });
}

public string SomeMethod( arg) => $"A: {arg.a}, B: {arg.b}, C: {arg.c}";
```

The refactoring not only extract the parameters into a new class, it will also ensure that references to the argument from within the method will be changed, to keep the code working. It also ensures that callers create the Parameter Object on call.

Note that ideally, the following things should be configurable:

* The name of the created Parameter Object
* The location of the created Parameter Object (nested, same file, new file)
* The name of the replacement parameter (`arg` in the example)

Additional:

* Allow the refactoring to create an immutable object (by default or configurable?)
* In case a (immutable) Parameter Object is created, precondition checks could be extracted to the Parameter Object's constructor.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.