Clear Naming Is the Best Documentation

While I advocate for simplicity, I also believes that a name's clarity is crucial documentation. The goal of naming is to clearly convey what something is and what it does.

The programmers' dilemma: "What's in a name?" goes the Bard. It's not a new debate. Some programmers grumble about the strict naming conventions - variable, function, and class names included. "Just pick something easy to type and move on!" they say.

But I respectfully disagree. The name of a thing is its most essential documentation. It's always present, always seen. A chance to inform and guide is too valuable to waste.

When naming something, your goal is simple: make it clear what the thing is and what it does. Easy to understand, easy to use.

Optimize for simplicity, not minimization

Code readability suffers when variable names are too short. It's a common mistake to prioritize type-efficiency over clarity when writing code.

In extreme cases, this leads to extremely short variable names. Older code or work from experienced programmers from past decades often exhibits this style.

Type-Efficiency (lacks clarity)

type DataMap = { [key: string]: number };

const process = (data: DataMap): number[] =>
  Object.keys(data).map((key) => data[key]);

// Usage
const input = { a: 1, b: 2, c: 3 };
const result = process(input); // [1, 2, 3]

Prioritizing Clarity

  • Renamed DataMap to KeyValueMapping for better context.
  • Renamed process to extractValues to explicitly convey its purpose.
type KeyValueMapping = { [key: string]: number };

const extractValues = (mapping: KeyValueMapping): number[] =>
  Object.values(mapping);

// Usage
const input = { a: 1, b: 2, c: 3 };
const result = extractValues(input); // [1, 2, 3]

Handle mixing name conventions

Naming inconsistencies can be confusing, especially when using external libraries with different conventions.

You are working on a project with camelCase naming conventions, but you need to use a library that provides functions and classes in snake_case. To minimize inconsistencies, you create a wrapper module to adapt the library's code to your project's naming convention.

Mixing conventions can be costly, requiring constant interpretation to understand which rules apply. This can make code harder to read and debug.

To minimize this issue, we can ""ring-fence"" foreign code by isolating it and adapting our naming scheme to match the library's conventions.

The importance of mechanical name conventions

For consistency in naming conventions, it's essential to make everything mechanical. If a convention requires judgment calls or thought, it won't work. Different programmers will make different decisions, leading to inconsistent names.

To achieve consistency, follow mechanical rules. This makes collaborating on code easier.

The key is to have strict, well-documented, and enforced conventions. This ensures everyone uses consistent names, making code collaboration a breeze.

Identify areas where your project conventions can be made more mechanical and implement them. You'll reap the benefits for years to come.