Wednesday, July 9, 2008

Use groovy as your scratchpad during java development

How many times have you wanted to test out a small piece of java logic before implementing it in your code?

In the past, whenever I did something 'smart' with Dates or Calendar or Strings or StringBuffers, I wrote a small java program (often called Test.java) to test the logic I had in mind. The process, as we all know, is slow to do in java because of Java's write-compile-run cycle. Even if you are using the most sophisticated IDEs, it is still a chore.

Enter Groovy

Groovy is a dynamic language written for the Java virtual machine. You can use Java syntax in a perl like language. While Groovy is a full featured new-age programming language (I am having lots of fun learning it by the way), I use it more often to test out bits of java logic before I want to introduce it into my code.

The other day, I wanted to check how GrgorianCalendar's firstDayOfWeek is affected by the Locale. Normally, I would have written this in Test.java but not any more. I opened the Groovy console and typed in the code snippet I wanted to test. I didn't have to create any class or import anything (java.unit.* is imported by default). There was no manual compilation needed (because it happens under the hood). All I had to do was type my code and press Ctrl-R. Presto. The output was displayed in the output screen. I changed the Locale in the constructor and again pressed Ctrl-R to re-run. No recompilation, nothing.






I recommend this tool to every serious java developer as a productivity tool. Of course once you get hooked to Groovy as a programming language, I am sure you'll find it as interesting as I do.



No comments: