DSLs vs APIs: an unnecessary fight

Recently I’m seeing a lot of discussion from people that believe that Domain Specific Languages (DSLs) are nothing more than glorified Application Programming Interfaces (APIs). In fact, a lot of this debate is put across in a derogatory tone:

“Hey kids, stop playing with your DSL toys and step into the real world. Here we just call them APIs”.

For me, this highlights what I’m seeing to be an increasingly growing divide - that between people that think of programming as formalising functionality and the people that think of programming as communicating process.  Both are equally valid views of the world and both viewpoints afford us with useful ways of thinking about the work we do on a daily basis.

As a very brief aside it’s important to say that in this article when I say DSL I’m actually mean internal DSLs - something written in a host language. For more discussion on this formalisation see http://www.martinfowler.com/bliki/DomainSpecificLanguage.html.

From a very high level perspective, both DLSs and APIs can be described as interfaces. Let’s call them language interfaces and programming interfaces. You might even argue that DSLs are typically an interface between software and a person, and APIs are typically an interface between software and software. However, this categorisation is remarkably bipolar; we’re increasingly seeing DSLs used for software to software interfaces and APIs still have to be read and understood by the humans connecting the software components together.

For me, a better distinction to make is the following:

“APIs focus on functionality, internal DSLs focus on communicating the functionality.”

This means that the programming as formalising functionality crowd will tend to see DSLs as unnecessarily wordy and complex APIs and the programming as communicating process will tend to see APIs as unreadable and unfriendly DSLs.

It doesn’t have to be this way. In fact both can co-exist perfectly happily. There’s no reason an API can’t do what it does best - formalise a particular set of functionality - and then it’s perfectly feasible to build a set of DSLs which sit on top of the APIs to communicate that functionality to different audiences each with their own focus and vocabulary.

I’d therefore like to ask people to stop the infighting. Discussion regarding the usage of specific terminology is important, sure. However I believe the term DSL is here to stay and although in many cases they may be functionally similar to APIs that’s not the point.

The point is that DSLs invite programmers to consider programming as communication and that’s a very good thing.

Posted April 2nd, 2009 by Sam Aaron
 

Comments

 
  1. Some programmers have suggested that well-designed APIs can communicate the intent of the code to such a degree that many (most?) comments are unnecessary. Fowler’s Refactoring book is almost a decade old.

    If that’s the case, what’s the distinction between a DSL and an API again?

    Posted April 2nd, 2009 10:26 by chromatic
  1. @chromatic

    If you write an API with explicit consideration of communicating intent, then there’s no real distinction between an internal DSL and an API. However, I don’t believe that renders term DSL as defunct. For example, consider the difference between the terms Test Driven Development and Behaviour Driven Development. Functionally you can argue that they’re equivalent, yet the main difference is the mindset they put developers in.

    For me, if the term DSL can put a programer in the mindset of writing an API with consideration of communicating to its many explicit and implicit audiences, then I’m happy with its use.

    Posted April 2nd, 2009 10:42 by Sam Aaron
  1. In other words, a developer who does a good job writes a DSL, while a developer who does a bad job writes an API? Surely it’s possible to write a DSL which does a poor job of communicating intent.

    Take the well-poisoning (DSL good! Intention-revealing code good! API bad!) and navel-gazing (But how did you *feel* when you wrote it?) out of that argument and the remaining distinction is far too subtle for me.

    Posted April 2nd, 2009 19:12 by chromatic
  1. I’d argue, DSLs are the grammar, no pun in tended, and API are the words. C, C++, Java, C# have similar constructs for their grammar, where as SQL is completely different. If one were to work with directed graphs a lot, one may create new DSLs for more common operations such as lineage, definition, manipulation and so forth.

    Beyond that, building new DSLs on your current language of choice seems almost silly, especially when the language already provides what is needed. Then again, forcing idioms using a languages’ constructs, into the language that doesn’t support it seems kinda un-fun. Take java and the lack of easy to implement lambdas. perl, python and ruby do it really well. The question is then, what do you gain out of writing a DSL for it.

    API are always dead easy to write, but can become cumbersome when the combination of uses grows large. In a language that doesn’t support vargs(…) constructs, writing function add(a, b) becomes tedious for dealing w/ three or five numbers. add(a,add(b,add(c……)))))

    Posted April 2nd, 2009 20:13 by exussum