Contents · entry I
Notes on building small tools
Most of what a tool needs to do fits on one screen. These are notes on building tools that small, and on keeping them that way.
A small tool is one you can hold in your head. You can read its whole source in a sitting, predict what it will do with a strange input, and delete it without a meeting. That last property matters more than it sounds: a tool you can delete is a tool you can afford to try.
Most tools do not start large. They grow one reasonable request at a time, until the help text scrolls and nobody remembers which flag the nightly job depends on. The cure is not discipline in the abstract. It is a short, written list of things the tool will never do.
Two or three jobs
Two or three jobs is a good number for a tool. One to do the work, one to explain itself when you run it with no arguments, and one to fail in a way that tells you exactly which line to look at.1
What a small tool refuses to do
The design work is mostly saying no. Before writing any code, write the refusals down:
- It does one job, and names that job in the first line of its help text.
- It reads plain text and writes plain text, so other tools can stand on either side of it.
- It keeps no hidden state. Everything it knows is in a file you can open.
- It can be deleted in an afternoon, along with everything it created.
A worked example
Here is the kind of thing meant by small. It reads a folder of markdown files and prints a reading time for each.
import { readdir, readFile } from "node:fs/promises";
const dir = process.argv[2] ?? "posts";
const files = (await readdir(dir)).filter((f) => f.endsWith(".md"));
for (const file of files) {
const text = await readFile(`${dir}/${file}`, "utf8");
const words = text.split(/\s+/).filter(Boolean).length;
process.stdout.write(`${Math.ceil(words / 220)} min ${file}\n`);
}
It has no options, no configuration file and no dependencies outside the standard library. When it starts to need an option, that is usually a sign that it is becoming a second tool.2
Keeping the accounts short
- Write the refusals before the code.
- Ship the first version the same day, even if all it does is print.
- Read the whole source again a week later. If you cannot, it is no longer small.
None of this is new, and that is the comfort of it.