Refactoring Plan for a Large Function
Break a 300-line function into safe steps.
The Open buttons send the prompt text as currently shown below to that site only when you click. Fill in the placeholders first.
Help me safely break up a very large function. Language: [LANGUAGE]. The function: [PASTE_FUNCTION]. What it is supposed to do: [PURPOSE]. Existing tests: [EXISTING_TESTS]. Constraints (public interface, performance, team conventions): [CONSTRAINTS]. Do not rewrite everything at once. Deliver the plan in small, safe steps with BEFORE and AFTER for the first three: STEP 0, safety net: write characterization tests that capture the current behavior, including odd cases; list the inputs to cover and give test code. STEP 1 onward: for each step, show a BEFORE excerpt, the AFTER excerpt, the technique used (extract function, introduce parameter object, replace conditional with early return, remove duplication, separate query from command), and the test to run after the step. Each step must leave the code working. SEQUENCE: a numbered list of all the steps, ordered by risk, with the commit message for each. RISKS: hidden side effects, shared mutable state, global variables, and order dependence you noticed in the code, quoted. STOP RULE: when the function is good enough, and what not to refactor now. Keep behavior identical; mark any bug you spot as a separate item, not fixed inside the refactoring.
Fill in before use
Optional: type your details and the prompt above updates. Nothing is saved or sent.
Replace each bracketed placeholder with your own details, then paste into Any AI or another assistant.
See an example result (fictional data)
STEP 0: write characterization tests for processOrder (normal order, empty cart, expired coupon, out-of-stock item, payment failure). STEP 1 BEFORE: 40 lines validating inputs inline. AFTER: validateOrder(order) returns a list of errors; processOrder returns early if the list is not empty. Technique: extract function. Test: run the characterization tests. Commit: "Extract order validation". STEP 2 BEFORE: nested ifs for discount rules. AFTER: early returns and calculateDiscount(order). SEQUENCE: 1 validation, 2 discounts, 3 inventory check, 4 payment call, 5 notifications. RISKS: line 112 mutates the cart (hidden side effect). Possible bug on line 140 (tax rounded twice): logged separately, not fixed here. STOP RULE: when each function fits on a screen and has one reason to change.
Illustrative only, made-up data. Your results will differ and must be reviewed.