Wednesday, April 23, 2008

Java Programmer looking at Groovy

Some things that might help you when looking over Groovy code with "Java" eyes:

  • everything is an object!
  • method parenthesis are optional
  • semicolons at the end of the line are optional
  • elements in brackets are either a List() or a Map()
    • Lists are comma separated elements
      • def myList = ["one", "two", "three"]
    • Maps are name value : pairs separated by commas
      • def myMap = [dairy:"cheese", meat:"chicken", shopper:"Mario"]
  • the return keyword is optional
    • by default the result of the last statement in a code block is returned
  • The questionmark protects you from null pointer exceptions
    • println myObj?.myAttribute will print myAttribute if myObj is not null, no error thrown
  • Strings can be single or double quoted
  • Double quoted strings can have variables nested with using the ${ var } syntax or simply $var
    • def var = "Crusty"
    • println "The clown $var unnerves me"
  • The ampersand allows you to call a method from an object
    • println myObj.&someMethod
  • The at symbol allows you to directly address a classes attribute
    • myObj.@fieldName = "somevalue"
  • A range of elements can be defined using an elipsis
    • (1..10).each {println "toe number $it"}
  • Now that opened up a couple of other items....
  • Code in braces is called a closure (see other blog "What are Groovy Closures?")
  • the keyword "it" is the default iterator for a closure
  • The double less than symbol is used for appending with StringBuffer, Lists, Sockets, Files & Writers
  • Named Parameters
    • For example, create a new user with a name and ssn provided
    • new User (name:"Gorge", ssn: "123-45-9999")
  • Parameter Defaults
    • def myMethod (String name, String type="chef") { //somecodehere }

What are Groovy Closures?

One of the more difficult concepts that I had to get my head around as a Java developer was the concept of Groovy closures.

Closures:
  • are classes (defined by Closure.class)
    • like defining new Closure() {}
  • are similar to anonymous classes
  • are defined with braces { and }
  • provide one default iterator parameter (if none specified) called "it" for iterator
    • (1..3).each {println "number $it" }
  • can accept parameters
    • (1..3).each {someNum -> println "Some number $someNum" }
  • can be predefined with a variable name and invoked (they are classes after all)
    • def myClosure = {x, y -> println "you sent $x and $y" }
    • myClosure 'Feet', 'Hans'
    • prints: you sent Feet and Hans
  • do not require an input parameter if its not used, do not provide one
    • def myClosure = { -> "returning this dude" }
    • println myClosure

Tuesday, April 22, 2008

Grails IntelliJ Plugin

The Grails plugin for IntelliJ Idea 7.0 was updated to version 1.5. I was using the previous versions and they were troublesome. The 1.5 release seems to be working well so far.

Plugin Site: http://www.jetbrains.com/idea/features/groovy_grails.html

One point of note, I could not find a way to perform "grails clean" within IntelliJ so I have to have a command prompt open to clean the project now and again when things don't seem to be behaving as expected. Sometimes you have to clean when one of your domain classes changes significantly.

New Grails User

A friend of mine, Ken Rimple, recently turned me onto Groovy on Grails. When I first heard of it, I wasn't interested in learning another something on something language/framework pair. But after a quick spin through the tutorial I was hooked.

The best thing about it is that its all based in Java and deploys as a war on your favorite web server. As a matter of fact it builds upon many of the top Java technologies such as Hibernate, Spring, log4J, JUnit, Jetty, HSQL, Quartz, Sitemesh and ANT.

Now that I have my prototype ready, the devil will be in the details to get it finely polished.