Tools

find_refactoring

Identify refactoring opportunities with priority and effort estimates

The find_refactoring tool analyzes your codebase to identify areas that would benefit from refactoring. It detects code duplication, complexity hotspots, organization issues, and interface problems, then provides prioritized recommendations with effort estimates.

How It Works

Stellarion uses multiple analysis techniques: hash-based duplication detection (with intelligent comment filtering), ComplexityAnalyzer for real complexity metrics, file system analysis for organization issues, and AST parsing for interface analysis. Results are prioritized by impact and effort for actionable improvement planning.

When to Use

  • Planning refactoring work: Identify what to improve and prioritize
  • Identifying technical debt: Find issues before they become problems
  • Prioritizing improvements: Know where to get the best ROI
  • Code review preparation: Understand areas needing attention
  • Sprint planning: Estimate refactoring effort

Parameters

ParameterTypeRequiredDefaultDescription
pathstringNo.Directory to analyze
focusstringNoallall, duplication, complexity, organization, or interfaces
thresholdnumberNo10Complexity threshold for flagging
includeTestsbooleanNofalseInclude test files in analysis
includePrioritybooleanNofalseCalculate and sort by priority
includeEffortbooleanNofalseInclude effort estimates
outputFormatstringNojsonjson, markdown, or both
saveToDiskbooleanNofalseSave markdown report to docs/
maxOpportunitiesnumberNo100Maximum opportunities to return

MCP Command Syntax

mcp__stellarion__find_refactoring focus:complexity path:src/ includePriority:true includeEffort:true

Focus Areas

All (Comprehensive Analysis)

Natural Language:

Find all refactoring opportunities in this project

Direct MCP Call:

mcp__stellarion__find_refactoring focus:all includePriority:true includeEffort:true

Finds: Duplication, complexity, organization, and interface issues


Duplication

Find repeated code patterns that should be abstracted.

Natural Language:

Where is there code duplication in this project?

Direct MCP Call:

mcp__stellarion__find_refactoring focus:duplication path:src/

Finds:

  • Exact code duplicates across files
  • Similar code blocks (5+ lines)
  • Copy-paste patterns
  • Filters out comments and docstrings

Complexity

Find overly complex code that's hard to maintain.

Natural Language:

Find complex functions that need refactoring. Show priority and effort.

Direct MCP Call:

mcp__stellarion__find_refactoring focus:complexity threshold:15 includePriority:true includeEffort:true

Finds:

  • High cyclomatic complexity (> 20 = high priority)
  • High cognitive complexity (> 15 = needs attention)
  • Deep nesting levels
  • Long methods

Organization

Find structural issues in the codebase.

Natural Language:

Are there any files that are too large or directories too crowded?

Direct MCP Call:

mcp__stellarion__find_refactoring focus:organization

Finds:

  • Large files (> 500 lines)
  • Crowded directories (> 20 files)
  • Poor module organization
  • Files that do too many things

Interfaces

Find interface and signature problems.

Natural Language:

Find functions with too many parameters

Direct MCP Call:

mcp__stellarion__find_refactoring focus:interfaces

Finds:

  • Functions with > 5 parameters
  • Unclear function signatures
  • Missing type information
  • Overly complex APIs

Examples

Prioritized Complexity Issues

Natural Language:

Find the most important complexity issues to fix, sorted by priority

Direct MCP Call:

mcp__stellarion__find_refactoring focus:complexity threshold:15 includePriority:true includeEffort:true outputFormat:markdown

Save Report to Docs

Natural Language:

Generate a refactoring report and save it to the docs folder

Direct MCP Call:

mcp__stellarion__find_refactoring focus:all includePriority:true includeEffort:true outputFormat:markdown saveToDisk:true

Creates: docs/refactoring-report.md


Production Code Only

Natural Language:

Find refactoring opportunities in production code only, excluding tests

Direct MCP Call:

mcp__stellarion__find_refactoring focus:all includeTests:false includePriority:true

High Complexity Threshold

Natural Language:

Find only the most complex functions (complexity > 25)

Direct MCP Call:

mcp__stellarion__find_refactoring focus:complexity threshold:25 includePriority:true

Specific Directory

Natural Language:

Find duplication issues in the services directory

Direct MCP Call:

mcp__stellarion__find_refactoring focus:duplication path:src/services/

Output Format

Opportunity List

Each opportunity includes:

FieldDescription
DescriptionWhat the issue is
LocationFile path and line numbers
Typeduplication, complexity, organization, or interfaces
Severitycritical, high, medium, low
PriorityCalculated priority score (when enabled)
EffortEstimated fix time (when enabled)

Priority Calculation

Priority is calculated from:

FactorDescription
SeverityHow bad is the issue?
ImpactHow important is the affected code?
FrequencyHow often is this code changed?

Priority values:

  • critical: Complexity > 30, should fix immediately
  • high: Complexity > 20 or duplication > 5 occurrences
  • medium: Complexity > threshold, moderate issues
  • low: Organization issues, minor improvements

Effort Estimates

LevelTypical TimeDescription
low< 1 hourSimple fixes, single-file changes
medium1-4 hoursModerate refactoring, few files
medium-high4-8 hoursSignificant changes, testing needed
high1-3 daysMajor refactoring, multiple files

Summary Statistics

  • Total opportunities found
  • Breakdown by category (duplication, complexity, organization, interfaces)
  • Breakdown by severity (critical, high, medium, low)
  • Total estimated effort

Understanding Results

Duplication Results

FieldDescription
Code blockThe duplicated code
OccurrencesNumber of times it appears
FilesList of files containing the duplicate
LinesLine numbers in each file

Complexity Results

FieldDescription
Function nameThe complex function
CyclomaticNumber of decision paths
CognitiveUnderstanding difficulty
Suggested actionHow to simplify

Organization Results

FieldDescription
File/directoryThe problematic location
IssueToo large, too crowded, etc.
MetricCurrent size/count
ThresholdExpected maximum

Interface Results

FieldDescription
FunctionThe problematic function
Parameter countNumber of parameters
SuggestionHow to improve

Tips for Effective Refactoring

  1. Start with high-priority, low-effort items: Best ROI for improvement
  2. Use includeEffort: true for planning: Helps with sprint planning
  3. Exclude tests when focusing on production code: includeTests: false
  4. Generate reports regularly: Track progress over time
  5. Use focus areas for targeted work: Don't try to fix everything at once

Combining with Other Tools

  1. Find + Impact: Check impact of refactoring candidates before changing
  2. Find + Quality: Use quality metrics to validate improvements after refactoring
  3. Find + Report: Generate reports to track refactoring progress