Prefer Local, Specific Code


As a default, prefer your code to be local and specific. This preference will make your code easier to understand and maintain.

Relation by Location

If code is colocated, it's easier to see what uses what. The callers and callees are side by side. You can see the association. The caller is not reaching far to get the code it needs. You're in the same namespace or module. We have created relation by location. Our import list stays small.

Specific to Need

If code is specific to our need, it'll be focused on just what is needed, no more. We'll avoid some bells and whistles that we thought might be great or useful in the future but aren't used now.

Specific code is smaller, tighter. By definition, it only cares about one or a limited number of things. Often, it'll be simpler.

Scoped Thinking

When we can focus on the need at hand, there's less to think about. There's less to hold in our head. We have a more narrow set of issues.

The work finishes more quickly. We aren't as overwhelmed. We don't have to consider history, other contexts, variants.

Scoped Usage

If there are no other callers other than those locally concerned, I don't have to consider them. There's no one reaching in. I'm grateful that I didn't export or make public so I'd have to check for those things. I don't have to search globally for references.

The refactors are easier. I don't have to learn about all the ways a piece of code is being used. I know there's, let's say, one caller. I know how it uses the code. When I refactor it, I need to make that one caller happy.

Generalize As Needed

At some point, you may want to generalize some piece of code, and you'll pay that price of reuse. Writing local, specific code first doesn't have to hurt those re-use payoffs. It increases your rate of success. It postpones reuse to the proper moment instead of every opportunity at re-use and paying for it.

At that moment, we'll need to do the work to find and generalize the code. And this is work, no doubt. But it's the right time to pay it because we can benefit from it now. And it's the right price to pay because we know accurately what we need right now.