5

The worst function in our codebase does input parsing, three network calls, retry logic, formatting, and logging in one 600-line block. I wanted GPT to carve it into small pure functions plus a thin orchestrator, but the naive ask always quietly changed error handling or reordered side effects.

The approach that held: make it first map the function as a list of numbered 'effects' and 'pure steps' in execution order, get me to confirm that map, then extract only pure steps into named functions while leaving the effect sequence in the orchestrator byte-for-byte identical. Behavior preservation becomes a checkable property, not a hope.

No answers needed, just banking the prompt here for the next poor soul who inherits a god function.

THE PROMPT
Refactor a large function into small pure functions + a thin orchestrator, WITHOUT changing observable behavior or the order of side effects.

STEP 1 - Effect map. Read the function and output a numbered list of every step in execution order, tagging each as PURE (no IO/mutation/randomness/clock) or EFFECT (network, disk, log, global mutation, time, exception thrown). Do not write code yet. Wait for me to confirm the map is accurate.

STEP 2 - Extract pure steps only. Pull each PURE run of steps into a named, side-effect-free function with explicit inputs and return values. Do NOT merge, reorder, or remove any EFFECT step. The orchestrator keeps the EFFECT steps in the exact same order and still calls them at the same points.

STEP 3 - Prove equivalence. Show the before/after side-effect sequence side by side and confirm they are identical (same calls, same order, same error propagation). For each extracted pure function, note that it is now unit-testable in isolation and give one example test.

Hard rules: no behavior changes 'while you're in there'. No swallowed exceptions. If you spot a real bug, flag it separately, do not fix it silently.

Function:
{PASTE}

0 Answers

Your Answer