Beautiful Code: Domain Specific Languages

Written on 5:10:00 PM by S. Potter

Last year when I was first introduced to Ruby on Rails through the screencasts, I remember watching DHH (Rails creator David H. Hanson) say that one of the main motivations behind developing Rails for him was to allow him to create "beautiful code" and that writing beautiful code made him happy and more productive. At the time I liked the concept, but wasn't really sure how true that would be, at least for me. I am a non-visual person and I really didn't think writing beautiful code would affect the way I work and make me noticably more productive. Well, I was wrong! Since last year (2005) when I started Rails development I have grown very attached to creating beautiful code, so much so that have noticed a remarkable difference in how productive I am in Ruby and Ruby on Rails projects compared to my Java work. Remember in 2003 I thought Java was the dog's tuxedo and only ever wanted to write Java (oh, how things have changed). Late last night I watched a Ruby SIG talk about designing domain-specific languages (DSLs), which peaked my interest in the relationship between Sapir-Whorf, beautiful code and developer productivity. Of course, I heard about Sapir-Whorf a while ago and found it interesting at the time, but wasn't really applicable to what I was working on at the time (or at least I thought), so I didn't pay much attention to it until now when almost every Ruby presentation discusses it. Until this evening, I was not able to work on CodeGenie since my last blog entry, but I think it was for the best. My new found love for rspec (a DSL for spec-ing software written in Ruby) and my interest in the relationship above both have inspired me to think about CodeGenie in a new light. Let's take a step back. The general idea behind the Sapir-Whorf hypothesis is that the language you use to "discuss" a subject affects your internal thought processes, such that using one language could restrict or expand your thought process compared to another language. This is applied to both natural and programming languages (in programming languages I include both DSLs and general purpose programming languages that must be Turing-complete, e.g. Ruby, Python, Java, C++, COBOL, etc.). For those that haven't looked at rspec, here is a quick snippet of code:

context "An empty Array" do
  setup do
    @array = []
  specify "should be empty" do

  specify "should have size of 0" do
    @array.size.should.equal 0
Of course, this is pretty simple stuff, but you can immediately read what the snippet specs out for the Array class. It uses language the is specific to the domain of specifying software requirements. In a loose way Rails provides DSLs for defining controller actions, data schema migrations, object-relational mapping, web services and whatever else you use the Rails environment for. However, rspec really goes the extra distance in defining a "language" to use for the sole purpose of specifying software requirements. In many ways this supercedes Test::Unit (or JUnit for the Java heads out there) - you should check out Dave Astels' presentation at Google on Behavior Driven Development (BDD) [very enlightening]. In CodeGenie I want to create a DSL that allows plugin developers to define generators and/or metamodels implemented within the plugin. The idea is that a plugin can contain one or more generators or metamodel implementations. Each generator defines one manifest per metamodel that it supports. A metamodel will define a translator that sucks in the metamodel data, parses it and then spits it out in an in-memory object representation custom to the metamodel format. CodeGenie itself will provide a set of very standard generators and metamodel implementations. Currently am thinking the plugin developer will define a plugin with one generator, something like the following snippet:
context = => "templates", :DOCUMENT_DIR => "documents")

generator "Rails startup generator" do
  manifest :yaml, context do
    # generate using data from the YAML metamodel passed in to the eRB template
    template "#{TEMPLATE_DIR}/" ""
    # create src directory in the root directory of the destination path.
    dir "src"
    # copy standard README, LICENSE and INSTALL files without processing it through the eRB engine - straight copy
For those familiar with the Rails generator, this should like slightly familiar, except that instead of extending a Rails class and defining a manifest class method, I have created a bit more syntactic sugar to make it more domain-specific. These are my initial thoughts and I will work on maturing the DSL over the next couple of weeks. My plan is to get a simple, but working CodeGenie released with YAML and limited MOF support by the end of 2006. Thanks for your patience and if you have suggestions for CodeGenie, please send a message to

Sapir-Whorf, Domain Specific Languages & Gay Marriage

Written on 6:46:00 PM by S. Potter


A while ago I was introduced to Sapir-Whorf, which is an intriguing linguistics hypothesis. Informally (my definition) it states that the language you use to describe a subject shapes the way you think about that subject. Formally Wikipedia defines the Sapir-Whorf hypothesis as:
there is a systematic relationship between the grammatical categories of the language a person speaks and how that person both understands the world and behaves in it. This controversial hypothesis is named after the linguist and anthropologist Edward Sapir and his colleague and student Benjamin Whorf.
Almost everywhere I look these days, especially in the domain specific language design discussions, Sapir-Whorf is cited.

Domain Specific Languages

