Wednesday, April 24, 2013

Cool Stuff: Building a DSL

Code Camp is a great place to expand your thinking.  Whenever I attend, I like to go to sessions on topics that I wouldn't normally seek out on my own.  I've already dedicated the day to attending, so I might as well try out some new things.  (I also go to meet other developers -- find out what works, what doesn't work, and what's really cool.)

This past weekend during Desert Code Camp, you may have seen "MIND BLOWN" come across my Twitter feed (and if not, then you can follow me: @jeremybytes). This came from the session "Building a DSL with an OData Source".

I didn't attend this session because I had a particular interest in the topic -- I've never worked with an OData feed, and I've always thought DSLs (Domain Specific Languages) were complicated (more on this in a bit).  I attended because it was presented by a friend of mine from the dev community: Barry Stahl (blog: Cognitive Inheritance; twitter: @bsstahl).  I was amazed at what I saw.

The Scenario
Barry showed how to query the OData feed from StackOverflow.  StackOverflow is a good choice since pretty much every developer is familiar with the site.  To query the data in its "native" state (meaning, after Visual Studio has a chance to make a service proxy based on the Atom feed), the original query looks like this:


This query gets questions that were asked in the last 30 days that have at least 1 accepted answer.  There are a lot of details that you need to know to write this query.  For example, "Parent == null" means that this is a root element (meaning, a question).  And, "AcceptedAnswerId != null" means that there is at least one accepted answer.

The goal was to create a DSL that would save the developer from these details and make this query readable and discoverable.  Like this:


This version is much easier to read and hides the details of the original API.  You can get the slides and code download for this session from Barry's site: DCC 2013.1.

The Mind Blowing Realization
What was amazing about this presentation is how easy this is to implement.  I had always thought of a DSL as something complex to build.  This put it into the "you probably don't need to do this" category in my mind -- best left for the big brains who work on languages and compilers and that sort of thing.

But Barry showed that building a DSL can be exactly the opposite -- something easy and obvious.  He used a combination of helper classes and properties along with extension methods to shield the user of the DSL from the underlying details.

For example, the "Questions" property (from the FluentStack object above) wraps the "Posts where ParentID == null" part of the original query.  The other elements are similar wrappers that make the API easy to use and discoverable (through IntelliSense).

Very simple.  Extremely approachable.  Amazingly cool.

Wrap Up
I never would have thought of this approach on my own.  I still don't know if I'll end up ever working with an OData feed.  But I do know that I'll be using some of these techniques to make complex APIs easier to use.

Barry is really smart and a great presenter (plus, he's a good guy to sit around and chat with).  He travels to Code Camps around the country, so be sure to look for him at an event near you.

Code Camp is a great way to expand your horizons, learn new things, and maybe even be surprised from time to time.

Happy Coding!

No comments:

Post a Comment