0

Inline First: a binding has to say something the expression doesn't

preview

I see const userName = user.name; followed by exactly one use of userName. The name says nothing that user.name hadn’t already said. It’s a hop the reader makes for nothing.

Fowler already had the answer: Inline Variable is the documented inverse of Extract Variable. Both moves live on the same continuum and most people only ever walk one way. Extract when the expression hides intent. Inline when the binding adds nothing the expression didn’t already say. The failure isn’t that the extract happened. It’s that the move never runs in reverse.

The other end of the continuum is the case I don’t want inlined. A function that retries three times and waits 5000 milliseconds between attempts reads worse than the same function with MAX_RETRIES = 3 and BACKOFF_MS = 5000. The constant earns its name because it carries information the literal doesn’t. That’s the test. It cuts both ways.

The trouble is that inlining never gets proposed. Extraction has a lint rule, a keyboard shortcut and a refactoring menu item behind it. Inlining has a name in a catalogue and no muscle memory. So the bindings accumulate, each one individually defensible. The function slowly fills with nouns that point at other nouns.

Agents make it worse, and in a way I hadn’t expected. Handed a file full of these bindings, an agent reads the names as the vocabulary and builds on them, so a binding that never earned its existence becomes the handle for everything downstream. It copies the surface it was given. The surface is a list of names.

Wrote the longer version up here.

prickles.org/tenet/inline-first/S5

I see const userName = user.name; followed by exactly one use of userName. The name says nothing that user.name hadn’t already said. It’s a hop the reader makes for nothing.

Fowler already had the answer: Inline Variable is the documented inverse of Extract Variable. Both moves live on the same continuum and most people only ever walk one way. Extract when the expression hides intent. Inline when the binding adds nothing the expression didn’t already say. The failure isn’t that the extract happened. It’s that the move never runs in reverse.

The other end of the continuum is the case I don’t want inlined. A function that retries three times and waits 5000 milliseconds between attempts reads worse than the same function with MAX_RETRIES = 3 and BACKOFF_MS = 5000. The constant earns its name because it carries information the literal doesn’t. That’s the test. It cuts both ways.

The trouble is that inlining never gets proposed. Extraction has a lint rule, a keyboard shortcut and a refactoring menu item behind it. Inlining has a name in a catalogue and no muscle memory. So the bindings accumulate, each one individually defensible. The function slowly fills with nouns that point at other nouns.

Agents make it worse, and in a way I hadn’t expected. Handed a file full of these bindings, an agent reads the names as the vocabulary and builds on them, so a binding that never earned its existence becomes the handle for everything downstream. It copies the surface it was given. The surface is a list of names.

Wrote the longer version up here.

prickles.org/tenet/inline-first/S5

0

Guard Clauses: the preconditions are the point of the function

preview

I wrote up the guard clause pattern because most functions I read still have the arrow shape. The code works, so nobody touches it.

Guard clauses are about what the body is allowed to assume. A function that checks null, type and permission at the top and returns early on each has told you three facts before you read a line of the work. A function that nests the same three checks has told you nothing until you’ve counted the closing braces at the bottom.

That’s the operational part. Once the checks sit at the top as separate returns, a reviewer can argue with the assumption itself. Is this function really the right place to enforce permission? Should that null be possible at all? Buried four levels deep, nobody asks.

The pyramid is a hidden preconditions problem.

prickles.org/tenet/guard-clauses/S4

I wrote up the guard clause pattern because most functions I read still have the arrow shape. The code works, so nobody touches it.

Guard clauses are about what the body is allowed to assume. A function that checks null, type and permission at the top and returns early on each has told you three facts before you read a line of the work. A function that nests the same three checks has told you nothing until you’ve counted the closing braces at the bottom.

That’s the operational part. Once the checks sit at the top as separate returns, a reviewer can argue with the assumption itself. Is this function really the right place to enforce permission? Should that null be possible at all? Buried four levels deep, nobody asks.

The pyramid is a hidden preconditions problem.

prickles.org/tenet/guard-clauses/S4