$npx -y skills add totvs/engpro-advpl-tlpp-skills --skill refactorSurgical code refactoring to improve maintainability without changing behavior. Covers extracting functions, renaming variables, breaking down god functions, improving type safety, eliminating code smells, and applying design patterns. Less drastic than repo-rebuilder; use for gr
| 1 | # Refactor |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Improve code structure and readability without changing external behavior. Refactoring is gradual evolution, not revolution. Use this for improving existing code, not rewriting from scratch. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | Use this skill when: |
| 10 | |
| 11 | - Code is hard to understand or maintain |
| 12 | - Functions/classes are too large |
| 13 | - Code smells need addressing |
| 14 | - Adding features is difficult due to code structure |
| 15 | - User asks "clean up this code", "refactor this", "improve this" |
| 16 | |
| 17 | --- |
| 18 | |
| 19 | ## Refactoring Principles |
| 20 | |
| 21 | ### The Golden Rules |
| 22 | |
| 23 | 1. **Behavior is preserved** - Refactoring doesn't change what the code does, only how |
| 24 | 2. **Small steps** - Make tiny changes, test after each |
| 25 | 3. **Version control is your friend** - Commit before and after each safe state |
| 26 | 4. **Tests are essential** - Without tests, you're not refactoring, you're editing |
| 27 | 5. **One thing at a time** - Don't mix refactoring with feature changes |
| 28 | |
| 29 | ### When NOT to Refactor |
| 30 | |
| 31 | ``` |
| 32 | - Code that works and won't change again (if it ain't broke...) |
| 33 | - Critical production code without tests (add tests first) |
| 34 | - When you're under a tight deadline |
| 35 | - "Just because" - need a clear purpose |
| 36 | ``` |
| 37 | |
| 38 | --- |
| 39 | |
| 40 | ## Common Code Smells |
| 41 | |
| 42 | Identify and fix these common code smells. See [references/code-smells-and-patterns.md](references/code-smells-and-patterns.md) for detailed before/after examples. |
| 43 | |
| 44 | | # | Smell | Fix | |
| 45 | | --- | ------------------------------- | --------------------------------------------------------------------------- | |
| 46 | | 1 | Long Method/Function | Break into focused functions | |
| 47 | | 2 | Duplicated Code | Extract common logic | |
| 48 | | 3 | Large Class/Module | Single responsibility per class | |
| 49 | | 4 | Long Parameter List | Group into parameter objects or use builder | |
| 50 | | 5 | Feature Envy | Move logic to the object that owns the data | |
| 51 | | 6 | Primitive Obsession | Use domain types | |
| 52 | | 7 | Magic Numbers/Strings | Named constants | |
| 53 | | 8 | Nested Conditionals | Guard clauses / early returns | |
| 54 | | 9 | Dead Code | Remove it (git has history) | |
| 55 | | 10 | Inappropriate Intimacy | Ask, don't tell — use encapsulation | |
| 56 | | 11 | IIF Usage | Replace `IIF()` / `IF()` with `If/Else/EndIf` for clarity and testability | |
| 57 | | 12 | API Calls in Loops | Move `GetMV()`, `ExistBlock()`, `Type()` out of loops — cache before loop | |
| 58 | | 13 | UI in Transactions | Never call `MsgAlert`, `MsgYesNo`, `Aviso`, `Help` inside transaction scope | |
| 59 | | 14 | SQL Injection via Concatenation | Replace string concatenation in queries with `FWExecStatement` | |
| 60 | | 15 | ISAM Driver Usage | Migrate `MSCREATE`, `DBCREATE`, `CRIATRAB` to `FWTemporaryTable` | |
| 61 | |
| 62 | ## Refactoring Techniques |
| 63 | |
| 64 | Detailed examples in [references/code-smells-and-patterns.md](references/code-smells-and-patterns.md): |
| 65 | |
| 66 | - **Extract Method** — Turn code fragments into focused methods |
| 67 | - **Introduce Type Safety** — Add types to parameters, returns, and variables |
| 68 | - **Strategy Pattern** — Replace conditional logic with polymorphism |
| 69 | - **Chain of Responsibility** — Replace nested validation with composable validators |
| 70 | |
| 71 | --- |
| 72 | |
| 73 | ## Refactoring Steps |
| 74 | |
| 75 | ### Safe Refactoring Process |
| 76 | |
| 77 | ``` |
| 78 | 1. PREPARE |
| 79 | - Ensure tests exist (write them if missing) |
| 80 | - Commit current state |
| 81 | - Create feature branch |
| 82 | |
| 83 | 2. IDENTIFY |
| 84 | - Find the code smell to address |
| 85 | - Understand what the code does |
| 86 | - Plan the refactoring |
| 87 | |
| 88 | 3. REFACTOR (small steps) |
| 89 | - Make one small change |
| 90 | - Run tests |
| 91 | - Commit if tests pass |
| 92 | - Repeat |
| 93 | |
| 94 | 4. VERIFY |
| 95 | - All tests pass |
| 96 | - Manual testing if needed |
| 97 | - Performance unchanged or improved |
| 98 | |
| 99 | 5. CLEAN UP |
| 100 | - Update comments |
| 101 | - Update documentation |
| 102 | - Final commit |
| 103 | ``` |
| 104 | |
| 105 | --- |
| 106 | |
| 107 | ## Refactoring C |