Zencos has merged with Executive Information Systems (EIS)
twitter-icon
Free Strategy Consultation
Zencos Icon

Services

Contact Us

Blog

YAML is my new favorite thing. It could be yours too

Support

Obi Mbah

04/07/2022

Hero

Stop me if you’ve heard this one before:  You’re writing some new application, and—instead of hardcoding a bunch of values in your program like a barbarian—you correctly elect to put these key/value pairs in a config file.  You open up your editor of choice, punch out an .ini file, then subsequently go on about your life.  Sound familiar?

After all, a config file is a lowly store of key/value pairs and isn’t worth putting much thought into.  Let’s move on to the far more interesting task of writing the program.

Or so I thought until I found myself constantly having to do annoying things to overcome the .ini file’s limitations. Mainly, it thinks everything is a string, even when they may actually be integers, booleans, or (when I’m feeling particularly wild) lists.  “It would be nice if my config file knew what type of variable I’m using,” I mused to myself, “so I wouldn’t have to cast it in my code.”  After splitting a comma-separated string into a list for the umpteenth time.  Then, facepalming and going back to strip the leading space from each value in said list (also for the umpteenth time).  I asked myself that ever-important existential question: 

What am I doing with my life?  I wrote this .ini file because it was easy, but it is in fact, making things harder. 

I figured that somebody else must’ve already figured out a better solution, so I turned to that oracle we all know and love, Google.  In my search, I came across YAML, and it is my new favorite thing. “What the heck is YAML?” you say?

TLDR: YAML stands for YAML Ain’t Markup Language (don’t think about it, its name makes no sense), and it is basically the answer to the question: What if an .ini file wasn’t annoying and a .json file wasn’t a finicky eyesore? It’s the best of both worlds.

Long-winded version:

While googling, and before discovering YAML, I was tempted (in a brief moment of insanity) to use JSON as a config file.  But then I remembered that JSON is ugly (it’s made for computers to consume, not human beings to read), and its syntax is so picky: keys need to be in double quotes, lists need commas between values (which is fine when the list is short but when the entire file is a list, I’m forever forgetting to end a line with a comma, but maybe that’s just me). And what if I wanted to make a comment? Well, JSON doesn’t allow comments!   Because who would ever want to explain what a config option is for or list what values it should take? Certainly not me!

In fairness, it’s not JSON’s fault. It’s not suited to be used as a config file, so I nixed that (terrible) idea and started looking for other alternatives. I toyed with the idea of TOML (which is kind of like a slightly less annoying INI) but ultimately decided on YAML because…

The virtuous virtues of YAML

  1. Its syntax is clean and intuitive (YAML does allow ugly syntax if you want to use it, but why would you do that?).
  2. You don’t have to end lines with commas when they’re lists, you can just begin the line with a hyphen instead.
  3. You can express a value as a list, precluding the need to split (and then go back and strip) a comma-delimited string.
  4. You don’t have to quote string values (you can, but you don’t have to). It’s good at recognizing that blah is a string and 100 is an integer.

What follows is a sample INI file with its YAML equivalent:

 

INI Version

[db]

host=my.fine.server

port=1984

autocommit=true

# schemas in the database

columns=schema, schema, schema

 

YAML Version

db:

  host: my.fine.server

  port: 1984

  autocommit: true

  schemas: [schema1, schema2, schema3] # YAML allows in-line comments like this

As you can see, the INI and YAML versions look basically the same, except for using a colon instead of an equal sign and using indented levels like Python.  So, what’s the big deal? 

As stated before, everything in an INI file is a string unless you tell it otherwise in your program.  But without changing anything else, once the YAML file is loaded, it knows that host is a string, port is an integer, autocommit is a boolean, and schemas is a list of strings (strings I don’t have to remember to strip, mind you).  It’s much more seamless, I just load the YAML file and all my values are the expected type without having to write explicit casts.

Now I imagine, you’re totally convinced and want to know how you too can get started.  Below is a nice tutorial that explains YAML’s syntax.  Most popular programming languages already have one (or more) YAML parsers written for them.  So, there’s nothing standing in your way from letting YAML become your new favorite thing:  https://www.tutorialspoint.com/yaml/index.htm