Functional core and imperative boundaries
Quick answer
A functional core computes decisions from explicit data; an imperative boundary performs effects such as reading input, displaying output or persisting results.
How it works
Design a core function with a clear input and output contract. Instead of directly logging or writing data, return a decision or a new state. A boundary validates input, invokes the core and interprets its result. For expected failures, a tagged result such as {ok:false, reason:"range"} makes the control path explicit. Test the core without UI, time or network dependencies and test the boundary separately.
function adjust(n,d){return n+d<0?{ok:false}:{ok:true,value:n+d};}console.log(JSON.stringify(adjust(4,-2)),JSON.stringify(adjust(1,-2)));Expected output in Node.js:
{"ok":true,"value":2} {"ok":false}A tagged result communicates the decision without performing an effect.
const core=n=>n*2;const out=[];function run(n,write){write(core(n));}run(3,x=>out.push(x));console.log(JSON.stringify(out));Expected output in Node.js:
[6]The boundary receives an injectable output function.
Common mistakes
Do not move effects into a supposedly pure callback. Avoid coupling the decision to one output medium. Functional architecture does not eliminate the need for integration tests.
Practice
Write a pure stock adjustment decision and a boundary that prints its result. Test rejection of negative stock independently.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Functional core and imperative boundaries — Knowledge Check
Reason about the protocol or programming technique and predict the example output.
Where should displaying a computed result normally occur in this design?
How can an expected business failure be represented by the core?
Does testing the functional core remove the need for boundary tests?
What is the exact console output in Functional core and imperative boundaries example 1?
function adjust(n,d){return n+d<0?{ok:false}:{ok:true,value:n+d};}console.log(JSON.stringify(adjust(4,-2)),JSON.stringify(adjust(1,-2)));What is the exact console output in Functional core and imperative boundaries example 2?
const core=n=>n*2;const out=[];function run(n,write){write(core(n));}run(3,x=>out.push(x));console.log(JSON.stringify(out));