Domain-Specific Languages vs. Notation

Programming languages often advertise themselves as being well-suited for writing domain-specific languages (DSLs), languages within the programming language that express the concepts rather than the mechanics of the code. Ruby is probably the most popular such language, but many others—such as Scala, CoffeeScript, and Io—also try to make it easy to write code that’s human-readable.

Take a look at Capybara’s DSL. Capybara is a library for interacting with a browser from code. It works hard to make the code read like English. To visit a URL, use the visit method. To click something, use click_link, click_button, or click_on. For forms, use fill_in, select, check, and others. To find something, use find, find_field, find_link, or find_button. And there’s many more.

But emulating the English language—complete with nouns, verbs, and prepositions—is simply the language of the domain, not a truly domain-specific language. A truly domain-specific language is a new language designed for use in a particular domain, with a new vocabulary and grammar suited to the specific problem.

Math notation is a truly domain-specific language. Mathematicians prefer to write symbols like + rather than words like “plus,” “add,” or “increase by.” They stack expressions in fractions and exponents, growing an idea in two dimensions rather than one linear, horizontal one. It’s much more terse than English, but it’s equally meaningful and much less ambiguous. It’s a language designed for its domain.

A key difference between using the language of the domain and a domain-specific language is that a true DSL—notation—is not only suited for expressing problems and solutions, but also for getting from one to the other. Mathematical notation is designed to be manipulated, for adjusting equations while keeping them balanced and true. Math notation is designed to make solving mathematical equations logical and systematic. Natural language is not.

There are notations in every field. Chemistry and quantum physics have their own math-like notations, ones specifically designed for their respective disciplines. And there are notations that are nothing like math notation—musical notation, dance notation, juggling notation, chess notation, and notations for various sports. They’ve all survived, not despite how terse and cryptic they can be, but because of it. People who deal in these problems day in and day out want brain-friendly languages, and often not even their mother tongue is good enough. A musician would rather play by musical notation than a paragraph written in her native language. The language of the domain wouldn’t allow her to keep up with the orchestra. Notations are highly efficient. Natural language just isn’t a good medium for working in the domain.

Notation is so powerful that Ken Iverson referred to it as “a tool of thought.” He based two separate programming languages on notation rather than natural language, and we would do well to remember the advantages of languages that help us solve problems, and not penalize them just because we wouldn’t want to read them out loud.

Conversation
  • Marius says:

    “They’ve all survived, not despite how terse and cryptic they can be, but because of it.”

    I can’t speak to the others, but perhaps the most noticeable thing about dance notation (besides the fact that there isn’t a dominant one) is that it’s basically never used. From beginning dance classes up through professional dancers, I’ve never seen anyone read or write it.

    That’s not to say that notation in general isn’t extremely valuable, but this doesn’t seem like a very good example of it.

  • Comments are closed.