Can I get down now dad?

Tramps like us, baby we were born to code

Groovy and Closures

Groovy and Grails (Ruby too) make a big thing of closures. A closure is defined, slightly unhelpfully, as:

A Groovy closure is like a “code block” or a method pointer. It is a piece of code that is defined and then executed at a later point.

If you are not use to them they can be tricky to grasp but they are hugely powerful and in many cases make code easier to read. See my cheesey code to print out the files in a directory:

groovyexp.png

This is using the Groovy eclipse plugin of course

The only problem with closures is that they are manna are from heaven to the lets-write-the-code-in-least-amount-of-lines-brigade. You know the sort of people, they chain a load of functions and objects together in one line and then stick a regular expression on the end. The technical term for these people, I believe, is bastards.

One Response to “Groovy and Closures”

  1. Rob McDonagh Says:

    Bastards, indeed. Sometimes we call ‘em feckers, too. :D

Leave a Reply