Home > Uncategorized > API vs DSL

API vs DSL

Had I ever designed any domain-specific languages? Sure, many times. Like many people, I did it accidentially. As soon as I notice a function which uses one of its arguments only to dispatch control flow, something like this:

process(action, data) {
 if (action == Open) {
   open(data);
 } else if (action == Close) {
   close(data);
 } else if (action == New) {
   new();
 }
}

I know I’ve just got an interpreter. And action+data is a DSL. I don’t like dispatch code, because every branch means analysis complexity: you either need to keep a value of action in your head or track back to find out what is the value. I think that having an interpreter means that you are exposing a narrow generic interface. User of your API doesn’t have any clues on usage, and he cannot rely on compiler to check for errors. That’s why I consider interpreters as a code smell and avoid them. Good API is better than DSL.

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: