dotnet / dotnet/roslyn

Extract New Class Refactoring Request

Open
#73,178 0 comments 0 reactions 0 assignees View on GitHub
Area-IDE
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Brief description:**
Sometimes code has properties like this:
```
public class Foo {
public string M1_Value1 { get; set; }
public string M1_Value2 { get; set; }
public string M1_Value3 { get; set; }
public DateTime M2_Value1 { get; set; }
public DateTime M2_Value2 { get; set; }
public DateTime M2_Value3 { get; set; }
}
```

Which should, ideally, be code like this:
```
public class Foo {
public M1 M1s { get; set; } = new();
public M2 M2s { get; set; } = new();
}

public class M1 {
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
}

public class M2 {
public DateTime Value1 { get; set; }
public DateTime Value2 { get; set; }
public DateTime Value3 { get; set; }
}

```

A refactoring should exist that enables this kind of conversion.

**Languages applicable:**
Both, but I only care about C#

**Code example that the analyzer should report:**
See above

**Additional information:**
This is identical to the "Extract Base Class" refactoring except the members should be wrapped into a child class instead of a base class.

It is OK if it simply moved the members and a subsequent refactoring would be used to rename them.
For example, after the first refactoring, you might have:
```

public class Foo {
public M1 M1s { get; set; } = new();
public DateTime M2_Value1 { get; set; }
public DateTime M2_Value2 { get; set; }
public DateTime M2_Value3 { get; set; }
}

public class M1 {
public string M1_Value1 { get; set; }
public string M1_Value2 { get; set; }
public string M1_Value3 { get; set; }
}

```

**Documentation requirements:**
When this analyzer is implemented, it must be documented by following the steps at [Documentation for IDE CodeStyle analyzers](https://github.com/dotnet/roslyn/blob/main/docs/contributing/Documentation%20for%20IDE%20CodeStyle%20analyzers.md).

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.