God Function Decomposition Plan
A behavior-preserving plan for breaking up a 300-line function: responsibility inventory, hidden coupling map, then an extraction sequence where every step compiles and passes.
Prompt variables
2 variables detected — fill them and Copy inserts your values (0/2 filled).
I have a function that has grown into a monster. Produce a decomposition plan — not a rewritten function dumped in one shot, but a sequence of safe extractions I can execute and verify one at a time.
Step 1 — **Responsibility inventory.** List every distinct job this function performs (validation, fetching, transformation, decision, persistence, notification, logging...). For each: which lines, and what data it consumes/produces.
Step 2 — **Coupling map.** The reason god functions resist extraction is hidden coupling. Identify:
- Variables written in one section and read far below (these become parameters or return values — list them per section)
- Early returns and exceptions that skip later sections (extraction must preserve exactly which sections get skipped)
- Temporal coupling: sections that only work because an earlier section mutated something (flag these LOUDLY)
- Reused mutable locals that mean different things at different points
Step 3 — **Extraction sequence.** Ordered steps where each step is one extraction, keeps the code compiling and all tests green, and can be committed alone. For each step: the new function's exact signature, what it returns vs. mutates (prefer returning), and a one-line rationale for the name. Sequence easiest-first so the plan builds confidence, unless a specific extraction unlocks the others — then say so.
Step 4 — **Target shape.** Show the final body of the original function after all extractions: it should read as a narrative of calls, roughly 10–25 lines. If sections turned out to belong in different classes/modules entirely, note it as follow-up work — do NOT fold moves into this plan.
Hard constraint: zero behavior change, including error behavior, logging output, and evaluation order of side effects. Anything that would change behavior gets listed at the end under "tempting but not now".
```{{language}}
{{code}}
```
Comments (0)