This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
One of Groovy's nicest features is that it can be used as a scripting language, though in practice most people write classes instead. Still, scripts are fine, and I cover both how to write them and how to test them in the book. If you give me a typical example of what you do in BeanShell I'll try to mock out a Groovy equivalent.
Groovy's best feature to management is its seamless integration with Java. You don't have to replace any existing Java code -- you can just add Groovy to it and it'll work. You don't lose any knowledge or existing libraries. You just add new power in about 1/10th as much easily readable code.
Incidentally, the Spring framework supports what they call "dynamic beans" in three languages: BeanShell, Ruby, and Groovy. I have a whole chapter on Spring and Groovy integration in the book.
By the way, my whole first chapter is an attempt to make the business case for adding Groovy to your system. I like to say it this way: Java is good for tools, libraries, and basic infrastructure. Groovy is good for everything else.
This may be an unusual requirement, but one of the nice features of BeanShell is the "!= void" and "== void" checks, which you can use to check if a variable has or has not been previously defined. (This is of course separate from a regular null check, which checks whether a previously defined variable is null.)
Does Groovy offer something similar?
It's not a must-have, we can work around the absence with Groovy by switching from defining variables based on activity to putting the variable name and value into a Map, and then checking if the Map contains the corresponding key later.
Kenneth A. Kousen
gunslinger & author
Joined: Sep 18, 2002
I'm not aware of anything in Groovy that checks to see if a variable has been declared. Your proposed map-based solution ought to work, though.