Thursday, October 18, 2007

API vs. DSLs

The discussion of what is the difference between an (internal) DSL and an API comes up over and over again.

So, what is it that distinguishes a DSL from a (really well designed) API? Is it method chaining ("fluent APIs")? Is it the fact that the API/DSL is readable by a domain expert?

In my view, the two important differences are:

(1) domain-specific syntax: the syntax does not look like a 3GL program. It might be a completeley different syntax in an external DSL or syntax "tweaks" in an internal DSL that make it look sufficiently different than typical host language code

(2) domain-specific validation: if something's wrong in your "DSL program" I want error messages on the same level of abstraction as the program (which is: domain-specific) as opposed to strange compiler or runtime errors (because you somehow misused a weird host language feature)

I am not sure these two points are good enough to distinguish things. What do you think?
Ah, so I'm not the only one struggling with this? :-)

My litmus test would be the degree to which you still need to know the intricacies of the host language. Which is what your two points come down to as well, I guess.

But using a term like "degree" makes things all murky en gray. To make things nice and black-and- white again, maybe we should stop seeing internal DSL's as DSL's all together. "Nicely fluent API's" might be the better term.

I think the main difference between an API and an embedded DSL is the intention.

With an API you want to consistently access/control some underlying code/framework/library.

For an DSL aside from the executing/rendering DSL the main point is the expression of content/semtantics of the sentence in the DSL's grammar.

DSLs are also much more limited in scope than APIs. (See RoleInterface)

Also APIs are (by now) an imperative approach whereas fluent/role specific DSLs are much more declarative.

When writing my own embedded DSL ( the difference became quite obvious. An API would looked like quite different. Much more imperative and OO-like.

Asking whether something is an API _or_ a DSL is not the correct queston to ask, in my opinion. Fluent interfaces (or whatever technique you may use to actually implement something DSL-stylish within a host language) will _always_ be an API. The question is, if an API may be perceived as being _also_ a DSL.

Besides establishing terms/names that closely reflect the concepts of a particular domain, the crucial point is to provide a decent grammar.

For example, using fluent interfaces together with properly named methods is not enough. You need some mechanism that restricts the use of those methods in some way, depending on the context in which they are used. I.e. the allowed sequence of "method chains" forms the sentences which are valid according to the grammar.

See, for example, the "ExpressionBuilder" pattern/technique described by Martin Fowler. As far as I remember there is also a blog entry by David Laribee, who describes this as "ordered fluency".


Uhh, Markus, you make it _really_ hard to leave comments at your blog :-(

What's so hard about leaving a comment? This is the standard Blogger mechanism...

I do have to explicitly moderate the comments, because otherwise I'll get all kinds of spam.

Re: What's so hard about leaving a comment?

Well, you have to create a google account. Then you must confirm the account at google. Then you can leave a comment. If you miss the second step (confirming the account), like I did (yes, I know, this wasn't very clever by me), you most likely will loose your comment. At least this happened to me.

There seems to be a session timeout for the CAPTCHA, which is OK. But unfortunatley, you don't get an appropriate feedback, when (for example) trying to preview your comment. So the privew does not work and you don't know why. Might be because you entered the wrong CAPTCHA, or you entered the wrong password, or you took too long to write your comment. And in the latter case, you don't even get logged in, even if you entered your correct credentials.

When I submitted my comment, I didn't realize, that this was a moderated blog. I finally noticed this after submitting the comment the second time. So you probably got my first comment twice (sorry for that).

Regarding spams: Do you really get spams? I thought the CAPTCHA mechanism was meant to prevent this.

PS: I didn't mean to offend you and I don't know the standard Blogger mechanism, but leaving comments on other blog sites seems to be much simpler, that's why I wrote the comment. I also know, that it is not _you_ who makes leaving comments here so hard, rather it seems to be the Blog service.

Regards, Dirk.
Post a Comment

<< Home

back to

This is Markus Voelter's Blog. It is not intended as a replacement for my regular web site, but rather as a companion that contains ideas, thoughts and loose ends.

December 2005 / January 2006 / February 2006 / March 2006 / April 2006 / May 2006 / June 2006 / July 2006 / August 2006 / September 2006 / October 2006 / November 2006 / December 2006 / February 2007 / March 2007 / April 2007 / May 2007 / June 2007 / July 2007 / September 2007 / October 2007 / November 2007 / December 2007 / January 2008 / February 2008 / March 2008 / April 2008 / May 2008 / June 2008 / July 2008 / August 2008 / September 2008 / October 2008 / November 2008 / December 2008 / January 2009 / February 2009 / March 2009 / April 2009 / May 2009 / June 2009 / July 2009 / August 2009 / September 2009 / October 2009 / November 2009 / December 2009 / January 2010 / February 2010 / April 2010 / May 2010 / June 2010 / July 2010 / August 2010 / September 2010 / October 2010 / November 2010 / December 2010 / January 2011 / March 2011 / April 2011 / May 2011 / June 2011 / July 2011 / October 2011 / November 2011 / December 2011 / January 2012 / February 2012 / October 2012 / January 2013 /

You can get an atom feed for this blog.