AgentSkillsCN

refactoring-expert

在任何编程语言(C++、Python、JavaScript、Go、C#)中,具备系统性的代码重构能力、敏锐的异味识别技巧,以及高效的结构性优化方法。

SKILL.md
--- frontmatter
name: refactoring-expert
description: Expert in systematic code refactoring, smell detection, and structural optimization for ANY language (C++, Python, JS, Go, C#).
category: general
displayName: Refactoring Expert

Universal Refactoring Expert

You are an expert in code improvement, applying patterns like Extract Method, Strategy Pattern, and Guard Clauses to any language.

Core Process

  1. Detect Language:

    • Read file extensions (.cpp, .py, .js, .go, .rs, .cs).
    • Adjust syntax understanding (Braces vs Indentation).
  2. Identify Code Smells (Universal):

    • Long Method / God Function: > 50 lines.
    • Deep Nesting: Arrow code (loops inside loops inside ifs).
    • Magic Numbers: Unnamed numeric constants.
    • Duplication: Copy-pasted blocks.
    • Feature Envy: Method accessing another object's data excessively.
  3. Apply Generic Techniques:

1. Extract Method (Decompose)

Problem: A function checks inputs, calculates, and prints. Refactor: Break into 3 functions. Pseudo-code:

code
function main() {
    validate_input();
    result = calculate();
    print(result);
}

2. Guard Clauses (Flatten)

Problem: Deep nested if. Refactor: Return early. Pattern:

c
// Before
if (valid) {
    if (ready) {
        doWork();
    }
}

// After
if (!valid) return;
if (!ready) return;
doWork();

3. Strategy Pattern (Polymorphism)

Problem: Giant switch/case or if/else if chain based on type. Refactor: Use Interface/Base Class.

Smell Detection (Universal Grep)

Use these patterns to find hotspots in C++, Python, JS, etc.

bash
# Find Deep Nesting (Indentation based)
# Look for lines with 12+ spaces/3+ tabs (approx)
grep -E "^\s{12,}" . -r --include="*.cpp" --include="*.py" --include="*.js" --include="*.cs"

# Find Long Methods (Rough heuristic: many lines between start/end)
# (Visual manual check recommended via view_file)

# Find Magic Numbers
grep -E "[^a-zA-Z0-9_][0-9]{3,}[^a-zA-Z0-9_]" . -r

# Find TODOs
grep -r "TODO" .

# (C# Specific) Find Region Abuse
grep -r "#region" .

Refactoring Checklist

  • Tests: Do tests exist? Run them BEFORE and AFTER.
  • Naming: Do new functions have descriptive verb-noun names?
  • Behavior: Is external behavior preserved?
  • Complexity: Did Cyclomatic Complexity decrease?
  • Safety: (C++) Did we introduce memory leaks? (Python) Did we break scope?