A domain specific language (DSL) is a programming language which may or may not be Turing complete that was design for the purpose of executing specific tasks. Examples of DSLs include:
  • rspec - a software requirements specification DSL
  • Maven POM - a project comprehension tool's DSL
  • awk - processes text data DSL
  • XSLT - XML transformation DSL

Gay Marriage

That all sounds very academic, so I wanted to take a few steps back to have a look at a real world example and extension of Sapir-Whorf, especially applicable to the US audience today. Until 2004 I do not believe the majority of voters in the US had considered what "gay marriage" actually meant. During the 2004 presidential elections it was at the forefront of the social issues in the debates even though both major presidential candidates (George W. Bush and John Kerry) were both against "gay marriage". In the public and media debate on "gay marriage" the social right characterized "gay marriage" as breaking down the "institution" of marriage. On the GLB and liberal side people characterized "gay marriage" as one step in a broader campaign promoting equality for a minority group analogous to the civil rights plight in the 1950s through 1970s but for the GLB minority. Today "gay marriage" remains a hot button issue for many Americans as we head into the mid-term elections.

Sapir-Whorf & Gay Marriage

My personal (religious/political) opinion on the matter of "gay marriage" is irrelevant to my application and extension of the Sapir-Whorf hypothesis in the discussion below. To understand my lay person's (note: I am anything, but an expert on this subject) linguistics argument you should also try to keep your personal opinions out of your mind when you process the discussion. According to Wikipedia:
A marriage is a relationship between or among [two] individuals, usually recognized by civil authority and/or bound by the religious beliefs of the participants.
Let us investigate what the GLB and liberal groups advocating marriage are referring to. Many references to gay marriage on the HRC (Human Rights Campaign, which advocates "gay marriage") website use the terms "gay marriage" and "civil unions" interchangeably and the meaning of civil unions seems pretty clear - a set of legal rights between individuals that enter into the union.

The Polls

The Washington Post and ABC News conducted a poll of 1,202 randomly selected adults in US in March 2006 found the following:
  • 59 percent of respondents oppose gay marriage (up 4 percentage points from February)
  • 51 percent of respondents favor allowing same-sex couples to enter into civil unions with the same basic legal rights as married couples (up 6 percentage points in less than a month)
This puzzles me: how can 59 percent (and rising) respondents oppose gay marriage, and yet also 51 percent (and rising) from the same respondents also favor same-sex civil unions with all of the same legal rights as heterosexual married couples? Even considering the margin of error, in this case 3%, this poll strongly suggests that many Americans (assuming the poll respondents are representative of the general population) are not interpreting "gay marriage" and "civil unions" to be interchangeable as GLB groups that advocate "gay marriage" do. Now let us consider the demographic of the social conservatives that vehemently oppose "gay marriage": people that consider themselves religiously active. People in this demographic would probably use the term marriage in a way that emphasizes the religious sacrament of marriage rather than a marriage simply for legal convenience. Of course, people get married everyday for a combination of these reasons - sometimes even business reasons, but as you will see it does not affect the rest of my argument. In fact, the social conservatives' argument against "gay marriage" demonstrates their different interpretation of the term "gay marriage". I suspect social conservatives feel like their freedom of religion is being attacked, but this direction in the argument is long and windy.


My suggestion then on the issue of "gay marriage" in the US is simple: the social conservatives and the advocates of "gay marriage" are not talking about the same thing at all AND they are NOT talking in the same language. Whether you agree with allowing same-sex couples to enter into a relationship recognized for legal purposes or not, let us all agree that there is a mismatch in meaning between what GLB groups are promoting as "gay marriage" and what the social conservatives who oppose it denounce as "gay marriage". I also suggest that the reason why these two groups on either side of the debate are talking about different things is because the dialects that they each use, while both are American-English in a broad sense, the meaning of words have subtle differences due to cultural nuances such that on this issue cause a potentially unnecessary collision of large proportions. Below is a proposal for a domain specific language for the purpose of defining political debates. It would look something like this for the issues of Gay Marriage:
political_debate "Gay Marriage" do
  argument_for do
    define :glb_people.includes :gay, :lesbian, :bisexual
    define :civil_union, legal_contract.between.2.individuals
    define :gay_marriage, civil_union.for.glb_people

  argument_against do
    define :glb_people.includes :gay, :lesbian, :bisexual
    define :sacrament, :religious_contract.with.god
    define :gay_marriage, sacrament.for.glb_people
    candidate_affiliation :george_w_bush, :john_kerry
Immediately from the snippet of code using my fictitious Political Debate DSL (PDDSL) that the primary for and against arguments have different definitions and that the debate needs to be restructured so that both sides are actually talking about the same thing. My suggestion would be to call it a debate on "civil unions" and NOT "gay marriage". I think this would end a lot of unncessary disputes on the political air- and blog-waves.