Frequently Asked Questions About ArjanCodes Functional Core Python Design

22 answers covering everything from basics to advanced usage.

// Basics

What does 'separation of what from where' mean in this pattern?

It means your core logic must not know where data came from or where decisions will be stored. The 'what' — your fraud rules, pricing calculations, or quality scoring — stays pure and ignorant of its infrastructure. The 'where' — databases, APIs, file systems — lives entirely in the imperative shell. This keeps domain logic stable even as infrastructure changes underneath it.

What is the difference between a side effect and a pure calculation?

A pure calculation only reads its arguments and returns a value, affecting nothing outside itself. A side effect is any action that affects the world outside the function — saving to a database, sending a notification, writing a file, or mutating an argument. The goal isn't to eliminate side effects but to isolate them clearly in the imperative shell.

Why are frozen dataclasses recommended for pipeline stages?

Frozen dataclasses cannot be changed after creation, so they prevent any pipeline step from secretly mutating data that another step depends on. Each one represents one stable snapshot of data rather than an object that gets mutated as processing continues. This makes program flow obvious and means you never have to understand an object's entire history to know its current state.

What are explicit dependencies and why do they matter?

Explicit dependencies means passing everything a function needs as arguments rather than letting it fetch external state silently. If a calculation needs a config value or a model prediction, you pass it in instead of fetching it inside the function. This makes dependencies visible, functions independently understandable, and eliminates hidden coupling that breaks tests and surprises readers.

// How To

How do I audit a function to find its mixed concerns?

List every distinct thing the function does, then categorize each into calculation/rule, state mutation, external I/O, or decision-making. Any function doing more than one category is a refactor candidate. This audit reveals exactly which parts belong in the pure core and which belong in the imperative shell before you write a single line of new code.

How do I extract a pure function from existing code?

Pull each calculation or rule into its own named function that takes only what it needs as arguments, returns a value, and touches no external state. Then ask: if I call this with the same inputs twice, do I always get the same output? If yes, it qualifies as pure. Repeat for every rule until your decision logic is a set of pure functions.

How do I compose the pipeline stages into one orchestrator?

Write a top-level function that calls each stage function in sequence, passing each output as the next input. Use intermediate variables for readability and debuggability — this is preferred in Python over deeply nested composition or compose operators, which are more idiomatic in Haskell. The orchestrator stays pure; it only coordinates transformations, never fetches or persists.

How do I handle a stage that needs a machine learning prediction?

Pass the prediction into the stage function as an argument rather than fetching it inside. The imperative shell loads the model and generates the prediction, then hands the result to the pure core. This keeps the core ignorant of the model's infrastructure and lets you test the scoring logic with any prediction value, no model loading required.

How do I build the imperative shell around my functional core?

Create a service or handler layer that retrieves raw data from external sources, prepares any external inputs the core needs like model predictions or config, calls the orchestrator function, then persists or dispatches the result. The shell is allowed to be imperative — that's intentional. The core must remain ignorant that the shell exists.

// Troubleshooting

My core function still needs a database mock to test — what went wrong?

If you need to mock a database, notification system, or API to test a core function, an external dependency has leaked into your core and must be refactored. Find the hidden I/O call or silent fetch, extract it to the imperative shell, and pass the data it produced into the core as an argument. A properly separated core needs only plain values to test.

Why is my refactored object in an invalid state halfway through processing?

You're mutating the same object across multiple processing steps, which means it passes through invalid intermediate states and you must understand its entire history to know its current state. Fix this by using immutable frozen dataclasses for each stage, so each transformation returns a new, complete, valid snapshot instead of partially mutating a shared object.

My functional code feels harder to read than before — what happened?

You've likely used deeply nested function composition syntax to look functional. In Python, explicit intermediate variables are more readable and debuggable than compose operators or deep nesting, and they produce the exact same design. Rewrite the pipeline as a sequence of named intermediate variables in the orchestrator function to restore clarity.

My immutable refactor is now slow because of constant copying — is that normal?

That's a sign you've forced immutability everywhere regardless of cost, which is a known pitfall. If you're constantly copying huge data structures just to stay pure, that's not a good trade-off in Python. Apply immutability where it makes the flow easier to reason about and relax it for large hot-path structures where performance matters more than theoretical purity.

// Comparisons

How does this compare to just using more classes and dependency injection?

Dependency injection makes dependencies explicit but still typically lets methods perform I/O and mutate state, so you keep mocking to test. Functional Core, Imperative Shell goes further: it makes the core entirely pure and side-effect-free, so you test logic with simple values and no mocks at all. DI and functional core can coexist — the shell often uses injection to supply external services.

How does a transformation pipeline compare to procedural step-by-step code?

Procedural code often mutates one object through many steps, mixing I/O and calculation, so you must trace the whole flow to understand any value. A transformation pipeline produces a fresh immutable snapshot at each named stage, so every value is complete and valid on its own. This makes debugging, testing, and reasoning about data flow dramatically easier.

How does functional core design compare to hexagonal architecture?

They share the goal of isolating domain logic from infrastructure, and the imperative shell plays a role similar to hexagonal's adapters. The difference is emphasis: functional core insists the domain logic be genuinely pure and immutable, not just interface-isolated. You can view Functional Core, Imperative Shell as a lightweight, function-first expression of the same separation hexagonal architecture pursues with ports and adapters.

Should I use this pattern for simple CRUD functions?

Usually not. If a function is a thin wrapper that only reads and writes data with little decision-making, there's no meaningful functional core to extract. This pattern pays off when functions contain real domain logic — rules, scoring, eligibility, pricing — that's worth testing in isolation. Applying it to trivial CRUD adds structure without benefit.

// Advanced

How do I apply this to a long-lived process with lots of state?

Model each state transition as a pure transformation that takes the current immutable state plus an event or input and returns a new immutable state. The imperative shell holds the mutable reference and persists it, while every rule about how state evolves lives in pure functions. This keeps complex stateful logic fully testable without simulating the whole runtime.

Can I use this pattern alongside AI-generated code?

Yes, but only if you understand the principles yourself. Don't assume AI-generated code embodies good design decisions — it often mixes concerns and hides dependencies. Use this framework as the standard you hold generated code to: check whether calculations are pure, dependencies explicit, and I/O confined to a shell. If you can't judge these yourself, you can't guide what your AI tool produces.

How do I name my pipeline stages and data types well?

Use your domain_context to name stages after the meaningful states data passes through, such as raw order → validated features → pricing assessment → order decision. Each stage maps to a clearly named frozen dataclass like PricingAssessment or Disposition. Good names make the pipeline self-documenting and signal exactly what has been computed at each point.

Where should logging and metrics go in this architecture?

Logging and metrics are side effects, so they belong in the imperative shell, not inside core calculation functions. Let the core return rich immutable results — including any flags or reasons for a decision — and have the shell log and emit metrics based on those returned values. This keeps the core pure and still gives you full observability at the edges.

What external dependencies should I list before starting?

List every external system the code touches: databases, APIs, ML models, file systems, notification services, and config sources. Each one is a candidate to pull into the imperative shell. Identifying them up front tells you which inputs the core will need passed in as arguments and which operations the shell must perform before and after calling the core.