Skip to content

Instantly share code, notes, and snippets.

@cainus
Created February 27, 2026 23:09
Show Gist options
  • Select an option

  • Save cainus/2ea7108aff3850ce87ad66144a564cd2 to your computer and use it in GitHub Desktop.

Select an option

Save cainus/2ea7108aff3850ce87ad66144a564cd2 to your computer and use it in GitHub Desktop.
/defrag claude skill
---
name: defrag
description: Analyze code for refactoring opportunities and suggest the top 10 highest-value improvements. Use when the user says /defrag, "refactor", "clean up code", or asks for code improvements.
---
# Defrag - Code Refactoring Analysis
Analyze the specified file(s) or the user's current selection and identify the top 10 highest-value refactoring opportunities.
## Refactoring Types to Look For
1. **shorten_file** - Files >300 lines should be broken into multiple coherent files
2. **shorten_function** - Functions >50 lines should be broken into smaller functions with descriptive names
3. **reduce_nesting** - Replace nested conditionals with guard clauses and early returns
4. **extract_function** - Extract repeated or complex code into well-named functions
5. **rename_for_clarity** - Improve variable/function names that are unclear or too short
6. **simplify_conditionals** - Simplify if/else logic, use early return where possible
7. **extract_constants** - Move magic numbers/strings to local constants (NOT global constants files)
8. **consolidate_duplicates** - Merge duplicate code blocks
9. **modernize_syntax** - Use modern language features (destructuring, optional chaining, etc.)
10. **avoid_globality** - Move global items closer to where they're used; no utils/constants/managers files
11. **optimize_imports** - Clean up unused or poorly organized imports
12. **remove_dead_code** - Remove unused code. In typescript you can use knip for that.
13. **add_tests** - Identify code lacking test coverage
14. **break_up_hotspots** - look at the last 30 days of git history for the 5 files with the most changes. Suggest decomposition for any file that's changed more than 30 times.
15. **reduce_code_with_library_suggestion** - Look for things that we're doing a lot, or require a lot of code that might be better served by installing a library. We love using libraries.
## Scoring (Value Score 0-100)
Calculate each refactoring's value using these weights:
- **Readability improvement** (35%)
- **Maintainability improvement** (30%)
- **Bug risk reduction** (25%)
- **Performance impact** (5%)
- **Scope size** (5%)
## Output Format
For each refactoring opportunity, provide:
### {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}
```
---
## Instructions
1. Read the file(s) the user specifies (or use their IDE selection)
2. Analyze for all refactoring types listed above
3. Score each opportunity using the weighted formula
4. Sort by value score (highest first)
5. Present the top 10 refactorings
6. End with: "Found {N} refactoring opportunities. Top 10 shown with average score: {avg}"
## Important Rules
- **Do NOT apply changes** - This is analysis only (plan mode)
- **Sort by value_score** - Highest value first
- **Show before/after code** - For each suggestion
- **Be specific** - Include exact file paths and line numbers
- **Follow YAGNI** - Don't suggest over-engineering or future-proofing
- **No global constants files** - Constants belong near their usage
After presenting, ask: "Would you like me to apply any of these? Say 'apply #1' or 'apply all'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment