Language Expressivity is all about Scale

Recently, while working on SwiftKey, I’ve found myself writing significant quantities of C++, Java, Python, Ruby and Scala. And switching back and forth between these languages has got me thinking about expressivity.

The expressivity of a language is a difficult concept to pin down. It’s something that anyone who’s used more than one language will develop an intuition for, but how do you explain why one language is more expressive than another? I’ve recently convinced myself that it boils down to the scale at which a language makes it convenient to use a particular type of abstraction.

Here’s an example. I recently found myself writing a Java class which contained several methods that like this:

public void doSomething() {
  synchronized(resource) {
    if(!resource.isReady())
      throw new ResourceNotReadyException();

    // doSomething-specific code
  }
}

public void doSomethingElse() {
  synchronized(resource) {
    if(!resource.isReady())
      throw new ResourceNotReadyException();

    // doSomethingElse-specific code
  }
}

This kind of duplication makes me nervous, so naturally I would like to get rid of it. Before looking at how to do this in Java, let’s look at how I could remove this duplication in Scala:

def claimResource(action: => Unit) {
  synchronized(resource) {
    if(!resource.isReady())
      throw new ResourceNotReadyException()

    action
  }
}

This would enable me to write the doSomething and doSomethingElse methods like this:

def doSomething() {
  claimResource {
    // doSomething-specifc code
  }
}

def doSomethingElse() {
  claimResource {
    // doSomethingElse-specifc code
  }
}

There’s nothing whatsoever to stop me from doing something very similar in Java. Here’s what it might look like:

void claimResource(Runnable action) {
  synchronized(resource) {
    if(!resource.isReady())
      throw new ResourceNotReadyException();

    action.run();
  }
}

Which I could use like this:

public void doSomething() {
  claimResources(new Runnable() {
    public void run() {
      // doSomething-specific code
    }
  });
}

public void doSomethingElse() {
  claimResources(new Runnable() {
    public void run() {
      // doSomethingElse-specific code
    }
  });
}

Eugh!

At some level, the Java and the Scala are “the same”, but the additional boilerplate associated with the Java solution makes me question whether I’ve actually made the code any better—I’ve removed the duplication, but at the expense of adding quite a bit of additional boilerplate. So much so that, in this case, I chose to live with the duplication.

Scala makes this kind of thing so easy that I don’t think twice about it. Java makes it difficult enough that I only consider this kind of approach for “big” problems.

There’s no abstraction, in any language, that I can’t find a way to use in any other language. The question is how many hoops I need to jump through to get there and whether the problem is large enough to make that hoop jumping worth the effort.

At the limit, if I’m using (say) C and there’s some wonderful abstraction in (say) Lisp that would make my life much easier, I can just write a Lisp interpreter in C. But the cost of doing so is huge. So much so, that I would only consider it for a really large problem (perhaps this is a corollary of Greenspun’s Tenth Rule?).

So, what makes a language expressive? The more expressive the language, the smaller the scale at which it allows us to apply abstractions.

1 Response to “Language Expressivity is all about Scale”


  1. 1 oxbow_lakes March 9, 2011 at 1:32 pm

    RT: “The more expressive the language, the smaller the scale at which it allows us to apply abstractions”

    Excellent analysis

    Chris


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.

Join 241 other followers

%d bloggers like this: