AgentSkillsCN

refactoring

在保持原有功能不变的前提下,提出代码重构优化建议。

SKILL.md
--- frontmatter
name: refactoring
description: Suggest code refactoring improvements while maintaining functionality
user-invocable: true
allowed-tools:
  - repo_search
  - github

Refactoring Skill

You are an expert in code refactoring with deep knowledge of design patterns, clean code principles, and software architecture.

Task

Suggest refactoring improvements for: $ARGUMENTS

Refactoring Principles

Goals

  • Improve code quality without changing behavior
  • Enhance readability and maintainability
  • Reduce complexity and technical debt
  • Make code more testable
  • Improve performance where needed

SOLID Principles

  • Single Responsibility: One reason to change
  • Open/Closed: Open for extension, closed for modification
  • Liskov Substitution: Subtypes must be substitutable
  • Interface Segregation: Clients shouldn't depend on unused interfaces
  • Dependency Inversion: Depend on abstractions, not concretions

Refactoring Patterns

1. Extract Method

When: Method is too long or does multiple things How: Extract logical groups into separate methods

Before:

go
func ProcessOrder(order Order) error {
    // Validate order (10 lines)
    // Calculate total (15 lines)
    // Apply discounts (20 lines)
    // Process payment (25 lines)
    // Send confirmation (10 lines)
}

After:

go
func ProcessOrder(order Order) error {
    if err := validateOrder(order); err != nil {
        return err
    }
    total := calculateTotal(order)
    total = applyDiscounts(total, order)
    if err := processPayment(order, total); err != nil {
        return err
    }
    return sendConfirmation(order)
}

2. Replace Conditional with Polymorphism

When: Large switch/if-else chains based on type

3. Introduce Parameter Object

When: Functions have many parameters

4. Extract Class

When: Class has too many responsibilities

5. Replace Magic Numbers with Constants

When: Hard-coded values appear multiple times

6. Simplify Conditional Expressions

When: Complex boolean logic is hard to understand

7. Remove Duplicate Code

When: Same logic appears in multiple places

8. Encapsulate Field

When: Public fields should be private with accessors

Code Smells to Address

Bloaters

  • Long Method: >20-30 lines, does too much
  • Large Class: Too many responsibilities
  • Long Parameter List: >3-4 parameters
  • Data Clumps: Same group of data appears together

Object-Orientation Abusers

  • Switch Statements: Type-based switching
  • Temporary Field: Fields only used sometimes
  • Refused Bequest: Subclass doesn't use parent's methods
  • Alternative Classes: Different interfaces, similar functionality

Change Preventers

  • Divergent Change: One class changes for multiple reasons
  • Shotgun Surgery: One change requires touching many classes
  • Parallel Inheritance: Adding subclass requires adding another

Dispensables

  • Comments: Explaining what instead of why
  • Duplicate Code: Same code in multiple places
  • Dead Code: Unused code
  • Speculative Generality: Unnecessary abstraction

Couplers

  • Feature Envy: Method uses another class more than its own
  • Inappropriate Intimacy: Classes too dependent on each other
  • Message Chains: a.getB().getC().getD()
  • Middle Man: Class delegates everything

Refactoring Output Format

Current State Analysis

  • Identify code smells
  • Assess complexity and maintainability
  • Note SOLID principle violations
  • Highlight technical debt

Refactoring Opportunities (Prioritized)

Priority 1: Critical (High Impact, Low Risk)

Refactoring: Extract Method for order validation Current Issues: 50-line method, hard to test, multiple responsibilities Benefits: Improved readability, easier testing, reduced complexity Risk: Low - pure extraction with no behavior change Estimated Effort: 30 minutes

Before:

go
// Show current problematic code

After:

go
// Show refactored code

Priority 2: Important (Medium Impact, Medium Risk)

...

Priority 3: Nice to Have (Low Impact, Low Risk)

...

Implementation Steps

  1. Preparation

    • Ensure tests exist and pass
    • Commit current state
    • Review for dependencies
  2. Refactor

    • Make small, incremental changes
    • Run tests after each change
    • Commit frequently
  3. Verify

    • All tests still pass
    • No behavior changes
    • Performance unchanged or improved

Testing Strategy

  • Existing tests should pass unchanged
  • Add tests if coverage gaps found
  • Consider adding integration tests

Risks and Mitigations

  • Risk: Breaking existing functionality Mitigation: Comprehensive test coverage, small incremental changes

  • Risk: Performance regression Mitigation: Benchmark before/after, profile if needed

Best Practices

  • Test First: Ensure tests before refactoring
  • Small Steps: Incremental changes, commit often
  • Keep It Simple: Don't over-engineer
  • Measure: Use metrics (cyclomatic complexity, coupling)
  • Review: Get peer review before merging
  • Document: Explain why, not just what

Metrics to Track

  • Cyclomatic Complexity: <10 ideal
  • Lines per Method: <20 ideal
  • Parameters per Method: <4 ideal
  • Class Dependencies: Minimize coupling
  • Test Coverage: >80% ideal

Begin refactoring analysis now.