Archive for May, 2004

Groovy builders and the right way to kill XML

Monday, May 17th, 2004

I was reading Mike’s response to Michael’s “Groovy killed the XML file?”

Its actually not a very good example. You could of course do the same thing in java code, and just as unreadably.

But in Groovy you can write a builder that gives you much more readable code. For example, using a builder Michael’s example would look more like this:

b = new SpringBuilder() {
    bean (id:”messageSource”, class:”org.springframework.context.support.ResourceBundleMessageSource”) {
        property(name:”basenames”) {
            list {
                value(“org/springframework/web/context/WEB-INF/${message-file}”)
                value(“org/springframework/web/context/WEB-INF/more-context-messages”)
           }
        }
    }
}

and that is just as readable as the XML.

You can see more examples on GroovyMarkuo and in the AntBuilder unit tests.

In some very important ways it is more readable than XML. In particular, our brains read by picking up the start and end of a word, and groovy style syntax, by doing away with those pesky braces, does make it more easy to read. compare The readability of

The quick brown fox jumped over the lazy dogs

and

<The><quick><brown><fox><jumped><over><the><lazy><dogs>