Skip to content

Implicit by default, explicit by exception

State the decisions. Let the system work out the facts.

Joshua Jordan
6 min read
  • Software design
  • Defaults
  • Configuration
The title Implicit by default, explicit by exception, with the MO Data logo above it.
In This Article

Two places knowledge can come from

Every system needs to know things. The type of a value. The port to listen on. Which fields to save. For each one, the knowledge has to come from somewhere. A person can write it down, or the system can work it out.

Explicit
A person writes it down. You can see it, and it says exactly what they meant. Someone also has to keep it true.Costs upkeep
Implicit
The system works it out. There's nothing to maintain. It can also work it out wrong without telling you.Costs trust

Years ago I left a note in a project's TODO file. It said most of my design choices came down to picking between these two, and that I'd write it up properly one day. This is that write-up.

My position is the title. Work it out by default. Write it down when you have a reason. The rest of this post is about what counts as a reason.

Facts and decisions

Start with the smallest case there is.

Copy
const count: number = 0;const count = 0;

The annotation on the first line tells you nothing the zero didn't already say. It's a fact about the code, written twice. Change the value to "none" and you have two places to edit.

Now a different line.

Copy
const port = 8080;

No tool could have worked that out. Someone picked 8080, and it wasn't true until they typed it.

State decisions. Derive facts.

When a file mixes the two, a reader can't tell them apart.

server.tsWrap linesCopy
createServer({ host: "localhost", port: 8080, timeout: 30000, retries: 3 });
createServer({ port: 8080 });

Which values in the first call did someone choose? You can't know. In the second call the one decision is the only thing on the line. Writing less told the reader more.

There's a second cost. Every fact you write down by hand is a copy, and copies go stale. Nobody updates the list of columns when the file gains a column. The copy stays in the config, wrong, and looks as trustworthy as the day it was typed.

The thing nobody mentioned

This is the part that took me longest to see. Explicit and implicit designs behave the same until something turns up that nobody wrote a line about. A new field in the data. A new file in the folder.

A list of what's allowed leaves the newcomer out. A list of what's blocked lets it in. Neither list mentions it, and both have already decided.

A new field called phone arrives

What you wrote

allow: ["name", "email", "total"]

Gets in

nameemailtotal

Left out

internal_notephonenew

phone was left out. Nothing failed and nobody chose that. You find out when someone asks where the phone numbers went.

Leaving something out is cheap when you'll notice soon and can go back for it. Letting something in is cheap when the thing is harmless. Most of the skill is knowing which of those you're dealing with.

When a guess goes wrong

Implicit has a bad reputation, and it earned it. Here are three guesses that have bitten real people. Predict each result before you reveal it.

What do you get back?
  • A spreadsheet cell

    SEPT2
  • A YAML 1.1 file

    country: NO
  • JavaScript

    [10, 9, 1].sort()

Look at how each one fails. The spreadsheet throws away what you typed, so you can't get it back. Almost nobody can predict the YAML rule. The sort gives no sign that it chose anything.

That gives me a test. A default is safe to leave implicit when it passes all four.

TestWhat it means
PredictableYou could say what it will do before it does it.
VisibleYou can see what it chose.
OverridableOne line changes it.
Cheap when wrongA bad guess costs a minute and loses nothing.

Where to stop guessing

There are three places where I write things down however well the system could guess.

At the boundary

When input comes from people you don't control, name what you accept.

routes/profile.tsCopy
// Saves whatever the request sends, including "is_admin": true.db.users.update(id, req.body);
// Saves two named fields. Everything else is ignored.const { name, email } = req.body;db.users.update(id, { name, email });

The first version never needs maintaining, and that's the problem. Inside your own system, the thing nobody mentioned is usually a colleague's new field. At the boundary it's whatever a stranger felt like sending.

Before anything you can't undo

Deleting, sending, paying, deploying. The system can work out what it would do. Then it should show you and wait.

TerminalCopy
$ cleanup --older-than 30dWould delete 214 files (1.2 GB).Run again with --yes to delete them.
An illustrative command, not a real tool.

When the evidence disagrees

A column holds 38 and 38.5, and then one day someone types n/a. There are two acceptable responses. Apply a written rule that gives the same answer every time, or stop and ask. The Zen of Python puts it as "In the face of ambiguity, refuse the temptation to guess."

Those three plus the fact-or-decision question make a short checklist. Try it on a few cases.

Should the system work this out?

Try a case

Is the answer already true somewhere the system can look?
Does the input come from your own side?
Is a wrong answer cheap to undo?
Can people see what was chosen?

Leave it implicit.It's a fact, it's yours, a mistake is cheap and anyone can see the result. Writing it by hand would only add a copy to maintain.

Implicit in, explicit out

The strongest objection is another line from the same poem, "Explicit is better than implicit." I agree with it. That line is about what a reader can see. So let the system do the working out, and have it write the result where people will look.

Package managers already work this way.

package.json and its lockfileCopy
// What you write. Intent, stated loosely."left-pad": "^1.3.0"
// What the tool writes back. The exact answer, saved and reviewed."left-pad": { "version": "1.3.0", "integrity": "sha512-..." }
Illustrative, not a real lockfile format.

You say roughly what you want. The tool resolves it and saves the exact answer in a file you can read and compare over time. The cleanup command above does the same thing when it prints what it would delete.

Other people got here first

None of this is new. Rails calls it convention over configuration. Go's proverb is "Make the zero value useful." Rico Mariani at Microsoft called it the pit of success, where the easy path and the correct path are the same path. Firewalls sit at the other end and deny everything they weren't told to allow. They're all drawing the same line in different places.

The short version

  • State decisions. Derive facts.
  • Every design has a default for what nobody mentioned. Pick the accident you can afford.
  • A safe default is predictable, visible, overridable and cheap when wrong.
  • At a boundary, before anything permanent, or when the evidence disagrees, write it down.
  • Implicit in, explicit out.

One question covers most of it. Before I add a setting, I ask whether a person had to decide it. If nobody did, it doesn't belong in the file.

Sources