agentleFS
GrishaAngelovGHGrishaAngelovGH/gemini-cli-agent-skills/expert-code-refactoring/SKILL.md

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.

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
  1. Distinctive:

    Analyze Context: Read the file and its associated tests.

    line 12
  2. Distinctive:

    Verify Tests: Before any changes, run existing tests to ensure a stable baseline.

    line 13
  3. Distinctive:

    Identify Smells: Look for long methods, deep nesting, duplicate code, or tight coupling.

    line 14
  4. Distinctive:

    Re-verify: Run tests after each significant change.

    line 16
  5. Distinctive:

    AHA Programming: Avoid Hasty Abstractions. Only abstract code when the duplication is clear and the abstraction doesn't make the code …

    line 21
  6. Distinctive:

    Functions: Keep functions small. Aim for a low number of parameters (prefer objects/records for many parameters).

    line 22
  7. Distinctive:

    Interface Segregation: If a class depends on a large interface but only uses one method, extract a smaller interface.

    line 28
  8. Distinctive:

    Characterization Tests: If tests are missing, write "Golden Master" tests that record the current behavior *before* changing it.

    line 34
  9. Distinctive:

    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.

    line 35
  10. Distinctive:

    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.

    line 36
  11. Distinctive:

    Break Dependencies: Use the "Extract and Override" pattern to isolate untestable static calls or constructors in a protected method that …

    line 37
  12. Very specific:

    SOLID & Modern Features: Use record (Java 14+), sealed classes (Java 17+), and Switch Pattern Matching.

    line 53
  13. Very specific:

    Immutability: Use final and immutable collections (List.of, Map.of).

    line 55
  14. Very specific:

    Exception Handling: Use try-with-resources. Avoid catching Throwable or Exception generically.

    line 56
  15. Distinctive:

    Data Integrity: Use Zod or similar for runtime validation at API/IO boundaries.

    line 61
  16. Very specific:

    Async Patterns: Prefer Promise.all for concurrency.

    line 62
  17. Very specific:

    Performance: Use useMemo/useCallback for stable references.

    line 67
  18. Distinctive:

    Secrets: Never hardcode keys; use environment variables.

    line 72
  19. Distinctive:

    Dependencies: Update deprecated/vulnerable packages discovered during refactoring.

    line 73
  20. Distinctive:

    Never change external APIs or public interfaces without explicit user permission.

    line 76
  21. Distinctive:

    Always maintain or improve test coverage.

    line 77

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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

  1. Analyze Context: Read the file and its associated tests. Identify dependencies and usage patterns.
  2. Verify Tests: Before any changes, run existing tests to ensure a stable baseline.
  3. Identify Smells: Look for long methods, deep nesting, duplicate code, or tight coupling.
  4. Incremental Changes: Apply transformations in small, verifiable steps.
  5. 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

SmellDescriptionRefactoring Pattern
Magic LiteralsHardcoded numbers/strings.Extract Constant or Enum.
Long MethodMethod does too many things.Extract Method.
Large ClassToo many responsibilities.Extract Class or Extract Interface.
Feature EnvyMethod uses another object's data more.Move Method.
Primitive ObsessionUsing primitives for domain concepts.Introduce Value Object.
Multi‑File Change RequirementOne change affects many files.Move Field or Inline Class to centralize.

Language Specifics

Java

  • SOLID & Modern Features: Use record (Java 14+), sealed classes (Java 17+), and Switch Pattern Matching.
  • Streams & Optional: Replace imperative loops with Stream. Use Optional to eliminate null checks.
  • Immutability: Use final and immutable collections (List.of, Map.of).
  • Exception Handling: Use try-with-resources. Avoid catching Throwable or Exception generically.

JavaScript / TypeScript

  • Modern Syntax: const by 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.all for 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/useCallback for 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.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.