1---2name: architecture-paradigm-functional-core-33description: Functional Core, Imperative Shell: isolate deterministic logic from side effects for testability. Triggers: functional core, imperative shell, pure functions, testability Use when: business logic is entangled with I/O or tests are brittle DO NOT use when: simple scripting without complex logic.4---56# The Functional Core, Imperative Shell Paradigm78## When to Employ This Paradigm9- When business logic is entangled with I/O operations (e.g., database calls, HTTP requests), making tests brittle and slow.10- When significant development time is spent rewriting adapters or dealing with framework churn.11- When you require a suite of fast, deterministic unit tests that operate on plain data, complemented by a thin integration testing layer.1213## Adoption Steps141. **Inventory Side Effects**: Create a map of all side effects in the system, such as database writes, external API calls, UI events, and filesystem access. Explicitly assign these responsibilities to the "shell."152. **Model the Core Logic**: Represent business rules and policies as pure functions. These functions should take domain data as input and return decisions or commands as output, avoiding shared mutable state.163. **Design the Command Schema**: Define a small, explicit set of command objects that the core can return and the shell can interpret (e.g., `PersistOrder`, `PublishEvent`, `NotifyUser`).174. **Refactor Incrementally**: Begin with high-churn or critical modules. Wrap legacy imperative code behind adapters while progressively extracting pure calculations into the functional core.185. **Enforce Boundaries**: Use code reviews and automated architecture tests to validate a strict separation. The shell should only handle orchestration, sequencing, and retries, while the core should never call directly into frameworks or I/O libraries.1920## Key Deliverables21- An Architecture Decision Record (ADR) detailing why this pattern was chosen, which modules are affected, and the scope of the migration.22- A suite of unit tests for the core with high (>90%) and deterministic code coverage. Where applicable, use property-based or fixture-based testing to cover a wide range of inputs.23- A suite of contract and integration tests for the shell that verify correct command interpretation, retry logic, and telemetry.24- A set of rollout metrics (e.g., deployment lead time, incident rate in the shell layer) to demonstrate the value of the architectural change.2526## Risks & Mitigations27- **Logic Drifting Between Core and Shell**:28 - **Mitigation**: It's common for business logic to accidentally be duplicated or placed in the shell. Enforce a "core owns all decisions" checklist during code reviews to prevent this.29- **Mismatch with Frameworks**:30 - **Mitigation**: The imperative shell may still need to interact with framework-specific lifecycle hooks. Before committing to a large rewrite, build small proof-of-concept adapters to validate the integration strategy.31- **Team Unfamiliarity with the Pattern**:32 - **Mitigation**: Introduce the pattern using pair programming and internal "brown-bag" learning sessions. Document common anti-patterns that are discovered during the pilot phase to guide future development.