Denis Defreyne

Naming principle: clear kind or unit

Up: Naming principles

Consider sticking the “kind” of a variable in its name.

For example:

  • What is a JSON response_body exactly — is it parsed (an AST) or raw (a byte sequence)? The variable names raw_response_body and parsed_response_body make it clear what to expect from their contents.

  • What is the unit? Is a duration in millisecond, or seconds? I like to add the unit as a suffix, e.g. duration_ms.

This also applies to functions. For example: what is the argument to sleep() — is it milliseconds? seconds? nanoseconds? It’d be neater to have sleep_ns(10000) and not sleep(10000). Or, in a language that has named arguments: sleep(ns: 10000).

Units

Use SI unit abbreviations:

  • For seconds, use s, not sec.
  • For minutes, use min, not m (the latter means meters).

What about the type system?

This has less to do with naming, but you can indeed also use the type system (tiny types) to avoid assigning a type representing second to a type representing milliseconds.

However, that is only effective in an IDE with a proper language server. You want to be able to look at a piece of code and tell the “kind” by observing it, rather than compiling or type-checking it (let alone running it).

Note last edited September 2025.
Incoming links: