Wednesday, August 27, 2008

Quotes from an agile War Room (no. 1)

(Post inspired by Weiqi Gao)

Two developers discuss an urgent issue in an open war room. They decide to pair-program.

Developer A begins to type, pauses, and says:

My spouse and I argued this morning: what colour is my shirt?

Developer B quickly responds:

I believe there is a hue between blue and purple called irrelevant.

Monday, August 25, 2008

Groovy's -e and friends: The Command Line for Java Developers

At the risk of turning this into a Groovy blog, here's an extended comment on a neat post by Jesse Wilson.

With its Java-friendly syntax and command-line options, Groovy opens up some serious possibilities on the command line.

Check out the doc for all the details and examples.

Here are some of the basics:

The -e option

Groovy's -e option accepts a one-liner as the input program. Many languages allow this but few offer such tight integration with Java.

For example, we can format a date. (Note that I'll use Unix-style line continuations here, but these are a single line.)

$ groovy -e " println \
> new java.text.SimpleDateFormat('yyyy.MM.dd G') \
> .format( new Date() ) "
$ 2008.08.25 AD
Note how easy it is to experiment with the format string. I work in an open "war-room", and often use little snippets like this to answer questions on the behaviour of various Java libraries.

Here we use java.util.Random. (Note: Groovy imports java.util.* by default.)

$ groovy -e " println new Random().nextInt(25) "
$ 18

The -n option

The -n option instructs Groovy to use standard input. The doc asks if there is a bug, but it seems to works fine for these examples.

In this mode, Groovy will provide the line and count variables like so (note the content of the file is shown first):

$ cat abc.txt

$ cat abc.txt | groovy -n -e " println count + ' : ' + line "
1 : a
2 : b
3 : c
When combined with Bash, this opens up a whole new world.

Java RegExs

It is difficult to duplicate Jesse's program with a single line. However, it is easy to use Java's regular expressions inline.

First, build an input file:

$ echo " CodeToJoy" > foo.txt

Here is the Groovy, in multi-line form for clarity:

// The =~ operator applies line against
// a regex within the /'s, and returns a Java matcher.
// The regex is a plain old Java regex.
// If new to Groovy, 'def' is like 'Object' (for now)

def m = ( line =~ /http:\/\/([\w.]+)\S*/ )

// Using Groovy truth: if m matched, then the matching
// groups can be accessed via an array.

if( m ) println m[0][1]

The one-liner looks like this (again Unix-style line continuations here):

$ cat foo.txt | groovy -n -e " \
> def m = ( line =~ /http:\/\/([\w.]+)\S*/ ) ; \
> if( m ) println m[0][1] "
The Upshot

Grails gets a lot of press, but Groovy offers a lot of utility, even on the modest command line. This is especially true when combined with Unix shells like Bash.

Sometimes, I want to use other tools but this damn language just won't go away as a useful option.

Thursday, August 14, 2008

Groovy File IO : Staying at the Ritz


Every few months, I find myself writing a quick utility where, among other things, I want to copy a file. Usually within an hour, I want not only to copy the file but do a simple substitution as well.

I don't have a particular example here, but note that this usually involves some other computation: it isn't something as simple as a Bash script.

I've come to realize that the file IO aspect is often a good benchmark for the 'friendliness' of a language.


Imagine that this exercise is like needing a place to stay for the night. I just need some 'computational shelter' to get my stuff done. I'm not looking for anything profound or scalable.

Whenever I'm in Java, and I need to write this utility, I feel like I'm about to buy a house. There is a lot of paperwork involved and the process is not particularly fun. Plus, there is a conceptual mismatch: if I wanted to buy a house, that would be one thing, but I just want to stay overnight.

By contrast, when working with Groovy, I feel like I'm staying at the Ritz (a phrase I use often in person). I feel as though there are people attending to my needs, and they are friendly. Start the bath running, pour some wine, and relax....


Check out the examples below. The first is a straight copy. This is not an ultra-efficient use of Groovy/Java libraries but it does lead nicely to the second version with a substitution. Note that this is a glorious use of closures as the withWriter method will flush and close the file.

// straight copy (not optimally efficient)

new File('new.txt').withWriter { file ->
new File('orig.txt').eachLine { line ->
Even if you don't know Groovy, you can probably grok this code, which are complete programs. Note that x -> declares a parameter (named x) for a closure.

And with the substitution:

// copy with substitution

new File('new.txt').withWriter { file ->
new File('orig.txt').eachLine { line ->
file.writeLine( line.replace('code', 'joy') )

Check out the Groovy JDK and enjoy a luxurious evening tonight!

Tuesday, August 12, 2008

Don't Attend the St Louis JUG meetings

This isn't reverse psychology. It behooves me if you don't attend the St Louis JUG meetings. Several sponsors give away schwag and the quality has gone up substantially in recent months.

My chances are clearly improved if you don't attend. So seriously: just stay at home idly firing dopaminergic neurons by playing Guitar Hero or Twittering with your new online pals. Wheels down on the couch!

Bonus: if you don't attend this month's presentation, you'll miss a presentation on Classloaders by my friend and colleague, Charles Sharp. That leaves more time for me to ask questions.

Wednesday, August 6, 2008

Groovy Reference PDF

I have no affiliation with DZone. I enjoy the site and have received a substantial amount of traffic from there.

They have a variety of free "RefCardz": reference PDFs on various subjects.

And, now, there is one for Groovy! I can't resist reference stuff like this. Check it out here if you are interested. It is free, though registration is required (including address and phone number, unfortunately).

Other subjects include Eclipse and Idea.

Sunday, August 3, 2008

Greenfoot Hero versus Guitar Hero

First, a rare rant: I am mortified by the popularity of Guitar Hero. If only kids spent half that energy on a real instrument! I assure you that this lad didn't get 46 million hits on YouTube by fiddling with a video game. I don't begrudge Activision for their success, per se, but they could score big, BIG points with me if they donated even a tiny percentage of the earnings to supporting true music programs in schools.

Music aside, it would be cool if our culture celebrated writing software as entertainment, no?

Huh? You might ask. You expect students to enjoy setting up a Maven project, or solving Eclipse classpath issues?

Good point. Enter Greenfoot, a delightful framework for Java that provides both a platform for learning about programming and even a way to quickly prototype domain models and games.

Check out this article by my friend/colleague Nathan Tippy for more details, and a walkthough of some projects. I did, and was blown away by Greenfoot: it abstracts away the tedious aspects of development and provides the opportunity for endless creativity.

Wouldn't it be great if there were competitions and adulation for "Greenfoot Hero", where kids got together to exchange ideas and programming hacks?