$npx -y skills add GrishaAngelovGH/gemini-cli-agent-skills --skill expert-code-refactoringExpert code refactoring for Java, JavaScript, and React projects. Focuses on SOLID principles, design patterns, and idiomatic improvements while ensuring test stability.
| 1 | # Refactoring Skill |
| 2 | |
| 3 | This skill guides the agent in refactoring codebases by identifying technical debt and applying industry-standard patterns tailored to the specific technology stack. |
| 4 | |
| 5 | ## General Refactoring Workflow |
| 6 | |
| 7 | 1. **Analyze Context:** Read the file and its associated tests. Identify dependencies and usage patterns. |
| 8 | 2. **Verify Tests:** Before any changes, run existing tests to ensure a stable baseline. |
| 9 | 3. **Identify Smells:** Look for long methods, deep nesting, duplicate code, or tight coupling. |
| 10 | 4. **Incremental Changes:** Apply transformations in small, verifiable steps. |
| 11 | 5. **Re-verify:** Run tests after each significant change. |
| 12 | |
| 13 | ## Architecture & Clean Code (Global) |
| 14 | |
| 15 | - **Naming:** Rename variables, functions, and classes to be descriptive and reveal intent. Avoid abbreviations. |
| 16 | - **AHA Programming:** Avoid Hasty Abstractions. Only abstract code when the duplication is clear and the abstraction doesn't make the code harder to follow. |
| 17 | - **Functions:** Keep functions small. Aim for a low number of parameters (prefer objects/records for many parameters). |
| 18 | - **Cognitive Load:** Reduce nesting levels (aim for max 2-3 deep). Use early returns to keep the "happy path" aligned to the left. |
| 19 | |
| 20 | ## Refactoring for Testability |
| 21 | |
| 22 | - **Dependency Injection (DI):** Replace hardcoded instances or static calls with injected dependencies (via constructor or parameters). |
| 23 | - **Interface Segregation:** If a class depends on a large interface but only uses one method, extract a smaller interface. |
| 24 | - **Pure Functions:** Extract business logic into pure functions that don't depend on external state or side effects, making them trivial to unit test. |
| 25 | - **Mocking Boundaries:** Identify "seams" where you can inject mocks (e.g., Database, Network, System Clock). |
| 26 | |
| 27 | ## Legacy Code Techniques (Safe Changes) |
| 28 | |
| 29 | - **Characterization Tests:** If tests are missing, write "Golden Master" tests that record the current behavior *before* changing it. |
| 30 | - **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. |
| 31 | - **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. |
| 32 | - **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. |
| 33 | |
| 34 | ## Common Code Smells & Solutions |
| 35 | |
| 36 | | Smell | Description | Refactoring Pattern | |
| 37 | | :--- | :--- | :--- | |
| 38 | | **Magic Literals** | Hardcoded numbers/strings. | **Extract Constant** or **Enum**. | |
| 39 | | **Long Method** | Method does too many things. | **Extract Method**. | |
| 40 | | **Large Class** | Too many responsibilities. | **Extract Class** or **Extract Interface**. | |
| 41 | | **Feature Envy** | Method uses another object's data more. | **Move Method**. | |
| 42 | | **Primitive Obsession** | Using primitives for domain concepts. | **Introduce Value Object**. | |
| 43 | | **Multi‑File Change Requirement** | One change affects many files. | **Move Field** or **Inline Class** to centralize. | |
| 44 | |
| 45 | ## Language Specifics |
| 46 | |
| 47 | ### Java |
| 48 | - **SOLID & Modern Features:** Use `record` (Java 14+), `sealed` classes (Java 17+), and **Switch Pattern Matching**. |
| 49 | - **Streams & Optional:** Replace imperative loops with `Stream`. Use `Optional` to eliminate null checks. |
| 50 | - **Immutability:** Use `final` and immutable collections (`List.of`, `Map.of`). |
| 51 | - **Exception Handling:** Use try-with-resources. Avoid catching `Throwable` or `Exception` generically. |
| 52 | |
| 53 | ### JavaScript / TypeScript |
| 54 | - **Modern Syntax:** `const` by default, destructuring, spread/rest, arrow functions. |
| 55 | - **TypeScript Advanced:** **Discriminated Unions**, **Utility Types** (`Pick`, `Omit`), and **Custom Type Guards**. |
| 56 | - **Data Integrity:** Use Zod or similar for runtime validation at API/IO boundaries. |
| 57 | - **Async Patterns:** Prefer `Promise.all` for concurrency; avoid "async-await" inside loops where possible. |
| 58 | |
| 59 | ### React |
| 60 | - **Component Design:** Decompose large components. Extract logic into **Custom Hooks**. |
| 61 | - **State Management:** **Principle of Least Privilege** (keep state local). Avoid **Prop Drilling** with Context or Composition. |
| 62 | - **Performance:** Use `useMemo`/`useCallback` for stable references. Avoid inline function definitions in props of memoized children. |
| 63 | - **Testing:** Focus on user behavior and accessible roles (`getByRole`) via **React Testing Library**. |
| 64 | |
| 65 | ## Security & Reliability |
| 66 | - **Input Validation:** Sanitize and validate all external input. |
| 67 | - **Secrets:** Never hardcode keys; use environment variables. |
| 68 | - **Dependencies:** Update deprecated/vulnerable packages discovered during refactoring. |
| 69 | |
| 70 | ## Constraints |
| 71 | - Never change external APIs or public interfaces without explicit user permission. |
| 72 | - Always maintain o |