ShtukbaseStandard

Code: a pure core, effects at the edges

code-functional-core · version 0.1.0
Code5 norms3 checks

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.

About the package

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.

Norms 5

IDNormLevelModeEnforcer
FC-R01
The core files (the core field in settings) do no input or output and do not read time, chance or the environment.
SHOULD observes
sensor
FC-R02
A core file imports only core files and third-party libraries, not the project's shell files.
SHOULD observes
sensor
FC-R03
Core functions do not change their parameters: they return a new value.
MAY observes
sensor
FC-R04
Branching, calculations and rules live in the core; the shell only reads the world, calls the core and writes the result, and has little branching.
SHOULD observes
judge
FC-R05
Tests of the core call a function with values and compare the result; stubs and substitutions are needed only by tests of the shell.
MAY observes
judge

"Blocks": the enforcer stops a violation. "Observes": it only notes it. Level MUST means required; SHOULD means yes by default.

Actions 4

NameWhat it doesFileWhen
effects-in-coreThe core has no effects
norm FC-R01
checks/effects_in_core.ts
check
core-importsDependencies point inward
norm FC-R02
checks/core_imports.ts
check
core-mutationThe core does not change what it is given
norm FC-R03
checks/core_mutation.ts
check
reviewA prompt for a reviewing agent over the package norms: by diff or by filesactions/review.ts
shtuk run

shtuk check runs the checks; shtuk run code-functional-core <name> runs any action. "After install" actions run by themselves only if the package comes from a trusted source.

Pages 6

They go into the project at these paths.

Package files 8

They go into .shtukbase/packages/code-functional-core/; actions run from there.