defrag
Analyze code for refactoring opportunities and suggest the top 10 highest-value improvements. Use this whenever the user says `/defrag`, asks to refactor, clean up code, improve code quality, identify code smells, or wants a ranked list of code improvements. This skill is for analysis only unless the user explicitly asks to apply a specific suggestion.
What this skill does
# Defrag
## Purpose
Use this skill to inspect one or more files, identify the highest-value refactoring opportunities, and present a ranked top 10 list with concrete code-level suggestions.
This skill is analysis-first. Do not apply edits unless the user explicitly switches from analysis to implementation.
## Default Behavior
1. Determine the target scope.
2. Read the relevant files or the user's current selection.
3. Inspect the code for the refactoring types listed below.
4. If git history is available, inspect the last 30 days to find change hotspots. Prefer `scripts/git_hotspots.py` for consistent counts.
5. Score each candidate refactor with the provided weighting model.
6. Return the top 10 opportunities sorted by value score.
7. End by asking whether the user wants any suggestion applied.
If the user did not specify files, use the most relevant local context you can discover from the conversation or repo state. If the scope is still ambiguous and a wrong guess would be misleading, ask for the file or directory to analyze.
## Hard Rules
- Do not apply changes during the analysis pass.
- Do not suggest speculative architecture work that is not justified by the current code.
- Follow YAGNI. Prefer targeted, local refactors over framework churn.
- Keep constants close to usage. Do not suggest dumping values into a global constants file.
- Be specific about file path, line range, and the concrete benefit.
- Show short before/after snippets for every recommendation.
- Sort strictly by value score from highest to lowest.
## Refactoring Types To Look For
Check for these opportunity types:
1. `shorten_file`: files over 300 lines that should be split into coherent units
2. `shorten_function`: functions over 50 lines that should be decomposed
3. `reduce_nesting`: nested conditionals that should become guard clauses or early returns
4. `extract_function`: repeated or complex logic that should move into a named helper
5. `rename_for_clarity`: unclear names, overloaded names, or names that hide intent
6. `simplify_conditionals`: brittle or verbose branching that can be simplified
7. `extract_constants`: magic strings, numbers, selectors, or thresholds that should become local constants
8. `consolidate_duplicates`: duplicate logic that should be merged
9. `modernize_syntax`: safe use of modern syntax such as destructuring or optional chaining
10. `avoid_globality`: move global items closer to where they are used
11. `optimize_imports`: remove unused imports and tighten import organization
12. `remove_dead_code`: identify unused code paths, stale helpers, or unreachable branches
13. `add_tests`: call out risky logic with weak or missing coverage
14. `break_up_hotspots`: inspect the last 30 days of git history and flag files changed more than 30 times
## Hotspot Analysis
When git history is available:
1. Inspect the last 30 days of changes.
2. Find the 5 files with the highest change counts.
3. For any file changed more than 30 times, suggest decomposition or responsibility splits.
If git history is unavailable, say so briefly and continue with static code analysis.
To collect hotspot data, you can run:
```bash
python skills/defrag/scripts/git_hotspots.py --repo . --days 30 --limit 5
```
## Scoring Model
Score each refactoring from `0` to `100` using this weighted model:
- Readability improvement: `35%`
- Maintainability improvement: `30%`
- Bug risk reduction: `25%`
- Performance impact: `5%`
- Scope size: `5%`
Use judgment, but keep scores internally consistent. High scores should correspond to changes that are both important and realistically actionable.
## Analysis Workflow
Follow this order:
1. Confirm the target files, folder, or code selection.
2. Read enough surrounding context to avoid shallow suggestions.
3. Check file size, function length, nesting, duplication, naming, imports, and dead code.
4. If applicable, inspect tests near the target code and note important gaps.
5. If applicable, inspect recent git history for churn hotspots.
6. Produce more than 10 candidate opportunities if needed, then sort and keep the best 10.
## Bundled Resources
- `scripts/git_hotspots.py`: counts per-file churn from recent git history and returns a JSON summary for hotspot analysis
- `evals/evals.json`: starter prompts for validating trigger behavior and output structure
- `agents/openai.yaml`: UI-facing metadata and default invocation prompt
## Output Format
Use this exact structure for each ranked item:
### {rank}. **{type}** (Value: {score}/100)
- **File:** `{filepath}:{start_line}-{end_line}`
- **Description:** {one-line description}
- **Rationale:** {why this helps}
**Before:**
```{language}
{current code snippet}
```
**After:**
```{language}
{refactored code snippet}
```
---
After the ranked list, end with:
`Found {N} refactoring opportunities. Top 10 shown with average score: {avg}`
Then ask:
`Would you like me to apply any of these? Say "apply #1" or "apply all".`
## Snippet Guidance
- Keep before/after snippets focused on the exact issue.
- Show enough context to make the refactor understandable.
- Prefer realistic after examples that fit the current code style.
- Do not fabricate unrelated helper layers or abstractions.
## Review Mindset
Optimize for the highest-value improvements, not the largest number of comments.
A strong result:
- catches structural issues before cosmetic ones
- points to the exact lines that matter
- proposes changes that reduce future editing cost
- avoids vague advice such as "clean this up" or "improve readability"
## Failure Modes To Avoid
Do not:
- recommend massive rewrites without evidence
- flood the list with trivial rename suggestions when there are larger structural problems
- suggest moving everything into shared utils or managers
- present style-only changes as high-value refactors
- apply code edits unless the user explicitly asks for implementation
Related in Code Review
gstack
IncludedFast headless browser for QA testing and site dogfooding. Navigate pages, interact with elements, verify state, diff before/after, take annotated screenshots, test responsive layouts, forms, uploads, dialogs, and capture bug evidence. Use when asked to open or test a site, verify a deployment, dogfood a user flow, or file a bug with screenshots. (gstack)
startup-due-diligence
IncludedLegal due diligence review for seed-stage and Series A startups (US, Delaware C-Corp focus). Supports both investor and founder perspectives. Capabilities include: (1) Interactive document review and issue spotting; (2) Document request list generation; (3) Cap table and SAFE/convertible note analysis; (4) Red flag identification with severity ratings; (5) Diligence report generation. TRIGGERS: due diligence, DD, startup investment, cap table review, Series A, seed round, investor diligence, legal review startup, SAFE analysis, convertible note, 409A, founder vesting.
interview-master
IncludedThis skill should be used when the user asks to "generate interview questions", "prepare for interview", "optimize resume", "conduct mock interview", "analyze git commits for resume", "generate resume from code", "review my resume", or mentions interview preparation, career assistance, or extracting project experience from git history. Provides comprehensive interview and career development guidance for both job seekers and interviewers.
fix-issue
IncludedFixes GitHub issues using parallel analysis agents for root cause investigation, code exploration, and regression detection. Reads issue context from gh CLI, searches codebase and memory for related patterns, generates a fix with tests, and links the resolution back to the issue via PR. Includes prevention analysis to avoid recurrence. Use when debugging errors, resolving regressions, fixing bugs, or triaging issues.
sf-apex
IncludedGenerates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.
swift-development
IncludedComprehensive Swift development for building, testing, and deploying iOS/macOS applications. Use when Claude needs to: (1) Build Swift packages or Xcode projects from command line, (2) Run tests with XCTest or Swift Testing framework, (3) Manage iOS simulators with simctl, (4) Handle code signing, provisioning profiles, and app distribution, (5) Format or lint Swift code with SwiftFormat/SwiftLint, (6) Work with Swift Package Manager (SPM), (7) Implement Swift 6 concurrency patterns (async/await, actors, Sendable), (8) Create SwiftUI views with MVVM architecture, (9) Set up Core Data or SwiftData persistence, or any other Swift/iOS/macOS development tasks.