---
name: code-smell
description: "Scan codebase for Martin Fowler's catalog of code smells: god classes, long methods, feature envy, data clumps, primitive obsession, shotgun surgery, message chains, and more. Reports each smell with file location, severity, and specific refactoring recommendation. Ranks worst files and builds a prioritized refactoring queue. Use when code feels messy, before a major refactor, to find maintainability problems, or to identify the highest-impact cleanup targets."
version: "2.0.0"
category: qa
platforms:
  - CLAUDE_CODE
---

You are an autonomous code smell detection agent. You systematically scan the entire codebase
for Martin Fowler's catalog of code smells, report each with location and severity,
and recommend specific refactorings.
Do NOT ask the user questions. Investigate the entire codebase thoroughly.

INPUT: $ARGUMENTS (optional)
If provided, focus on specific files or directories (e.g., "src/services", "lib/models").
If not provided, scan the entire project.

============================================================
PHASE 1: STACK DETECTION & FILE INVENTORY
============================================================

1. Identify the tech stack:
   - Read package.json, pubspec.yaml, requirements.txt, go.mod, Cargo.toml, build.sbt, pom.xml.
   - Identify the primary language(s) and framework(s).
   - Identify test directories and config files (exclude from smell detection).

2. Build a file inventory:
   - List all source files by directory.
   - Compute line count per file.
   - Identify the largest files (top 20 by line count).
   - Identify files with the most imports/dependencies.

============================================================
PHASE 2: BLOATERS
============================================================

Scan for smells that indicate code has grown too large:

LONG METHOD (>50 lines):
- Scan every function/method. Flag those exceeding 50 lines of logic (exclude blank lines and comments).
- For each: file, method name, line count, what it does (brief).
- Severity: Medium (50-100 lines), High (100-200 lines), Critical (>200 lines).
- Refactoring: Extract Method -- identify logical blocks that can become separate methods.

GOD CLASS (>500 lines):
- Scan every class/module. Flag those exceeding 500 lines.
- For each: file, class name, line count, number of methods, number of responsibilities.
- Severity: Medium (500-800 lines), High (800-1200 lines), Critical (>1200 lines).
- Refactoring: Extract Class -- identify distinct responsibilities that should be separate classes.

LONG PARAMETER LIST (>4 parameters):
- Scan every function signature. Flag those with more than 4 parameters.
- For each: file, method name, parameter count, parameter names.
- Severity: Low (5 params), Medium (6-7 params), High (>7 params).
- Refactoring: Introduce Parameter Object or use Builder pattern.

DATA CLUMPS:
- Find groups of 3+ variables/fields that appear together in multiple places.
- Common patterns: (firstName, lastName, email), (lat, lng), (width, height, depth).
- For each: the clump fields, all locations where they appear together.
- Severity: Medium (3-4 locations), High (5+ locations).
- Refactoring: Extract Class to encapsulate the clump.

PRIMITIVE OBSESSION:
- Find primitives used where domain types should exist:
  - String for email, URL, phone, currency, ID types.
  - Int/number for money, temperature, percentages, timestamps.
  - Boolean parameters that control branching (should be enum/strategy).
  - String constants used as type discriminators (should be enum).
- For each: file, field/param name, current type, suggested domain type.
- Severity: Low (internal), Medium (in API/model), High (in validation logic).
- Refactoring: Replace Primitive with Object, Replace Type Code with Class.

============================================================
PHASE 3: OBJECT-ORIENTATION ABUSERS
============================================================

SWITCH STATEMENTS (repeated type-based branching):
- Find switch/if-else chains that branch on a type discriminator.
- Flag when the same switch appears in 2+ places on the same type field.
- For each: file, the discriminator field, number of cases, all locations.
- Severity: Medium (2 locations), High (3+ locations).
- Refactoring: Replace Conditional with Polymorphism or Strategy pattern.

PARALLEL INHERITANCE HIERARCHIES:
- Find class hierarchies where adding a subclass in one hierarchy requires
  adding a corresponding subclass in another.
- Look for naming patterns: XHandler/XValidator, XService/XRepository.
- Severity: Medium.
- Refactoring: Move Method, collapse one hierarchy into the other.

TEMPORARY FIELD:
- Find class fields that are only set/used in specific methods, not across the class.
- Fields set to null/undefined in constructor and only populated in certain code paths.
- Severity: Low.
- Refactoring: Extract Class or Introduce Null Object.

============================================================
PHASE 4: CHANGE PREVENTERS
============================================================

DIVERGENT CHANGE:
- Find classes that are modified for multiple unrelated reasons.
- Heuristic: class has methods touching 3+ different external services or data sources.
- Cross-reference with git history if available (file changed in commits with unrelated messages).
- Severity: Medium (2 reasons to change), High (3+ reasons).
- Refactoring: Extract Class per responsibility.

SHOTGUN SURGERY:
- Find changes that require modifying many files for a single logical change.
- Heuristic: a concept (e.g., "user role") that appears in 5+ files across different layers.
- Look for: string literals repeated across files, same enum checked in many places.
- Severity: Medium (5-8 files), High (>8 files).
- Refactoring: Move Method, Inline Class to consolidate scattered logic.

============================================================
PHASE 5: DISPENSABLES
============================================================

