shtukbasehub

Code: a pure core, effects at the edges

The school of Gary Bernhardt ("Functional Core, Imperative Shell", 2012) and Mark Seemann ("impureim sandwich", 2020): decisions live in a pure core, input and output in a thin shell; three heuristics over the named core and two judges.

Packagecode-functional-corev0.1.015 files

Code: a pure core, effects at the edges

The school. Decisions are made by a pure core: it receives values and returns values, with no time, chance or input and output. The shell is thin: it reads the world, calls the core and writes the result. The core is tested by simple tests without stubs, the shell by a few end-to-end ones.

The authors. Gary Bernhardt; Mark Seemann. Source: Gary Bernhardt, "Functional Core, Imperative Shell" (Destroy All Software, 2012) and the talk "Boundaries" (2012); Mark Seemann, "Impureim sandwich" (blog.ploeh.dk, 2020) and the book "Code That Fits in Your Head" (2021). The norms below retell the ideas in our own words; they are not quotes.

When to take it. Code with logic that is important to check: rules, calculations, merging, planning, parsing formats. It fits well with "pure cores and thin shells" in Rust crates.

When not to take it. Code that is all input and output (a driver, a thin client); there is nothing to move into a core.

Norms

NormWhat holdsLevelEnforcer
FC-R01The core has no effectsSHOULDcheck effects-in-core
FC-R02Dependencies point inwardSHOULDcheck core-imports
FC-R03The core does not change what it is givenMAYcheck core-mutation
FC-R04Decisions are in the core, the shell is thinSHOULDjudge judges/decisions-in-core.md
FC-R05The core is tested without stubsMAYjudge judges/core-tests.md

All norms are in observe mode: the checks print findings and pass, the judges are a procedure for the reviewer. A check you trust on your code is moved by the owner to mode: enforce in the norm's page, and then a finding fails shtuk check.

How to connect

shtuk install <path>/apps/standard/packages/code-functional-core --dir <project>
shtuk check --dir <project>
shtuk run code-functional-core review                 # a prompt for the reviewer over the uncommitted change
shtuk run code-functional-core review --diff main..HEAD
shtuk run code-functional-core review src/a.ts src/b.py

The package requires norms-core and code-lib (the shared code-parsing library and shared settings). It is installed on top of Basic.

Settings

Which code to read is the shared page /norms/code.md of the code-lib package: roots (roots), exclusions (exclude), languages, whether to read tests. This package's thresholds are the page /norms/code/code-functional-core.md:

KeyDefaultWhat it means
core[]paths of the pure core: folders or masks (FC-R01...R03); while empty, the checks stay silent
effects[]your own regular expressions of effects, in addition to the built-in ones (FC-R01)
rustMutRefsfalsecount a &mut parameter in Rust as changing what is given (FC-R03)

The same page has its own exclude list (path masks from the project root). One place is released by the comment code-allow: <check name> on its line or the line above.

Honestly about the checks

The checks are rough heuristics: regular expressions and bracket parsing for TypeScript/JavaScript, Python and Rust, with no parsers and no dependencies. They err both ways, which is why they stand in observe. The meaning of a norm is held by the judge; the heuristic only shows where to look. 3 checks, 2 judges.