REFACTORING OBJECTIVE
Decompose the 400-line ProcessOrder method into small, named steps without altering a single business rule.
Refactoring goal: readability — make the code communicate its intent without changing what it does.
Change how the code is built — never what the code does. If a transformation cannot be made safely with the available information, leave that code unchanged and say why.
EXISTING CODE CONTEXT
Code context: business-critical — this code implements rules the business depends on.
- Treat every conditional, threshold, and special case as a business rule until proven otherwise — oddities may be load-bearing.
- Do not "fix" behavior that looks wrong; flag it as a question instead. Surprising behavior may be a requirement.
- Prefer transformations that keep the business rules visibly intact and reviewable by a domain expert.
Language: not specified — infer it from the code and stay consistent with it.
Stated constraints:
- Order state transitions must happen in exactly the current sequence
Code to refactor:
```
[Paste the code to refactor here]
```
REFACTORING GOAL
Primary goal: Readability.
Transformation priorities for this goal:
1. Rename for clarity: variables, functions, and types should say what they mean — a reader should not have to reverse-engineer intent.
2. Extract small, well-named functions from long blocks — each doing one nameable thing.
3. Make intent explicit: replace clever constructs and magic values with code that states what it is doing.
4. Reduce the reader's working memory: fewer things to track at once, shorter distance between definition and use.
RISK PROFILE
Risk level: Balanced.
- Apply reasonable improvements: moderate restructuring is allowed where the benefit is clear.
- Group related changes, but keep each group reviewable and separately verifiable.
- When a transformation is borderline-safe, apply the conservative version and note the more aggressive option.
SAFETY RULES
These rules apply to every transformation, at every risk level:
1. Identify behavior assumptions before changing code — list every assumption you rely on.
2. Flag uncertain behavior instead of deciding it: if you cannot tell what a branch does or why it exists, ask — do not guess.
3. Do not remove functionality. Code that looks redundant may be load-bearing.
4. Do not invent requirements. Refactor toward the stated goal, not toward an imagined better product.
5. Do not rewrite unrelated code. Touch only what the goal requires.
6. Explain why each change exists — every transformation traces to the stated goal or to a named problem in the code.
7. Preserve public contracts — signatures, return types, error types, serialized shapes — unless explicitly instructed otherwise.
TRANSFORMATION GUIDANCE
Known failure modes of this goal — watch for:
- A rename is a behavior risk at every call site — trace every reference, including reflection, serialization, and string-based lookups.
- Do not "clean up" expressions you do not fully understand — readability refactors fail when they simplify semantics, not just syntax.
BEHAVIOR PRESERVATION REQUIREMENTS
- Preserve observable behavior: the same inputs produce the same outputs, including error cases.
- Preserve business rules: every condition, threshold, and special case survives the refactor exactly.
- Preserve outputs: formats, ordering, precision, and encodings stay identical unless the goal explicitly targets them.
- Preserve side effects: writes, emitted events, log lines other systems consume — and their relative order.
- Preserve integrations: API shapes, serialized payloads, database access patterns, and external call sequences.
- These requirements hold unless the instructions explicitly say otherwise — and any knowingly behavior-affecting change must be flagged, never silent.
- If achieving the goal and preserving behavior conflict, behavior wins: stop and explain the conflict instead of compromising it.
VALIDATION STRATEGY
Goal-specific validation: renames and extractions must be reference-complete — verify every call site, override, and string-based reference was updated.
- Provide a before/after verification plan: the specific inputs to run against both versions and the outputs that must match.
- Provide a behavior comparison: for each changed unit, the observable behaviors it had before and the evidence each is intact after.
- Provide regression validation: the existing tests that must pass, and the missing tests that should exist before this refactor ships.
- Provide integration verification: every external touchpoint — API consumers, database, events, files — and how to confirm each still sees identical behavior.
ASSUMPTIONS
- List every assumption made about the code's behavior, inputs, or environment.
- Mark each assumption VERIFIED (with its evidence) or UNVERIFIED (with the question that would resolve it).
- Any transformation that depends on an UNVERIFIED assumption must be flagged as conditional on it.
NON-GOALS
- Do not invent requirements.
- Do not redesign the entire system.
- Do not remove behavior without justification.
- Do not introduce new features.
- Separate assumptions clearly from facts.
- Flag uncertainty instead of resolving it silently.
- Explain the tradeoffs of any change that has them.
OUTPUT REQUIREMENTS
- Return the refactored code, complete — no elided sections, no "rest unchanged" placeholders inside changed units.
- Follow it with a change list: for each change — what changed, why it serves the goal, and its behavior impact (expected: none).
- Flag any change that could plausibly alter behavior, with the reason it might and the check that would confirm it does not.
- Where a worthwhile transformation was skipped for safety, list it under "Suggested but not applied" with what information would unlock it.