HACKER Q&A
📣 tomcam

Should my YAML tags start with caps? Use hyphens?


Working on yet another CMS and want to know best practice for YAML. Should my YAML tag names, some of which are several word, use mixed case for readability, or all lowercase, or what? Surprisingly, the standard (https://yaml.org/spec/1.2.2/) is silent on how to represent identifiers, although most of their examples are all lowercase.

One option is embedded caps with hyphens:

    Exclude-Dirs:
    - *.css
    - *.go
    - backup
Or all lowercase, for consistency and ease of typing? It's a bit harder to scan in a long listing.

    exclude-dirs:
    - *.css
    - *.go
    - backup
I'm going to avoid the programmery compound words, like this, because I'm angling somewhat for the power user, as opposed to programmer, audience:

    # Nope
    excludedirs:
    - *.css
    - *.go
    - backup


  👤 smt88 Accepted Answer ✓
This seems like a style preference, not something where there is a clear "best" option.

I personally suggest snake_case for five reasons:

1) It's very readable (an underscore visually mimics a space between words)

2) The question of case sensitivity is avoided because it's all lower case

3) No one has to remember whether the capitalization is PaschalCase or camelCase or whatever the other cases are -- it's just lowercase all the way through

4) The YAML key becomes a valid variable in every modern programming language (which is not true if you use a hyphen)

5) Double-click in any system will select the_whole_key instead of separate-words (test those in your browser to see what I mean)