Refactor Nested Procedural Code into Pure Pipelines

Transforms complex, deeply nested imperative code into clean, testable pure functions organized as a sequential data pipeline.

Agent prompt for ChatGPT and Claude

Prompt

Prompt
Act as a Senior Software Engineer specializing in functional programming. I will provide a block of procedural code. Refactor it into a series of pure, single-responsibility functions that form a data pipeline. Follow these rules: 1. Remove all side effects from the logic; keep I/O at the boundaries. 2. Ensure every function is immutable and deterministic. 3. Provide a clear Type Signature comparison table showing the input and output types before and after the refactor. 4. Use a clear composition style to show how the functions link together. Output format: # Refactor Summary, ## Type Signature Comparison (Table), ## Refactored Pipeline Code, ## Implementation Notes. This is not a substitute for formal code review; consult your lead engineer for architectural alignment.

Example output

Original vs refactored function signaturesInput and output types before and after the refactor
ItemOriginalRefactored
Input typeList[Dict]List[User]
Output typeNone (Side-effect)List[Invoice]

Example for: a legacy Python function processing nested user billing data.

Refactor Summary

The original monolithic function contained three levels of nested loops and conditional state mutation. The refactor extracts logic into pure, testable units: filter_active_subscriptions, calculate_tax, and format_invoice_data.

Type Signature Comparison

VersionInput TypeOutput Type
OriginalList[Dict]None (Side-effect)
RefactoredList[User]List[Invoice]

Refactored Pipeline Code

def get_active_users(data: List[Dict]) -> List[User]:
    return [User(**u) for u in data if u.get('active')]

def calculate_tax(users: List[User], rate: float) -> List[Invoice]:
    return [Invoice(user=u, total=u.amount * (1 + rate)) for u in users]

def process_billing_pipeline(raw_data: List[Dict], tax_rate: float) -> List[Invoice]:
    # Pipeline composition
    return calculate_tax(get_active_users(raw_data), tax_rate)

Implementation Notes

  • The pipeline is now fully deterministic; given the same input, it returns the same output.
  • Side effects like database writes or API calls should be handled by a caller function wrapping this pipeline.
  • Type hints are added to ensure compatibility with static analysis tools like MyPy.
Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Forces the model to strip away side effects, making code easier to test.
  • The required type signature table highlights the shift from loose dicts to strict schemas.
  • Pipeline composition creates a readable flow that replaces deeply nested if-else blocks.

Use this with GitHub Copilot or Claude to clean up legacy spaghetti code or monolithic functions. Paste your nested function after the prompt. It works best for data transformation logic in languages like TypeScript, Python, or Rust.

Common edits

  • Adjust the naming conventions to match your existing codebase style.
  • Add specific library requirements like Pydantic or Zod if you need runtime schema validation.

Reuse it: Use in a workflowAdapt in the builder

Prompt details

By
Prompts-GPT editors
Published
How prompts are written and checked

Did this prompt work?

Refactor Monolithic God Classes into Clean Services

Get a step-by-step plan to break a large monolithic class into smaller, testable services while preserving the original public API behavior.

Coding

Refactor nested legacy loops into flat readable helper functions

Decompose deeply nested legacy loops into single-purpose functions. Use this when reviewing complex procedural code that is hard to test or debug.

Coding

Plan an incremental refactor for legacy code

Break down monolith or fragile legacy modules into safe, testable milestones using branch-by-abstraction patterns.

Coding

Refactor Pasted Code for Readability Without Changing Its Behavior

Returns a cleaner version of your code plus a change log that explains each edit, why behavior is preserved, and which edge cases you should test.

Perform Static Call Graph Trace Before Modifying Code

Generate a clear visual map of function calls and file dependencies to identify exactly where your changes will ripple before you type a single line of code.

Coding

Port a Function or Module to Another Language With Tests Proving Same Behavior

Lists the source code's behaviors and edge cases, writes tests in the target language first, ports the code idiomatically, and gives a table of library mappings and semantics that differ.