Adding a payment channel to a project that’s been running for years: the business logic itself is only a few lines, but the rest of the time is spent finding all the places to touch: add an item to the channel enum, add an item to the gateway whitelist, add an item to the admin dropdown list, add an item to the frontend icon mapping, add a line to each of several test fixtures, and finally write a reference table for the ops team. What really slows me down isn’t difficulty, it’s that I first have to confirm “how many places are there exactly,” and even after confirming I’m not at ease, because searching for strings with grep will always miss something.
That day I wrote this down as my definition of maintainability: change one requirement, and the change lands in several places, and how long it takes me to know which places those are. It actually has little to do with whether the code is written cleanly. Clean is of course good, but a clean piece of code can still be scattered across eight files; conversely, some rather rough code, as long as the related logic is all piled into one function, is actually more reassuring to change.
How many places the change lands in
The term shotgun surgery has been around for a long time, but its cost is easily underestimated, because each individual place doesn’t look like much: just add a line. The problem is completeness. Miss one place and it’s a production incident, and whether you missed one depends on my memory and grep’s luck.
A different way of calculating makes it clearer: the real cost of a change is roughly “number of landing points × cost of changing each” plus “time spent confirming nothing was missed.” The first term is usually small; the second grows quickly with the number of landing points, and has no upper bound—you can’t prove you’ve found them all, you can only search repeatedly.
So now when I judge whether a piece of code is maintainable, I first look at whether it lets the compiler or type checker find all the landing points for me. Passing the same channel identifier, two ways of writing it have very different maintenance costs:
1 | def pay(order: dict, channel: str) -> dict: |
1 |
|
In the first way, channel is a bare string, and a "alipay" can be stuffed in anywhere. If the channel is renamed someday, no tool will remind me. The second way at least turns “which channels exist” into an object that can be imported and iterated; when adding a channel it forces me to handle the places where no branch is written. Python has no exhaustiveness checking, but match paired with a assert_never covers part of it.
There’s another kind of landing point that isn’t written in the code at all: configuration, switches, dropdown options in the admin backend. They have no type checking and don’t go through compilation; with luck you get an error at startup, without luck an option is silently missing and the user finds it first. My current approach is to let the value domain grow out of the code: the channel list is written in only one enum, and the yaml only holds parameters specific to that channel, such as fee rate, limits, settlement cycle, rather than copying the channel names again. The cost of copying is never this one time, it’s the next time.
I’m not saying everything should be typed. A temporary dict used inside a function is fine, quick to change, and not worth building a class for. My boundary is roughly two lines. One is “who will change it”: if only I use it and throw it away inside one function, I can remember it; once it will be consumed by other modules, it should have a name. The other is “will it go outside”: if this structure will be persisted to the database, go through an API, or be rendered by the frontend, then it’s already a contract, and a bare dict is a landmine for whoever comes later.
First findable, then fast to change
Sometimes there aren’t many landing points, but I can’t find them, and that costs even more time. I’ve seen a project where all handlers are registered by naming convention: the file name must be xxx_handler.py, it can go in any directory, and the framework scans once at startup. It’s indeed convenient when writing, no registry to maintain. But after I took over, to find “where is the user deregistration logic,” I could only search globally for handler, then open file names one by one and guess. What was saved was the ten minutes of writing the registration back then; what was paid was one navigation for every person who came later.
Naming conventions themselves aren’t the problem; the problem is that they make “what entry points exist” implicit. Implicit things aren’t nonexistent, it’s just that no one can state them clearly at once.
I now lean toward gathering entry points in one place: one directory each for routes, scheduled tasks, and message consumers, with the registry written out explicitly. Code not in these directories is all called upon, and doesn’t actively receive external events. This way a newcomer (including me six months later) can read down from the entry points, build a map first, then decide which branch to dig into. An explicit registry has a side effect: a new route shows up in the diff (code difference), rather than hiding in a file name.
This also has a cost. Explicit registration means adding something requires changing one more file, and occasionally you forget. I accept this cost, because an error from forgetting in the registry can be found at startup, while an error from forgetting in the scan convention only surfaces in production.
But I don’t intend to push it as a universal rule. A small service with three routes, a pile of one-off scripts, data processing that’s thrown away after running—maintaining a registry for them is a burden instead. Whether entry points are worth making explicit depends on whether this system will have a second person take over. As for whether the registry itself will become a new conflict point after dozens of modules, I don’t have a good answer yet; for now I can only split it smaller by business domain.
Error messages are written for the person six months later
No one looks at error messages when the code runs; when something goes wrong, they’re the only clue. I’ve seen too much of this:
1 | raise KeyError("status") |
The person who sees this log line half a month later needs to know which order, which config file, and which segment is missing. My current standard is for the error to answer three things as much as possible: which input, what was expected, where to change it. If it can’t do all three, at least leave a unique identifier that can be grepped, such as an error code or config file name. Spending thirty extra seconds when writing can save half an hour when troubleshooting.
Nor does every raise need to be written as a short essay. What really blocks troubleshooting is these kinds of places: across module boundaries, config loading, external callbacks—places no one looks at normally, and when a problem occurs only this one log line is left. For parameter validation inside a function, throwing casually is not a big deal.
An error code system sounds formal, but in a small team it easily fizzles out halfway: the first twenty interfaces carefully divide code segments, from the thirtieth on people write strings casually, and in the end no one dares rely on it. My current default is not to do global error codes, but to fix a set only where there’s a real external commitment, such as payment callbacks and open APIs; elsewhere it’s enough to ensure the error message carries the key identifier.
By the way, errors and logs are two different things. Errors are responsible for stating clearly what’s wrong; logs are responsible for reconstructing what happened before. Mixing them makes both unusable.
Code that can’t be deleted shows what it’s tangled with
No one stops you when adding features; only when deleting features can you see whether the structure is good: if taking a feature offline requires touching a dozen files and there are two tables no one dares to drop, it means it’s tangled with other things.
The cost of deletion is usually invisible, because it doesn’t produce new features. Adding a channel has a schedule; cleaning up an old channel doesn’t, so it’s always pushed to the back, piling up until no one dares touch it.
I have a crude method: every so often, pick a piece of code I think is useless and try to delete it, then run the tests. If it can’t be deleted, write “why it can’t be deleted” in a comment above it—which scheduled task is still calling it, or which report depends on this field. When someone wants to delete it half a year later, at least they know what they’ll run into, instead of doing the archaeology all over again.
This method has a prerequisite: the tests must cover the callers. If a piece of code has no tests running at all, all-green tests after deletion prove nothing, and you can only verify with grep plus production monitoring. So in projects with thin tests, I delete at most within the range I’m confident about, and note down the rest first rather than forcing it.
Also, not being able to delete something isn’t necessarily a design problem. Orders and accounting are supposed to be tied together; some dependencies are intentional. What is a problem is the other situation: no one can clearly explain why something can’t be deleted, only that “something seems to still be using it.” Distinguishing between these two is often more useful than pursuing complete decoupling.
Now when I review my own PRs, I ask one more question: if this feature needs to be taken offline in six months, how many files will I have to touch? If it’s more than five, I stop first, tighten up the entry points and configuration, and then write the feature. Last week’s payment channel ended up going from seven places down to two: one channel enum, one yaml file. The work didn’t get smaller, but next time I add a channel, I know which two files to open.
The requirement you’re working on right now—how many files does it land in?
Comments