Apr 16, 2008

Groovy: No Class More Than a Screen Full

I'm coding in Groovy on a pet project. Along the way I've developed a rule of thumb: "No class more than a screen full" (i.e. 30 lines of code). This really works in Groovy**, and my code is cleaner for it. If I didn't use a browser I could rip out my PageUp/PageDown buttons. ;)

It's funny to look over earlier code where I'd write Map map = new HashMap(); and now just write def map = [:]. Duck typing is also pretty liberating. My pet project is working with byte streams, and I've been wiring components together with listener patterns. My listeners use write for their notification method. This means I can use anything that has a signature of a write method taking one parameter*. No need to cludge Adapters together. No need for any over-arching interface. E.g.:

  input.eachByte { ch ->
listeners.each {
it.write ch
}
}
and

class SomeClass {
def write(ch) {
...
}
and

someSubject.listeners << System.out << new SomeClass()
All very liberating stuff. I like Groovy. In fact Grails is capturing the imagination of colleagues at work. More about that another time.

*more or less
** for the project I'm working on at least. I don't have any imposed database markup on domain objects or anything like that that would automatically enlarge my classes.

0 comments: