Separate functional core and external boundaries
Separate functional core and external boundaries
A functional core computes decisions from explicit inputs. An imperative boundary obtains inputs and performs effects. For example, expiry can be calculated without reading the system clock. This makes business rules testable independently of external services.
package main
import ( "fmt" "time")
func expired(now, until time.Time) bool { return !now.Before(until) }func main() { until := time.Date(2025, 1, 1, 12, 0, 0, 0, time.UTC) fmt.Println(expired(until.Add(-time.Second), until), expired(until, until))}The deadline itself is expired. Passing now allows exact boundary tests without sleep. A real caller can pass time.Now(); the test controls the input. Avoid hidden clocks in business rules.
Expected output:
false trueCompute first, write second
package main
import ( "bytes" "fmt" "io")
func shipping(total int) int { if total >= 5000 { return 0 } return 500}func receipt(w io.Writer, total int) error { _, err := fmt.Fprintf(w, "shipping=%d\n", shipping(total)) return err}func main() { var b bytes.Buffer if err := receipt(&b, 4999); err != nil { panic(err) } if err := receipt(&b, 5000); err != nil { panic(err) } fmt.Print(b.String())}Amounts are nonnegative integer cents. shipping computes the fee while receipt handles output and propagates write failures. Test both sides of the threshold. A pure calculation does not make an entire workflow atomic.
Expected output:
shipping=500shipping=0Try it locally
Write discount(total int) int returning 200 cents for totals of at least 3000 cents, otherwise zero. Assume nonnegative totals; print results at 2999, 3000 and 3001.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Professional architecture 04: Separate functional core and external boundaries
Check the lesson objective before moving on.
Why pass current time into expired?
What does a pure shipping function guarantee?