Tpl situacao refatoracao codigo legado

From artubss/SKILLS-CLAUDE-CODE · View interactive version

Tpl situacao refatoracao codigo legado is an AI agent skill from the GitHub repository artubss/SKILLS-CLAUDE-CODE. Template do pack (situacao/01-refatoracao-codigo-legado.md). Orienta o agente em tarefas situacionais como debug, seguranca e refactor alinhado a esse contexto.

How to use this skill

Skill file

SITUATION: Refactoring Legacy Code

Skill gerado a partir do pack templates-claude-code. Arquivo de origem: situacao/01-refatoracao-codigo-legado.md. Use como baseline e adapte ao projeto antes de mudancas grandes.

Conteudo do template

CONTEXT

Use this configuration when you need to safely improve the structure of existing code without changing its observable behavior. This applies to: removing dead code, extracting functions, renaming for clarity, adding types to untyped JavaScript, splitting god objects/files, or paying down technical debt incrementally. NOT for rewrites from scratch.

OBJECTIVES

APPROACH RULES

  1. Test before you touch. If there are no tests for a piece of code, write characterization tests first — tests that capture current behavior, even if that behavior is wrong.
  1. Strangler Fig Pattern. Never do big-bang rewrites. Create the new implementation alongside the old, redirect traffic gradually, then delete the old code. Never leave both running indefinitely.
  1. One concern per commit. Each commit should do exactly one thing: rename, extract, move, or simplify. Never mix refactoring with behavior changes in the same commit.
  1. Red-Green-Refactor. If adding tests: write a failing test → make it pass → refactor. Never skip the red phase.
  1. Preserve the public interface. Refactoring internal logic is safe. Changing method signatures or return shapes is a breaking change — handle it separately.
  1. Adding TypeScript types to JS: Start at the leaf nodes (utility functions) and work inward toward the core. Use unknown instead of any. Enable strict: true in tsconfig from day one — do not plan to "add strict later."
  1. The Boy Scout Rule. Leave every file you touch slightly better than you found it. Never make it worse. Even a simple rename or extracted constant counts.
  1. Refactor vs Rewrite decision: Refactor if the logic is fundamentally sound but messy. Rewrite if the logic is broken or the architecture cannot support future requirements. Default to refactoring — rewrites almost always take 3× longer than estimated.

ROUTING TABLE

| If you encounter | Then |

|-----------------|------|

| A function over 50 lines | Extract sub-functions with descriptive names. Each function should do one thing. |

| A file over 300 lines | Identify natural module boundaries and split. Keep the original file as a re-export barrel temporarily. |

| Magic numbers or strings | Extract to named constants at the top of the file or a constants.ts file |

| Deeply nested conditionals (3+ levels) | Apply early return / guard clause pattern to invert the logic |

| Any // TODO or // HACK comment | Create a GitHub Issue for it, then fix it in this PR if it's in scope |

| any type in TypeScript | Replace with the correct type. If unknown, use unknown and add a type guard |

| Duplicate logic in 3+ places | Extract to a shared utility. Do not extract if only 2 occurrences — wait for the third |

| A class doing 5+ unrelated things | Apply Single Responsibility — split into separate classes/modules |

| No error handling in async code | Add try/catch with explicit error types. Never swallow errors silently |

| Commented-out code | Delete it. Git history exists for a reason |

DO NOT

OUTPUT FORMAT

For each refactoring session, produce:

  1. Summary of changes — bullet list of what was changed and why
  2. Before/After snippets — for each significant change, show old code vs new code
  3. Test coverage delta — lines/branches covered before vs after
  4. Open items — things noticed but intentionally left for follow-up issues
  5. Risks — any changes that could have non-obvious side effects

Example commit message format:

refactor(auth): extract token validation to pure function

- Moved token parsing logic from AuthMiddleware into validateToken()
- Added unit tests for all token edge cases
- No behavior change — same logic, same error messages

Refs: #342

QUALITY GATES

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.