Rzemiosło · The Craft
What is The CraftLevelsGet startedChapters Search Download PLEN
Rules of chapter 02
Level 3 · Codex Chapter deep-dive

In the core the rules are short and hard. Here is what stands behind them: the why, examples and stories from real projects. Rule quotes come straight from the core.

02 — Skills and refactoring

Chapter 02 joins two things that at first glance have little in common: skills (written-down procedures) and refactoring (tidying code). The common denominator is repeatability and predictability — a skill makes a routine look the same every time, and refactoring discipline makes code after a change still look like the rest of the project. Below is what stands behind the rules.

A skill: “how we do it”, written down

Build a skill when ↗

A skill is a repeatable procedure wrapped in a single call — /run-project, /run-tests, /update-ai-readme, /add-migration. In the reference project they turned out to be leverage, because they codify the way of working: the assistant doesn’t guess how to start the server and which of the few dozen smoke checks to run; it follows a proven recipe.

Three signs it’s worth writing one:

  • Repetition. You’re doing the same thing for the third time — starting the server with checks, the full test run, adding another data source.
  • An order that’s easy to get wrong. A database Migration (database) — A controlled change to the database layout — adding a column, a table or moving data — step by step. Like a renovation to plan: rebuilding data in a set order so nothing “collapses”. more in the glossary → has five steps in a strict sequence; skipping one (e.g. updating the directory map) only surfaces a week later.
  • A project-specific trap you have to remember every time — e.g. on Windows the encoding must be set to UTF-8 before running a script, and Jest tests must run one after another while a server runs in the background. The assistant doesn’t “remember” this between sessions; a skill does.

A skill for something you do once is just overhead — you’re writing a recipe nobody will use.

Contract and honesty

A skill has an output contract ↗

A good skill ends unambiguously: exit code 0 means “green”, and any other result says what failed. That lets the skill plug into a larger process (e.g. a pre-Deploy — Releasing a new version of the app “live”, where real people use it. It’s the moment a change reaches real people — which is why it happens only on an explicit “deploy”. more in the glossary → check), and nobody has to read a mile of logs to learn whether things are fine. It’s worth adding a list of traps for the platform and a short “when it’s NOT needed” section.

A skill that quietly deletes dead code or cleans out a directory is a landmine — trust in the automation ends the day something disappears that nobody asked to remove. So every destructive action leaves a trace in the output: “removed 3 unused functions: …”. It’s the same spirit as the honest reports of 03.

Skill vs hook

A skill is a recipe for the agent; a Hook — Automation in the tool’s configuration that always fires on a specific event (e.g. after a file edit). Anything that must happen “always after something” shouldn’t depend on the assistant remembering. more in the glossary → is automation run by the harness. ↗

This distinction saves a lot of frustration. A skill is run by the assistant — when you invoke it, or when it decides it fits the task. A hook is run by the tool (the harness, e.g. Claude Code), automatically, on a specific event — always, with nothing to remember. A request like “from now on, run the formatter after every edit” saved in memory or in a skill works sometimes; saved as a hook in the configuration — every time. If something should happen always after something, it’s a hook.

Refactoring: first understand why it is the way it is

Search for prior art first ↗

The golden rule of refactoring: write code that reads like the code next to it — comment density, naming and idiom matched to the surroundings. Consistency beats your preferences.

And before you rewrite anything — history. git log -S and git blame (05) often show that an “ugly” shape has a reason: compatibility with an older version, an edge case, an old decision from a specific Issue — A task or bug recorded in a tracker (e.g. GitHub Issues), with a description and acceptance criteria. That’s where the “why” of a change lives — not in someone’s head or a chat that will disappear. more in the glossary →. A refactor that “beautifies” such code brings back a bug fixed a year ago.

One change at a time

Refactor separately from a behavior change ↗

A Commit — A saved, labelled point in the project’s history — a confirmation of a specific batch of changes. Small, described commits make a history you can return to any working state from. more in the glossary → that both tidies code and changes its behaviour is hard to review and even harder to Rollback — Reverting a change to the previous, working state — “Ctrl+Z” for a deployment. When a new version breaks production, a rollback restores the previous one in seconds instead of fixing in a panic. more in the glossary →: when something breaks, nobody knows whether the cleanup or the new feature is to blame. One commit is either a cleanup (behaviour unchanged, tests green before and after) or a new feature.

Reuse before rewrite ↗

Before you write yet another function that normalises, matches or saves a price — check whether it already exists. Two versions of the same logic drift apart at the first fix someone makes to only one of them. Duplication is debt with interest.

Don’t break other people’s entry points

Keep old entry points working ↗

Moving modules is a common source of silent failures: something — a cron script, another module, the documentation — still calls the old path. In the reference project, moving scripts into subpackages left thin “adapters” (shims) in the old places that re-export the new location; old calls kept working. The alternative is just as good: update every caller in the same change. Only the third way is bad — move things and hope nobody was calling them.

Small steps

Small verified steps: ↗

Refactor → tests green → commit, and again. A “big rewrite in one go” ends in a state where something broke but nobody knows what — and the only way out is to roll back everything. A series of small, tested steps gives a return point after each one. Refactoring a critical path (e.g. login) is done as a separate step, first secured by a test that pins the current behaviour (03).

SOLID without the cult

Apply SOLID — Five code design principles (single responsibility, open for extension, substitutability, narrow interfaces, dependency inversion). They help keep the next change cheap — applied in proportion, not as a cult. more in the glossary → proportionally: ↗

SOLID isn’t dogma but five tests of whether the next change will be cheap:

  • Single responsibility — one module, one reason to change. A function you have to touch for three unrelated reasons is worth splitting into three. It’s the same discipline as “one commit = one thing”.
  • Open for extension — a new path is added alongside, behind a switch (a flag), instead of rewriting a proven Branch — A separate working version of the project where you change something on the side, without touching what already works. On a branch you experiment safely; only what’s been checked reaches the main version. more in the glossary → live (12).
  • Substitutability (Liskov) — a specific version must keep the promises of the general one. If it breaks the caller’s assumptions, it isn’t a subtype but a trap.
  • Narrow interfaces — code shouldn’t depend on methods it doesn’t use.
  • Dependency inversion — logic receives the database or queue “across a boundary” and doesn’t know the provider by name; that makes it swappable and testable (03).

Apply them in proportion to the risk. Five layers and a factory of factories for a three-field form isn’t engineering, it’s cost (12: don’t overengineer).

no abstraction before the second concrete case. ↗

An interface invented from a single use guesses the future — and usually guesses wrong, after which every new case has to be squeezed into it. First a second concrete case, only then a shared contract drawn from what both cases really have in common.

Two kinds of review

Two review modes, never mixed: ↗

A “simplify” review (can it be clearer, is something repeated, can it be faster) and a “find the bug” review (adversarially hunting for what will break) have different goals and a different confidence bar. Mixed, they give a review that neither simplifies properly nor hunts bugs properly: remarks about naming drown out the one real threat. Tools often keep them separate (e.g. separate commands for simplifying and for correctness review) — and that’s how they’re worth using.