The concept of "Functional Core, Imperative Shell," coined by Gary Bernhardt fourteen years ago, divides application code into two main parts. The Functional Core handles business logic without IO or destructive state updates, while the Imperative Shell manages state, external dependencies, and interactions with the outside world.
The Functional Core is designed to be purely functional, meaning the same inputs consistently produce the same outputs. This isolation makes it highly amenable to testing, as there are no external dependencies to mock or stub. The Imperative Shell, conversely, has less complex logic but is responsible for coordinating with external systems like databases or APIs.
The article argues that the key property making pure functions testable is determinism, not necessarily pure functional programming itself. Determinism ensures that given a specific sequence of inputs, the system's behavior is repeatable and predictable. This allows for reliable testing of the core's logic.
The author demonstrates that state machines can also provide deterministic behavior, similar to pure functions. An example of an `AddMachine` shows that even with imperative state updates, a consistent sequence of calls to its `transition` function will always result in the same final state. This suggests that state machines can serve as deterministic cores, offering the same testability benefits where pure functional approaches might be impractical due to language or performance constraints.
✨ This summary was generated by AI from the outlets' reporting listed below. It is not independently verified and may contain errors — check the original sources. How BrevFeed works →
One email each morning: the day's tech stories, clustered across outlets and summarized. No account needed.
One email a day. Unsubscribe in one click, any time.
Spend a few minutes, get the whole day. Every topic's top stories in one hands-free rundown — listen, watch, or read the transcript.
▶ Play today's briefNew every morning, and the back catalogue is archived by date.
This article re-examines Gary Bernhardt's "Functional Core, Imperative Shell" architecture, proposing that the core can be deterministic even if not purely functional. It suggests that state machines can achieve the same testability benefits as pure functions for the core component. This perspective offers an alternative for structuring applications where pure functional programming might not be feasible.