On repetition, YAML, errors, and yelling.
On our new project we’re relying heavily on YAML. Not for internationalization purposes (though that’s one possible benefit of our approach), but rather to keep things like class names and IDs consistent throughout the various views and controllers, and throughout the HTML, CSS, and Javascript. (More on that later.)
One thing that’s been annoying me, however, is how much I need to repeat myself in the strings.yaml file. For instance, I find myself writing things like:
friends_classname: "friends"
friends_behavior: "friends"
friends_id: "friends"
It turns out that YAML can easily handle this. As the spec puts it:
Repeated nodes are first identified by an anchor (marked with the ampersand – “&”), and are then aliased (referenced with an asterisk – “*”) thereafter.
So the above YAML can be written like this:
friends_classname: &friends "friends"
friends_behavior: *friends
friends_id: *friends
And both will work exactly the same.
I realize, of course, that the second example is actually more verbose than the first. But I’m not trying to avoid repetition to reduce the number of characters used; I’m trying to decrease the likelihood of subtle errors, and thereby decrease the difficulty of debugging.
In the first case, if I made a typo like friends_id: "freinds", friends_id would be wrong, and things would fail silently. In the second case, a similar typo, like friends_id: *freinds, would fail completely. YAML will give a BadAlias error, and my app will break, and Rails will yell at me.
I like loud errors: they save time.

No Comments, Comment
Reply to “On repetition, YAML, errors, and yelling.”