Don’t Write a Programming Language

So my domain name just renewed and entertainingly charged my wife’s credit card (I have no recollection as to how exactly that happened.) and since I haven’t posted in three years I figured it might be time I actually justified the blog’s existence. Also, frankly, I’ve been on something of a polemic tear for several days. Much of which, I’m afraid, is on a company intranet with large numbers of references to internal system so even if you could read them it would be hard to disagree with me. But this particular observation, won the incredibly painful way, seemed general enough to put in a blog post.

So, having opened my first blog post in three years with an entirely irrelevant digression, let me TL;DR; what I’m going to say here:

  • Don’t write a small programming language.
  • That includes DSLs.
  • Use a real programming language.

I think implementing tiny languages or DSLs are kind of a standard mistake anyone who loves coding makes. What’s not to like about writing code that allows you to write more code? I’ve made this mistake, sometimes accidentally, more than once. I must not be a fast learner. These days if I’m called to implement e.g. templating I am very careful to lock down the functionality to the most useful, extremely limited, features and let people just write custom code to deal with anything else. I strongly believe that if you are getting to the point where you want to implement:

  • if statements,
  • variable substitution,
  • or, heaven preserve us, for loops

You want to use a real programming language that’s maintained by professionals, possibly by embedding it into your code. If not, redesign your requirements so you don’t need that kind of complexity. It always sounds much simpler than it will prove to be. (See also: maintaining an open source library people actually use.) This is one of the reasons I champion dotnet script: it’s a proper programming language with the affordances you need to keep your code under control.

This, incidentally, goes double for complex configuration files. You may not think of their implementation as a rules engine written in an ad-hoc language, but the maintenance problems you encounter will not care what mental model you’re using.

As you can probably tell, I feel pretty strongly about this. I have nothing but respect for people like Gabriella Gonzalez who have done this and pulled it off but I’m also pretty sure she knew exactly what she was getting into. I also don’t believe reading this is going to stop anyone from going down this road. My hope, rather, is that someone who’s read this will, after the first time they’ve burnt themselves creating a DSL that they’ve gradually grown to hate, they won’t do what I did and try again.