home / skills / pluginagentmarketplace / custom-plugin-software-design / refactoring

refactoring skill

/skills/refactoring

This skill detects code smells and guides safe refactoring to improve maintainability and readability across Python projects.

npx playbooks add skill pluginagentmarketplace/custom-plugin-software-design --skill refactoring

Review the files below or copy the command above to add this skill to your agents.

Files (6)
SKILL.md
4.1 KB
---
name: refactoring
description: Detect code smells and apply safe refactoring techniques
sasmp_version: "1.3.0"
bonded_agent: 04-refactoring
bond_type: PRIMARY_BOND
version: "2.0.0"
updated: "2025-12-30"
---

# Refactoring Skill

> Atomic skill for code smell detection and safe refactoring

## Skill Definition

```yaml
skill_id: refactoring
responsibility: Single - Code smell detection and refactoring guidance
atomic: true
idempotent: true
```

## Parameter Schema

```typescript
interface SkillParams {
  // Required
  action: 'detect' | 'plan' | 'apply' | 'validate';
  code: string;
  language: string;

  // Optional
  smell_categories?: SmellCategory[];
  risk_tolerance?: 'low' | 'medium' | 'high';
  test_coverage?: 'none' | 'partial' | 'full';
}

type SmellCategory = 'bloaters' | 'oo_abusers' | 'change_preventers' | 'dispensables' | 'couplers';

interface SkillResult {
  smells?: CodeSmell[];
  plan?: RefactoringPlan;
  refactored_code?: string;
  validation?: ValidationResult;
}
```

## Validation Rules

```yaml
input_validation:
  action:
    required: true
    enum: [detect, plan, apply, validate]

  code:
    required: true
    min_length: 10
    max_length: 100000

  language:
    required: true
    allowed: [typescript, javascript, python, java, csharp, go]
```

## Retry Logic

```yaml
retry_config:
  max_attempts: 3
  backoff:
    type: exponential
    initial_delay_ms: 1000
    max_delay_ms: 10000
    multiplier: 2

  retryable_errors:
    - TIMEOUT
    - RATE_LIMIT

  non_retryable_errors:
    - INVALID_CODE
    - UNSAFE_REFACTORING
```

## Code Smell Catalog

### Bloaters

```yaml
long_method:
  threshold: 20 lines
  severity_scale:
    20-30: low
    30-50: medium
    50+: high
  refactorings:
    - Extract Method
    - Replace Temp with Query

large_class:
  threshold: 200 lines
  refactorings:
    - Extract Class
    - Extract Subclass

long_parameter_list:
  threshold: 3 params
  refactorings:
    - Introduce Parameter Object
    - Preserve Whole Object
```

### Couplers

```yaml
feature_envy:
  description: "Method uses another class's data more than its own"
  refactorings:
    - Move Method
    - Extract Method

message_chains:
  pattern: "a.b().c().d().e()"
  threshold: 3 calls
  refactorings:
    - Hide Delegate
```

## Refactoring Techniques

```yaml
extract_method:
  safety: high
  steps:
    1. Create new method with descriptive name
    2. Copy extracted code
    3. Replace original with call
    4. Run tests

move_method:
  safety: medium
  steps:
    1. Copy method to target
    2. Adjust references
    3. Update callers
    4. Remove original
    5. Run tests
```

## Usage Examples

### Smell Detection
```typescript
// Input
{
  action: "detect",
  code: `
    class OrderProcessor {
      process(order, user, config, logger, db, cache, validator) {
        // 150 lines of code
      }
    }
  `,
  language: "typescript"
}

// Output
{
  smells: [
    {
      name: "Long Parameter List",
      category: "bloaters",
      severity: "high",
      location: "OrderProcessor.process"
    },
    {
      name: "Long Method",
      category: "bloaters",
      severity: "high"
    }
  ]
}
```

## Unit Test Template

```typescript
describe('RefactoringSkill', () => {
  describe('detect', () => {
    it('should detect Long Method smell', async () => {
      const code = generateLongMethod(60);
      const result = await skill.execute({
        action: 'detect',
        code,
        language: 'typescript'
      });

      expect(result.smells).toContainEqual(
        expect.objectContaining({ name: 'Long Method' })
      );
    });
  });
});
```

## Error Handling

```yaml
errors:
  INVALID_CODE:
    code: 400
    message: "Code cannot be parsed"
    recovery: "Fix syntax errors"

  UNSAFE_REFACTORING:
    code: 400
    message: "Refactoring too risky without tests"
    recovery: "Add tests or increase risk_tolerance"
```

## Integration

```yaml
requires:
  - ast_parser
  - smell_detector

emits:
  - smells_detected
  - refactoring_planned
  - refactoring_applied

consumed_by:
  - 04-refactoring (bonded agent)
  - 01-design-principles (for principle violations)
```

Overview

This skill detects common code smells and generates safe, actionable refactoring guidance for Python and other supported languages. It identifies problematic patterns, proposes prioritized refactoring plans, and can apply or validate refactorings when tests or risk parameters permit. The goal is to reduce technical debt while preserving behavior.

How this skill works

The skill analyzes source code with an AST-based smell detector to find categories like bloaters, couplers, dispensables, and change-preventers. For a requested action (detect, plan, apply, validate) it returns detected smells, a refactoring plan with recommended techniques (e.g., Extract Method, Move Method), refactored code when safe, and validation results against tests or heuristics. It respects configurable risk tolerance and test coverage to avoid unsafe changes.

When to use it

  • Before major feature work to reduce the chance of regressions caused by complex code
  • During code review to provide automated, prioritized refactoring suggestions
  • When preparing legacy code for extension or integration
  • To generate safe, test-driven refactor plans for long methods or large classes
  • When validating that an automated refactor preserved behavior with available tests

Best practices

  • Provide at least partial test coverage before applying risky refactors; prefer low/medium risk actions when tests are absent
  • Limit refactoring scope per run (single method or class) to keep changes reviewable and reversible
  • Use the plan action to preview steps and filenames before applying code modifications
  • Configure smell categories to focus on the most relevant issues for the codebase (e.g., couplers vs bloaters)
  • Run validation after apply to confirm behavior unchanged and update tests if needed

Example use cases

  • Detect and report Long Method and Long Parameter List in a legacy Python module
  • Generate a step-by-step Extract Method plan for a 60-line function, including test checkpoints
  • Apply safe refactorings for low-risk transformations when full test coverage exists
  • Validate that recent refactors did not introduce behavioral changes using provided tests
  • Prioritize refactoring work by severity and risk tolerance for sprint planning

FAQ

What languages are supported?

The skill supports Python, TypeScript, JavaScript, Java, C#, and Go as input languages.

What does 'unsafe refactoring' mean?

An unsafe refactoring is one that cannot be verified by tests or heuristics and could change behavior; the skill will refuse or flag such changes unless risk_tolerance is increased or tests are added.