Never Underestimate Radical Vision

May 8th, 2008

Domain Specific Languages with Groovy

In general a DSL, domain specific language, is a language built to attack a specific problem domain and the lexicon of that language matches the problem domain. Huh?! What?! Is that even English?! In a nutshell a DSL is a language that maps to a very specific problem and the keywords are ones the users/developers of that problem space are familiar with. For example you have the problem domain of interacting with Apple’s iPod. Instead of using a general purpose programming language like Java you could develop a DSL that attacks the specific problem of interacting with and iPod. You could have specific commands like play, load, fast-forward, etc. Doing this in Groovy is really simple with method pointers. An example method pointer would be:

say = System.out.&println 

So lets setup the iPod interaction scenario and create a DSL around that. The first thing we need is a Song class to hold song information:

class Song {
  def title
  def artist
  def album 

  static Song newSong(title, artist, album) {
    new Song(title: title, artist: artist, album: album)
  }

  String toString() {
    "${title} by ${artist} on ${album}"
  }
}

Next we need an iPod class to hold that information:

class iPod {
  def static songs = []
}

This now gives us a basis for creating a DSL. We could use the classes out of the box but they are not in the language the use would be used to. Let’s abstract the user away from the class model and create a DSL they would be familiar with. We will do this by setting up some method pointers:

create_song = Song.&newSong
say = System.out.&println
play = { iPod.songs.each{song -> say song} }
load = iPod.songs.&add

Now we can write a little DSL script for interacting with out iPod:

load(create_song("Blue Collar Love", "StarFlyer 59", "Silver"))
load(create_song("Chasing Cars", "Snow Patrol", "Eyes Open"))

play()

This is just a small example of what is possible. There could be other ways to accomplish the same thing but I like this way. With the

ExpandoMetaClass

and meta-class programming combined with method pointers you can create a really interesting DSL that can help users or developers of your application by abstracting away the hard stuff. You could also embed a scripting language into your application by using a DSL.

This is just another example of what makes Groovy so "Groovy"

May 7th, 2008

Give me two Red Pills

I just got out of a session about taking the Red Pill – Groovy and Meta-programming. This session was flat out awesome! The speaker was excellent and his demonstrations were impressive. In my group at work we have been building applications with Groovy for about two months and I have already swallowed the Red Pill, but after this session I want a second dose. There is so much more about Groovy that I need to learn to truly unlock its power. The meta-programming aspect of the language is my untapped market. The speaker did a great job of showing how Groovy enables more productivity by having Java examples and then showing the Groovy equivalents. There were many oohs, ahhhs and laughter about how easy certain things are. What was really cool is how he javap’ed the Groovy class to basically show the bytecode and prove that it is really Java under the covers. He did a great job pf pointing out that he has heard that Groovy may replace Java and laughed that off. He basically said it would be live "icing replacing cake", it is not going to happen. Groovy is Java and they live happily together. This is a really excellent point. You can use Groovy without fear that you are cheating on your precious Java. I will blog hopefully later about some really cool code examples of things he showed with the MetaClass programming. This stuff is really super cool programming magic.

The takeaways really are threefold:

  1. Try it! Use in unit tests because the meta-programming can really make unit testing rock for even the hardest of problems. Not only does it rock but it is like a drug, the more you use it the more you need it.
  2. If you need to build rapid applications for proof-of-concept, prototypes, etc. on a JVM you really can not afford to not use Groovy. It is just Java but with the icing to let you get were you need to go quickly. Groovy is not better than Java it is just the icing on the cake. You can build real applications for very low cost in development time. Not only that it is easy for Java programmers to pick up because it is a world they are already familiar with.
  3. Groovy can be used and is used it production. Oracle, IBM, LinkedIn, Sun, Mutual of Omaha, etc. are all examples of companies that use Groovy in production applications. Is is slower than Java, sure, but only in milliseconds. Not everything about a business application is performance. They use it, you can too!

I strongly encourage you to watch this presentation when it is posted for FREE on the JavaOne web site. I will post a link when it becomes available. Be fore you make your mind up on Groovy give this presentation a watch. Give Groovy a try before you relegate it to the insignificant. I have taken the Red Pill and am about to swallow my second.

May 7th, 2008

It is all about YOU

So I am at JavaOne 2008 and had a great time listening to the Day One keynote/general session. Sun had some entertaining demos and the speakers were good. There were some very interesting messages they were trying push:

  1. Java + You – This is the main focus and message at this JavaOne. Sun is extending its focus from business to consumer. The new slogan is "Java on all the screens of your life", meaning phone, computer, car and TV. These screens should bring new an innovative content and applications to our life. The message was that Java is already there and will only grow to help us as developers deliver the apps that consumers want/need. This is a really interesting pitch. It was more like business, been there done that, the consumer/social ecosystem is where the new money is. How to role Web 2.0, RIA, social networking, mobile apps play into the new business model and how to monetize that market.
  2. JavaFX – this is the technology Sun is betting on for hitting the Web 2.0/RIA crowd. They introduced this tech last year but had more to show this year. I find this approach really interesting as there are already major players in this market (Flex and Silverlight). I am not sure what Sun wants to gain by inventing its own instead of integrating with Flash/Flex. We should see this play out over the next couple months. It may be just to leverage the ubiquity of the JVM instead of Flash. The really interesting bits was the ability to drag and FX app out of a web page and save it your desktop and use it like a desktop app. This was extremely cool! That seamless interaction between the Web and the Desktop is the holy grail in my opinion. When you can abstract away the idea of the connection then you can truly innovate the way we work with our apps.
  3. Mobile is a viable platform – Sun and Java have existed on the mobile platform for years and this was just extending the idea with FX running on the devices. This was really just a personnel message to myself that if I want to stay relevant I need to get into the mobile space a bit.

The keynote overall was really cool, very software rockstarish. Loved that they brought Neil Young on stage and showed how he was able to fulfill a dream by meshing BluRay and Java technology to provide a true interactive music experience.

I recommend anyone hoping online and watching the archived streaming keynote. It will definitely be interesting to see how the social aspect of Web 2.0 change the way we build applications. The focus on usability by non-techs will be huge in the future. Time to get creative!

|