ShtukbaseStandard

Code: the pragmatic programmer

code-pragmatic · version 0.1.0
Code5 norms3 checks

The school of Hunt and Thomas ("The Pragmatic Programmer", 1999; ed. 2019): DRY as one piece of knowledge in one place, orthogonality, no broken windows, tracer bullets, crash early; three heuristics and judges.

About the package

The school. Craft is habits. Every piece of knowledge in the system is written once. The parts of the system are independent, and a change to one does not touch the others. Small decay is fixed at once, otherwise it invites more. New things are built as a thin end-to-end path and then grown.

The authors. Andrew Hunt and David Thomas. Source: Andrew Hunt, David Thomas, "The Pragmatic Programmer", Addison-Wesley, 1999; the 20th anniversary edition is 2019. The norms below retell the ideas in our own words; they are not quotes.

When to take it. Any project where the code lives longer than a week and is changed by several people or agents.

When not to take it. Learning and throwaway sketches, where the code will be discarded.

Norms

NormWhat holdsLevelEnforcer
PRG-R01One piece of knowledge, one placeSHOULDcheck repeated-literals and judge judges/dry-knowledge.md
PRG-R02Modules are independent: no import cyclesSHOULDcheck import-cycles
PRG-R03No broken windowsSHOULDcheck broken-windows
PRG-R04New things as a thin end-to-end pathMAYjudge judges/tracer-bullets.md
PRG-R05The program checks its preconditions and crashes earlySHOULDjudge judges/crash-early.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-pragmatic --dir <project>
shtuk check --dir <project>
shtuk run code-pragmatic review                 # a prompt for the reviewer over the uncommitted change
shtuk run code-pragmatic review --diff main..HEAD
shtuk run code-pragmatic 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-pragmatic.md:

KeyDefaultWhat it means
minLength16length of a string literal whose repetition is counted (PRG-R01)
minFiles3in how many files a repetition is already a finding (PRG-R01)
commentedLines3lines of commented-out code in a row (PRG-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, 3 judges.

Norms 5

IDNormLevelModeEnforcer
PRG-R01
Every piece of knowledge (a path, a key, a format, a rule, a number with a meaning) is written in the system once; the same string literal is not repeated in minFiles files (3).
SHOULD observes
sensor
PRG-R02
Modules do not import each other in a circle: dependencies go in one direction.
SHOULD observes
sensor
PRG-R03
The code has no TODO, FIXME, HACK or XXX without a task and no commented-out code.
SHOULD observes
sensor
PRG-R04
A new capability is first run as a thin path through all layers (input, logic, storage, output) and then grown; a whole layer is not built before the path works.
MAY observes
judge
PRG-R05
Code checks at the entry whatever it cannot work without, and on a violation stops with a clear message instead of going on with bad data.
SHOULD 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
repeated-literalsOne piece of knowledge, one place
norm PRG-R01
checks/repeated_literals.ts
check
import-cyclesModules are independent: no import cycles
norm PRG-R02
checks/import_cycles.ts
check
broken-windowsNo broken windows
norm PRG-R03
checks/broken_windows.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-pragmatic <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-pragmatic/; actions run from there.