swyxio / swyxdotio

This is the repo for swyx's blog - Blog content is created in github issues, then posted on swyx.io as blog pages! Comment/watch to follow along my blog within GitHub
https://swyx.io
MIT License
335 stars 45 forks source link

How To Name Things #123

Closed swyxio closed 2 years ago

swyxio commented 2 years ago

title: How To Name Things slug: how-to-name-things category: essay tags: ['Tech', 'Naming', Advice] begun: 2019-05-09 date: 2019-05-16

There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors. - Leon Bambrick

I've vacillated on my opinion on naming things (this post mainly deals with naming things in code, though I end with other resources on naming everything). I think most people start out with no or weak opinions, looking slightly askance at the weirdos who do have strong opinions. They absorb naming conventions by osmosis, and then run into real problems at scale/over time, and then develop extremely strong opinions informed by that experience.

The Weirdo's Journey.

I've given this essay a slightly clickbaity title. Spoiler: I'm not going to solve the problem of naming things today. All I hope to do is describe some opinions I've formed from my experience in Python and JS, list some considerations, invite you to share yours, and suggest you have this debate on your team.

Use AI! [Sept 2022 Edit]

Not Naming Things [Aug 2019 Edit]

One option people sometimes forget they have at their disposal is to just not name things where possible. I have a couple examples for you.

Example 1: Not giving different names at module and function boundaries

Mind your "name stack". This is the number of names you have to keep in your head as you read code.

You can name the same thing 8 different ways at boundaries and hate life when you have to refactor or grep your own code:

// index.js
const grault = require('./corge')
const foo = grault('baz')

// corge.js
export default function doBar(qux) {
  let quux = parse(qux)
  return quux
}

or just 2 ways and hate life less:

// index.js
const { getFoo } = require('./getFoo')
const foo = getFoo('bar')

// getFoo.js
export function getFoo(bar) {
  return parse(bar)
}

Example 2: Using Tooling to autogenerate names

Instead of naming a title in CSS and then also a <Title className="title"> in React, opening yourself up to global conflicts and subsequent refactoring, you could choose to use either a CSS Module or CSS in JS approach to scope and manage them together. Credit for this idea comes from Max Stoiber.

Notable Exception: Kyle Simpson famously does not use => function syntax, preferring explicit function declaration, because he wants to avoid anonymous functions in the stack trace. That is his prerogative, but I don't think this is a battle worth fighting.

Heroku-like autogenerated app names

Example: "rough-snowflake-1142", "nameless-star-13", "cool.leaf.6743"

Tools

Probably Bad Names

Inherent in having any opinion on naming things is some intuition that some names are worse than others.

This can feel a bit silly in languages where naming has no impact on program behavior, especially in JavaScript where everything gets minified. In that sense, naming is bikeshedding.

But code is not just written for correctness, it is also written for other humans to read (and maintain). In a strong form of Sapir Whorf, what you name a thing can totally shape and artificially limit your creativity. In that sense, naming is -not- bikeshedding.

And yes, I've unironically been in standups where we bikeshedded on whether something was bikeshedding. The rabbit hole goes deep.

I'll motivate the discussion with some examples:

Name Pollution

It is possible to have too much of a good thing! Even if all names technically fit whatever guidelines you choose, it is still possible to have way too many names. Every new name demands more space in your working memory. One very pervasive way this happens is when names cross file and module boundaries:

This was adapted from real code in a popular framework. Here we end up with 3 different names for the exact same thing. Triple the bikeshedding. As Joe Fiorini puts it, name files after their default export, or even better, don't have a default export, and still name the file after the "main" export anyway.

Controversial Names

Not all names are obviously bad, even though they may seem bad to you.

Probably Good Ideas and their Considerations

Having dealt with the easy stuff above, I'm now on much more equivocal territory. Here we deal with some considerations you may want to think about in forming your naming guidelines and where I stand:

Sidenote: Naming is a subdiscipline of a broader art I call "API Design" - a very important and difficult-to-study topic I hope to one day write about.

As usual, it is possible to take good ideas too far - encoding types into EVERYTHING and being concise leads you to the commonly misused form of Hungarian Notation, which nobody likes.

The Cost of Enforcement

I do have a strong opinion that naming opinions should be breakable guidelines rather than strict rules. If you are spending more than 30 seconds discussing a name in a code review, and opinions differ, its probably not worth further debate. Your team's time is valuable and this costs more the bigger your team is. (Although if someone comes up with a name that everyone agrees better fits the concept/domain, then that is a great use of time!)

But wait, what about code standards? Without constant vigilance, my codebase will descend into a pit of chaos!

Well, first of all, nice to see that you trust your colleagues that much.

Second, whatever can't be automated can't be enforced. Code reviews cost. Human code review will have inconsistencies. The person who nitpicks names all the time will either be resented or joked about because they don't see the bigger picture. It just never ends well. Don't be the bad cop - let the machine do it.

The base level is trusting in syntax and tests - if the code is valid and works as advertised, you very likely already have bigger problems you should pay attention to. The next level is autoformatting (prettier for JS, black for python) and linting where you write or adopt code that looks at your AST and enforces simple naming rules. Be careful: Overly eager linting is a problem.

As Nick Shrock says: Delegate to Tooling Whenever Possible. His advice on Code Reviews is worth a full read here. Importantly: the goal of a code review is not to make it so that the code looks as if you wrote it. Internalize that.

Sindre Sorhus has some strong opinions on naming. You may not agree with all of them, but at least they are enforced in code. Check eslint-plugin-unicorn.

Domain Driven Design

(Aug 2019 Update) I was fortunate enough to attend a workshop by Andrew Cassell on Domain Driven Design (slides) where the concept of "Ubiquitous Language" drives naming and I really like this concept. However some of the application examples I've seen bleed the domain all over the place whereas I really only think it matters most at the public API.

Collections of Things

(Aug 2019 Update) Don't pluralize lazily, e.g. blog.js and blogs.js. This is terrible to grep especially with one name being a substring of the other. Prefer to name both items and collections visibly. This is similar to the Hungarian notation idea, but works even if you use a type system. Tweet

2021 Edit: see also my popular post on Premature Pluralization

When all else fails... who writes the code?

If you're still spending a lot of organizational energy bickering over a name... remember this story from Bret Taylor about how Google Maps' Satellite Mode was almost named Bird Mode.

Code Complete [Nov 2019 Edit]

The volumninous Code Complete offers an entire chapter on the Power of Variable Names. This has a lot of good advice. Here are some nice examples pulled from the book:

Naming Content

Naming Startups

More References

Your Opinion Here!

I asked for more opinions on Twitter, and here are some I got:

Last but not least, in the mailing list preview, Massimiliano also recommended Joel Spolsky's Making Wrong Code Look Wrong, which I can't help but recommend again.

2023 addendum: letting your users name themselves

Discord recently went thru a painful rename: https://support.discord.com/hc/en-us/articles/12620128861463

swyxio commented 1 year ago

How Apple Names Things - interesting to see the clusters https://nicolas.kruchten.com/content/2022/12/apple_tm/

swyxio commented 1 year ago

https://davidwells.io/blog/clean-analytics

swyxio commented 1 year ago

how Stripe was named