Syntax Sugar

I have been doing a lot of reading lately about MDSD and DSLs and I have to admit the ideas they suggest are rather seductive. I don’t mean that in a negative way, I simple mean the end goal of these two techniques are to bring the developer closer to solving the problem domain in an appropriate language. Being a rails developer in my spare time I have already been introduced to numerous DSLs with out even knowing it. Some may argue that rails itself is a DSL but based on the research I have done, the big pushers of DSL believe that rails is actually a collection of DSLs. For instance ActiveRecord is a DSL for expressing actions in a persistence domain, and ActionPack is a DSL for expressing how you want a request to be routed and processed.

After reading an article by Martin Fowler I realized that there are two different approaches to creating a DSL. He refers to these two different approaches as internal and external DSLs. An internal DSL is one that can be expressed in the native language of the environment in which you are working (e.g. ruby), and external DSLs are languages that must be “compiled” to a parent language before being executed. You will typically see external DSLs in Java, C#, or most non-scripting language environment.

The main advantage to internal DSLs is there is no additional logic that needs to be written in order to start working/writing your very own DSL. The reason why ruby and basically all other scripting languages are so great for writing DSLs is because they are founded on the notion of having syntax sugar. What I mean by that is they have multiple ways to express the same statement. In the olden days this was frowned upon, and I am sure many Java and C++ developers will cringe at the idea of writing an if statement 4-5 different ways. They might tell you “You should always write it the same way so that everyone is writing there code, and can interpret it the same way”. The problem with this is you are writing your code in some weird syntax to increase readability, when you could just be writing what you mean. Let me stop jabbering and give some examples:

hotels.each do |hotel|
  if ( hotel == selected_hotel ) {
    hotel.reserve()
  }
end

Everyone who understands enumerates and if blocks should understand what is going one here. But you could also right this statement like this:

hotels.each do |hotel|
  if hotel == selected_hotel
    hotel.reserve()
end

more importantly you could write it like this:

hotels.each do |hotel|
  if hotel == selected_hotel == hotel.reserve()
end

and if you wanted to, you could write it like this:

hotels.each do |hotel|
  hotel.reserve if hotel == selected_hotel
end

Now from a developer stand point I am sure all of these make sense except maybe the last one. But to someone who hasn’t written any code before the line:

  hotel.reserve if hotel == selected_hotel

will make perfect sense. That is the purpose of syntax sugar and DSLs.

Now for good measure I am going to throw in an example to help a co-worker out…who will remain nameless. One of the nicer features of Ruby is the unless construct. Now in simple terms

unless == if not

but to give an example:

print 'The select hotel is not available' if not hotel.available?

is the same as saying:

print 'The select hotel is not available' unless hotel.available?

I hope these examples make sense, and nameless co-worker, I hope this helps you understand the unless construct.

This entry was posted in languages, programming and tagged , , , . Bookmark the permalink.

One Response to Syntax Sugar

  1. Pingback: Roop says » Blog Archive » JUnit 4.4

Leave a Reply

Your email address will not be published. Required fields are marked *