TechWorkRamblings

by Mike Kalvas

202203221610 Naming conventions should use unambiguous delimiters

When naming variables, files, etc. we need to delimit words, initialisms, and segments of meaning for people to interpret. Therefore, we should use unambiguous methods to delimit each word. In practice this means casing like kebab-case and snake_case are objectively simpler and superior to things like camelCase.

Further explanation

Firstly, acronyms and words. Symbol delimited names are the only way to universally disambiguate words. There's a reason that CSV stands for Comma Delimited Values and not Capital-letter Delimited Value. It only takes one example to see this.

What do we do when we want to name something like "URL Parser". Our options are urlParser, uRLParser, URLParser, and UrlParser. The option urlParser isn't so bad, but we also need to solve for when the acronym isn't the first word in the sentence like "parse URL": parseUrl or parseURL. It certainly loses even more appeal if the acronym is in the middle: parseUrlParams and parseURLParams. And (though the example is getting contrived now I've seen similar and worse in real world code) the final nail in the coffin is "a URL parser": aURLParser, AURLParser, AUrlParser, or aUrlParser.

Ok, well we could just make a bunch of rules about acronyms and words and things (which I've seen done) for people to adhere to, but consider that for every instance like this, some dev is wasting time doing one or more of the following:

  1. Trying to massage names into a form that isn't ambiguous to avoid dealing with these issues in the first place, often resulting in less than ideal naming.
  2. Just picking one that seems right to them at the time.
  3. Having to remember or worse, spend time looking up, the rules that we decided would be our standard.

Now think about how if we just go with kebab-case, all of this is solved instantly. It is unambiguously clear that we name these url-parser, parse-url, and a-url-parser. We choose where the breaks are, casing is always all lowercase, and we never have to pick anything besides the clearest name possible. This adheres to 202203221620 The golden rule of programming.

The second key reason is systems, history, behavior, safety, etc. The choice of a hyphen as the symbol for our naming structure is important because it's one of the best symbols that just works™️ cross-platform and cross-tooling. This includes things like what the behavior is when you hit ⌥ + ⌫ while typing the name, if these cause parsing errors, whether it's ASCII vs UTF-8, URL conventions, and more. Think about how annoying it is to use files that have spaces in their name from the terminal.

Addendum: snake_case is a close runner up, but I have anecdotally experienced better performance in this second category with hyphens over underscores. Also the shockingly minor yet impactful difference of typing the - vs ⇧+- really adds up over time.