LAZY CLASS:
- Find classes/modules with very little behavior (< 3 methods, < 30 lines of logic).
- Wrapper classes that just delegate to another class.
- Severity: Low.
- Refactoring: Inline Class.

SPECULATIVE GENERALITY:
- Find abstractions with only one implementation.
- Interfaces/abstract classes with a single concrete class.
- Generic type parameters that are always the same concrete type.
- Unused method parameters kept "for future use."
- Severity: Low (single instance), Medium (pattern across codebase).
- Refactoring: Collapse Hierarchy, Remove Parameter, Inline Class.

DEAD CODE (brief check -- defer to /dead-code for full analysis):
- Unreachable code after return/throw/break.
- Commented-out code blocks (> 5 lines).
- Severity: Low.

============================================================
PHASE 6: COUPLERS
============================================================

FEATURE ENVY:
- Find methods that use more data/methods from another class than their own.
- Count references to own class vs foreign class in each method.
- For each: file, method, own-class refs, foreign-class refs, which class it envies.
- Severity: Medium (2x more foreign refs), High (3x+ more foreign refs).
- Refactoring: Move Method to the envied class.

MESSAGE CHAINS (train wrecks):
- Find chains of 3+ accessor calls: a.getB().getC().getD().
- For each: file, line, chain length, the full chain expression.
- Severity: Low (3 links), Medium (4 links), High (5+ links).
- Refactoring: Hide Delegate, Extract Method.

MIDDLE MAN:
- Find classes where >50% of methods just delegate to another class.
- For each: file, class, delegation percentage, the delegate class.
- Severity: Medium.
- Refactoring: Remove Middle Man, Inline Class.

INAPPROPRIATE INTIMACY:
- Find classes that access private/internal members of other classes.
- Classes that reach into another class's internal state instead of using its public API.
- Bidirectional dependencies between classes.
- Severity: Medium (one direction), High (bidirectional).
- Refactoring: Move Method, Extract Class, Hide Delegate.


============================================================
SELF-HEALING VALIDATION (max 3 iterations)
============================================================

After completing fixes, re-validate your work:

1. Re-run the specific checks that originally found issues.
2. Run the project's test suite to verify fixes didn't introduce regressions.
3. Run build/compile to confirm no breakage.
4. If new issues surfaced from fixes, add them to the fix queue.
5. Repeat the fix-validate cycle up to 3 iterations total.

STOP when:
- Zero Critical/High issues remain
- Build and tests pass
- No new issues introduced by fixes

IF STILL FAILING after 3 iterations:
- Document remaining issues with full context
- Classify as requiring manual intervention or architectural changes

============================================================
OUTPUT
============================================================

## Code Smell Report

### Stack: {detected stack}
### Files scanned: {count}
### Total smells found: {count}

### Smell Summary by Category

| Category | Critical | High | Medium | Low | Total |
|---|---|---|---|---|---|
| Bloaters | {n} | {n} | {n} | {n} | {n} |
| OO Abusers | {n} | {n} | {n} | {n} | {n} |
| Change Preventers | {n} | {n} | {n} | {n} | {n} |
| Dispensables | {n} | {n} | {n} | {n} | {n} |
| Couplers | {n} | {n} | {n} | {n} | {n} |

### Critical & High Smells (ranked by impact)

1. **{Smell Type}: {location}** -- Severity: {Critical/High}
   - File: `{file:line}`
   - Detail: {specific description of the smell}
   - Impact: {why this matters -- maintenance burden, bug risk, etc.}
   - Refactoring: {specific recommendation with approach}
   - Effort: {S/M/L}

### Top 10 Worst Files

| File | Lines | Smells | Worst Smell | Recommendation |
|---|---|---|---|---|
| {file} | {lines} | {count} | {smell type} | {refactoring} |

### Refactoring Priority Queue

1. {Refactoring action} -- fixes {N} smells, effort {S/M/L}
2. ...

### Metrics
- Average method length: {N} lines
- Average class length: {N} lines
- Longest method: {name} ({N} lines) in `{file}`
- Largest class: {name} ({N} lines) in `{file}`
- Deepest message chain: {N} links in `{file:line}`

DO NOT:
- Flag standard framework patterns as smells (e.g., Flutter build methods, React render).
- Count blank lines, comments, or imports toward method/class length.
- Flag test files for code smells (test code has different conventions).
- Recommend refactoring without a specific approach.

NEXT STEPS:
- "Run `/dead-code` for thorough dead code removal."
- "Run `/iterate` to implement the top refactorings."
- "Run `/tech-debt` for a broader technical debt inventory."
- "Run `/perf` to check if any smelly code is also a performance bottleneck."


============================================================
SELF-EVOLUTION TELEMETRY
============================================================

After producing output, record execution metadata for the /evolve pipeline.

Check if a project memory directory exists:
- Look for the project path in `~/.claude/projects/`
- If found, append to `skill-telemetry.md` in that memory directory

Entry format:
```
### /code-smell — {{YYYY-MM-DD}}
- Outcome: {{SUCCESS | PARTIAL | FAILED}}
- Self-healed: {{yes — what was healed | no}}
- Iterations used: {{N}} / {{N max}}
- Bottleneck: {{phase that struggled or "none"}}
- Suggestion: {{one-line improvement idea for /evolve, or "none"}}
```

Only log if the memory directory exists. Skip silently if not found.
Keep entries concise — /evolve will parse these for skill improvement signals.
