SuperAPI AI Directory

csharp-refactor

Coding & development

View repository

Updated 10-15 19:47

About this skill

C# Refactoring Skill

Systematically refactor C# code for maintainability, readability, and adherence to best practices.

Important: All results must be written in Korean. Keep code identifiers, technical terms, pattern names, etc. as-is in the original text, but use Korean for descriptive parts such as explanations, issues, and improvement suggestions.

Arguments

  • $ARGUMENTS[0]: Target file path (optional, if unspecified, search for .cs files)
  • $ARGUMENTS[1]: Refactoring type (optional): solid, pattern, modern, extract, all
    • If unspecified: Scan all categories and apply only the items found

Execution Steps

Step 1: Identify Target

If no file is specified ($ARGUMENTS[0]is empty):

  • Search for recently modified .cs files
  • Ask the user to select a target file

Step 2: Code Analysis

Read the target code and analyze refactoring opportunities.

Check TargetFramework: .csprojRead <TargetFramework>to determine the C# version, and suggest only features available in that version.

Step 3: Apply Refactoring

$ARGUMENTS[1] Analyze the criteria or all categories:

Refactoring Categories

SOLID Refactoring (solid)

Violation Refactoring
SRP Extract class, separate responsibilities
OCP Introduce Strategy/Template Method pattern
LSP Modify inheritance hierarchy, use Composition
ISP Split interfaces into smaller units
DIP Extract interface, dependency injection

Pattern Introduction (pattern)

Code Smell Recommended Pattern
Complex object creation Builder, Factory Method
Multiple conditional statements by type Strategy, State
Global state access Singleton (carefully), DI
Complex subsystems Facade
Tree/composite structures Composite
Dynamic feature addition Decorator
Request handling chain Chain of Responsibility

Modern C# Syntax (modern)

Existing Syntax Modern Alternative Minimum Version
Constructor + field assignment Primary constructor C# 12
new List<T> { ... } Collection expressions [...] C# 12
Multiple null checks Pattern matching, ?., ?? C# 8
Verbose switch statement Switch expressions C# 8
Manual INPC implementation [ObservableProperty] (CommunityToolkit.Mvvm) -
Mutable properties (non-MVVM) required, init C# 11
class for simple data Record types C# 9
Traditional foreach LINQ (when appropriate) -

Extract Refactoring (extract)

  • Extract Method: Long method → small, focused methods
  • Extract Class: Large class → multiple cohesive classes
  • Extract Interface: Concrete dependency → interface abstraction
  • Extract Base Class: Duplicate code → shared base class
  • Extract Parameter Object: Multiple parameters → single object

Output Format

All content is written in Korean. Code identifiers and technical terms are kept in the original text.

# 리팩토링 결과

## 대상
- 파일: {파일 경로}
- 리팩토링 유형: {유형}
- TargetFramework: {버전}

## 적용된 변경

### {리팩토링 이름}
- 위치: `file.cs:line`
- 변경 전:
```csharp
// 기존 코드
  • After change:
// 리팩토링된 코드
  • Benefits: {description}

Summary

  • Total refactorings: {N}
  • Number of changed lines: {N}
  • Newly created files: {목록}

Additional recommendations

  • Additional applicable improvements
  • Related patterns to consider

## 에러 처리

| 상황 | 처리 |
|------|------|
| `.cs` 파일 없음 | "리팩토링 대상 .cs 파일이 없습니다" 메시지 출력 후 종료 |
| 대상 파일 미발견 | 사용자에게 파일 경로 재확인 요청 |
| TargetFramework < net8.0 | C# 12 전용 기능 제안 제외, 경고 노트 출력 |

## 가이드라인

- 기존 기능을 유지합니다 (동작 변경 없음)
- 대규모 재작성이 아닌 점진적 변경을 합니다
- 영리함보다 가독성을 우선합니다
- 팀의 패턴 친숙도를 고려합니다
- 로직이 명확하지 않은 경우에만 주석을 추가합니다
- 테스트 프로젝트가 존재하면 `dotnet test` 실행 후 회귀 없음을 확인합니다