expert-code-refactoring
Skill16 starsChanged 10 months agoMITView on GitHub
Expert code refactoring for Java, JavaScript, and React projects. Focuses on SOLID principles, design patterns, and idiomatic improvements while ensuring test stability.
In this repository6 other files it gives its agents.
- bug-investigatorSkill
- code-reviewerSkill
- frontend-ui-designerSkill
- project-analyzerSkill
- project-feature-explainerSkill
- react-test-engineerSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
21 rules. About 0 are common, 16 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈16
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 12
Analyze Context: Read the file and its associated tests.
- Distinctive: line 13
Verify Tests: Before any changes, run existing tests to ensure a stable baseline.
- Distinctive: line 14
Identify Smells: Look for long methods, deep nesting, duplicate code, or tight coupling.
- Distinctive: line 16
Re-verify: Run tests after each significant change.
- Distinctive: line 21
AHA Programming: Avoid Hasty Abstractions. Only abstract code when the duplication is clear and the abstraction doesn't make the code …
- Distinctive: line 22
Functions: Keep functions small. Aim for a low number of parameters (prefer objects/records for many parameters).
- Distinctive: line 28
Interface Segregation: If a class depends on a large interface but only uses one method, extract a smaller interface.
- Distinctive: line 34
Characterization Tests: If tests are missing, write "Golden Master" tests that record the current behavior *before* changing it.
- Distinctive: line 35
Sprout Method: When adding a feature to a messy method, write the new logic in a new, clean method and call it from the old one.
- Distinctive: line 36
Wrap Method: Add new behavior by creating a new method with the same signature that calls the old method and then adds the new logic.
- Distinctive: line 37
Break Dependencies: Use the "Extract and Override" pattern to isolate untestable static calls or constructors in a protected method that …
- Very specific: line 53
SOLID & Modern Features: Use record (Java 14+), sealed classes (Java 17+), and Switch Pattern Matching.
- Very specific: line 55
Immutability: Use final and immutable collections (List.of, Map.of).
- Very specific: line 56
Exception Handling: Use try-with-resources. Avoid catching Throwable or Exception generically.
- Distinctive: line 61
Data Integrity: Use Zod or similar for runtime validation at API/IO boundaries.
- Very specific: line 62
Async Patterns: Prefer Promise.all for concurrency.
- Very specific: line 67
Performance: Use useMemo/useCallback for stable references.
- Distinctive: line 72
Secrets: Never hardcode keys; use environment variables.
- Distinctive: line 73
Dependencies: Update deprecated/vulnerable packages discovered during refactoring.
- Distinctive: line 76
Never change external APIs or public interfaces without explicit user permission.
- Distinctive: line 77
Always maintain or improve test coverage.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Refactoring Skill
This skill guides the agent in refactoring codebases by identifying technical debt and applying industry-standard patterns tailored to the specific technology stack.
General Refactoring Workflow
- Analyze Context: Read the file and its associated tests. Identify dependencies and usage patterns.
- Verify Tests: Before any changes, run existing tests to ensure a stable baseline.
- Identify Smells: Look for long methods, deep nesting, duplicate code, or tight coupling.
- Incremental Changes: Apply transformations in small, verifiable steps.
- Re-verify: Run tests after each significant change.
Architecture & Clean Code (Global)
- Naming: Rename variables, functions, and classes to be descriptive and reveal intent. Avoid abbreviations.
- AHA Programming: Avoid Hasty Abstractions. Only abstract code when the duplication is clear and the abstraction doesn't make the code harder to follow.
- Functions: Keep functions small. Aim for a low number of parameters (prefer objects/records for many parameters).
- Cognitive Load: Reduce nesting levels (aim for max 2-3 deep). Use early returns to keep the "happy path" aligned to the left.
Refactoring for Testability
- Dependency Injection (DI): Replace hardcoded instances or static calls with injected dependencies (via constructor or parameters).
- Interface Segregation: If a class depends on a large interface but only uses one method, extract a smaller interface.
- Pure Functions: Extract business logic into pure functions that don't depend on external state or side effects, making them trivial to unit test.
- Mocking Boundaries: Identify "seams" where you can inject mocks (e.g., Database, Network, System Clock).
Legacy Code Techniques (Safe Changes)
- Characterization Tests: If tests are missing, write "Golden Master" tests that record the current behavior before changing it.
- Sprout Method: When adding a feature to a messy method, write the new logic in a new, clean method and call it from the old one.
- Wrap Method: Add new behavior by creating a new method with the same signature that calls the old method and then adds the new logic.
- Break Dependencies: Use the "Extract and Override" pattern to isolate untestable static calls or constructors in a protected method that can be overridden in a test subclass.
Common Code Smells & Solutions
| Smell | Description | Refactoring Pattern |
| Magic Literals | Hardcoded numbers/strings. | Extract Constant or Enum. |
| Long Method | Method does too many things. | Extract Method. |
| Large Class | Too many responsibilities. | Extract Class or Extract Interface. |
| Feature Envy | Method uses another object's data more. | Move Method. |
| Primitive Obsession | Using primitives for domain concepts. | Introduce Value Object. |
| Multi‑File Change Requirement | One change affects many files. | Move Field or Inline Class to centralize. |
Language Specifics
Java
- SOLID & Modern Features: Use
record(Java 14+),sealedclasses (Java 17+), and Switch Pattern Matching. - Streams & Optional: Replace imperative loops with
Stream. UseOptionalto eliminate null checks. - Immutability: Use
finaland immutable collections (List.of,Map.of). - Exception Handling: Use try-with-resources. Avoid catching
ThrowableorExceptiongenerically.
JavaScript / TypeScript
- Modern Syntax:
constby default, destructuring, spread/rest, arrow functions. - TypeScript Advanced: Discriminated Unions, Utility Types (
Pick,Omit), and Custom Type Guards. - Data Integrity: Use Zod or similar for runtime validation at API/IO boundaries.
- Async Patterns: Prefer
Promise.allfor concurrency; avoid "async-await" inside loops where possible.
React
- Component Design: Decompose large components. Extract logic into Custom Hooks.
- State Management: Principle of Least Privilege (keep state local). Avoid Prop Drilling with Context or Composition.
- Performance: Use
useMemo/useCallbackfor stable references. Avoid inline function definitions in props of memoized children. - Testing: Focus on user behavior and accessible roles (
getByRole) via React Testing Library.
Security & Reliability
- Input Validation: Sanitize and validate all external input.
- Secrets: Never hardcode keys; use environment variables.
- Dependencies: Update deprecated/vulnerable packages discovered during refactoring.
Constraints
- Never change external APIs or public interfaces without explicit user permission.
- Always maintain or improve test coverage.
- Adhere to the project's existing linting and formatting rules.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

