Behavior-Preserving Refactor

Refactor messy code without changing behavior: named invariants, small reviewable steps that each pass tests, and a strict scope fence.

Refactor the code below. The hard constraint: observable behavior must not change, same outputs, same side effects, same error behavior, same performance characteristics within reason.

What hurts about the current code: [THE PAIN, e.g. "600-line function", "logic duplicated in 4 places", "impossible to test"]
Test coverage: [WHAT TESTS EXIST, or "none", in which case start with step 0]
Scope fence, files/functions you may touch: [LIST. Nothing outside this list.]

Code:
"""
[PASTE CODE]
"""

Work like this:
1. **Invariants first**: list the observable behaviors that must survive: inputs → outputs, side effects and their order, thrown errors and their types, anything callers depend on. If tests don't cover an invariant, write the missing test BEFORE refactoring (step 0), so we're never flying blind.
2. **Stepwise plan**: break the refactor into steps of one named technique each (extract function, inline variable, replace conditional with lookup, move responsibility). Every step must leave the code compiling and tests passing, no step depends on a later step to restore correctness.
3. **Execute**: show the result after each step, not one final blob. For each step: which technique, what moved, and one line on why the behavior is provably unchanged.
4. **Summary**: before/after comparison: lines, duplication removed, what's now testable that wasn't. And list anything you noticed but did NOT fix because it was outside the scope fence or would change behavior. Flag it, don't touch it.

Rules: no renaming beyond what the refactor requires, no "while I'm here" improvements, no new dependencies, no API changes unless I explicitly listed them as in scope.
Originated fromStan SedberryUpdated
Refactoringintermediate

More coding prompts

Write the commit message for this change.

Diff:
"""
[PASTE THE STAGED DIFF]
"""

Why I made this change: [THE REASON, THE TICKET, THE BUG REPORT, or "you infer it"]
Convention: [Conventional Commits / this repo's existing style, pasted below / plain]
Recent commits from this repo, to match style:
"""
[PASTE 5-10 RECENT COMMIT SUBJECT LIN

Commit Message

Write a commit message that explains why the change was made, in Conventional Commits format, split into separate commits when needed.

Codingbeginner
Help me recover from a git mistake without making it worse.

What I was trying to do: [THE GOAL]
What I ran: [THE EXACT COMMANDS, IN ORDER]
What happened instead: [THE OUTPUT OR THE STATE NOW]
Has this been pushed or shared: [YES/NO, and to which branch and whether anyone else has pulled]
Uncommitted work I cannot lose: [WHAT AND WHERE, o

Undo a Git Mistake

Recover from a bad commit, force push, wrong branch, or lost work with a reversible plan and the exact commands, explained before you run them.

Codingintermediate
Handler code, routes, and models:
"""
[PASTE THE ROUTE DEFINITIONS, HANDLERS, REQUEST AND RESPONSE TYPES, VALIDATION SCHEMAS, AND MIDDLEWARE]
"""

Generate an OpenAPI 3.1 specification from the code above.

API name, version, and base URL: [DETAILS]
Auth scheme: [BEARER JWT / API KEY / OAUTH / SESSION COOKIE, and where it is enforced]
Con

OpenAPI Spec From Code

Generate an accurate OpenAPI 3.1 spec from handler code, including error responses and auth, with gaps flagged instead of invented.

Codingintermediate

Search prompts

Find a prompt by title, description, tag, or category.