Explicit empty else
branches in templated configs
Examples in this entry use Jinaj2 syntax, but the topic is completely stack agnostic and does not require deep understanding of any specific templating language.
Problem statement
When templating config files, it's not uncommon that one ends up with some form of the following:
{% if something %}
some-config-statement
{% endif %}
It's a conditionally added statement, so not just a config param that we would assign on/off value, but rather something that we want to either be present or be completely skipped - maybe absence of this statement is the only state that has semantics that we want, maybe we want to lean on a default behavior hardcoded in the configured software etc.
At a first glance there is not much to it. But, if we were to look at the resulting config file, we will either see that the statement is set or... we will see nothing (surrounded by the rest of the file).
And while the existence of nothing can be argued (2013 Isaac Asimov Memorial Debate: The Existence of Nothing ), it's of not much use for us here, because we will either not notice it when later reading the resulting file, or we won't remember why it's there or what it means, exactly, or if this is maybe some bug, missed decision about the configuration state etc.
Improving the situation
What I often times find much more useful, is to make the else branch explicit and put a comment there:
{% if something %}
some-config-statement
{% else %}
# option disabled - skipping config lines for XXX
{% endif %}
Or maybe render skipped statements in a commented-out fashion:
{% if something %}
some-config-statement
{% else %}
# some-config-statement
{% endif %}
That way we make absence explicit and thus more clear outside of the context of the template source and specific templating config applied.