Revelations about new Web development technologies are getting to be a regular thing. The natural energies and consequences of open source projects and the rapid fire innovations they spawn (prototypejs.org) or legitimize (Ajax and the back-channel feature of modern browsers), life for software development bears little resemblance to disk drive-grinding Pascal compilers of the not-so-distant past.
The newest revelation for me is what I'm characterizing as "playdough programming." For someone who, as a boy, couldn't wait to paint my car models before the glue had dried, a language like Groovy has changed the way I tackle new programming tasks. 1. Get it going (i.e., start some tactical coding), 2. validate concepts and strategies early, and 3. then begin refining your project into something that gets early feedback. And because it's still basically a Java app, (modestly) refactor it into compilable code (if you think it's a necessity) and leave the rest to the Groovy components as library elements that can natively interact with Java code.
A quick example. In my job I wanted to know about patterns in collections of files in various directories. So I quickly wrote the following:
def grabDataFromFiles(directory) { def listOfFiles = [] new File(directory).eachFile { f -> listOfFiles.add(f) } return listOfFiles }Pretty terse, eh?
listOfFilesis an ordered list of file names. And
defnot only declares the function, it also returns the type "linked list" because Groovy uses duck typing, which you can choose to leverage or you could just go with the more formal Java declaration
ListKeeping this to one page, let me list some of the things I enjoy and value about Groovy.grabDataFromFiles(directory) { ...
- Greatly lowers barrier to generating basic functionality
- Code is testable from the start.
- Easier to get there with constructs like : ${'A'..'Z'}.each {}
- 100% compat with Java (Java to Groovy, but not other way around)
- Great for newbies
My most pleasant surprise, besides the pure joy of using and exploring Groovy, is the amount of code re-use I'm practicing. You just feel much more like _not_ rewriting existing functionality -- probably because it's so easy to locate and modestly rework where differences demand.
So far I haven't mentioned closures. In a future blog entry, I'll explain the other 90% of why I'm smitten with Groovy. And yes, insert "Ruby" here if you like. Same value. Same new world.