I propose a new refactoring feature for Visual Studio aimed at seamlessly relocating methods along with any associated code to either a new or existing class. Allow me to illustrate the scenario:
Currently, I have a class dedicated to benchmarking code. However, this class has grown so extensive that it takes BenchmarkDotNet over five hours to generate the reports for 672 tests. My proposed solution involves moving one or more of these benchmarking methods to a new benchmarking class. Yet, simply relocating the method isn’t sufficient; I also require all related methods and fields to be transferred to the new class. This automation would significantly alleviate manual efforts and streamline the refactoring process. While I’ve long desired this functionality, the urgency to address this issue is now paramount.
To provide a concrete example, consider the benchmark method below that I aim to relocate:
[Benchmark(Description = "foreach(): MemoryExtensions.AsSpan List<ref>")]
public void ForEach_MemoryExtension_AsSpan_Ref()
{
var collection = MemoryExtensions.AsSpan(this._personRefList.ToArray());
foreach (var person in collection)
{
base.Update(person);
}
}
The method in question utilizes the _personRefList field, which must also be moved along with it. Moreover, it’s worth noting that the collection within this field is instantiated within the method itself. Thus, it’s imperative that both the method and the field, along with its instantiation, are relocated together.
public override void Setup()
{
base.Setup();
this._personRecordList = [.. this.GetPersonRecordCollection()];
this._personRefList = [.. this.GetPersonRefCollection()];
this._personValList = [.. this.GetPersonValCollection()];
}
In addition to moving the specified method and field, it’s essential to relocate or create the Setup() method in the new class. This includes transferring the call to base.Setup() and the line responsible for populating the _personRefList field with data. The new Setup() method should look like this:
public override void Setup()
{
base.Setup();
this._personRefList = [.. this.GetPersonRefCollection()];
}
Considering that this benchmark class inherits LargeCollectionBenchmark, the new class should inherit it as well, along with any necessary using statements.
Furthermore, the capability to select multiple methods for relocation should be incorporated. For instance, all three of these methods and their associated code should be movable:

Furthermore, if these three methods constitute the sole users of the fields mentioned earlier, any relevant fields or associated code within the original class should be removed. However, if these moved methods are referenced elsewhere within the solution, it’s imperative to refactor the respective code to reference the new class appropriately. This ensures seamless integration and maintains the integrity of the solution.
Implementing this refactoring feature would significantly expedite the process of relocating code to different classes, offering substantial benefits to software engineers. By automating the transfer of methods along with related fields and code, developers can streamline their workflow, reduce manual effort, and enhance productivity. This feature would undoubtedly find utility among a wide range of software engineers, helping them efficiently organize and maintain their codebases.
GitHub
It seems like the Visual Studio team has moved my request to GitHub. I hope this means they’ll address it sooner rather than later! Fingers crossed!
https://github.com/dotnet/roslyn/issues/72936
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.
