Groovy for Business Software. Why not?
Dynamic scripting languages have existed since the 1960′s. However, recent years have seen the dramatic evolution of scripting languages into Tour-de-Force langauges that are now, in certain applications, employed instead of traditional languages. One such application is Web-based Software, where languages like PHP, Ruby, Python, and PERL are the most popular.
And it’s clear why. Production times & costs can be severely reduced by using a scripting language. People from other disciplines can even participate in the coding process, such as designers and analysts. Not to mention, reducing development times by astronomical figures. With so many benefits, it is clear why scripting langauges like PHP, Ruby, and Python are championing the web.
And then there’s Java. Web-based Business Software would not be the same without Java. Sure, it is a compiled language, even if it’s a lot simpler than C/C++, but it allows developers to write Enterprise-ready software that can withstand the stresses and hardships of the Enterprise with diligence. But production time/costs are not nearly as good as those achieved by writing applications one of the dynamic scripting languages, like PHP or Ruby.
Enter Groovy. With the syntax and strength of Java, and backed by all the Enterprise-tools available to Java, Groovy provides an amazing tool to leverage the best of both worlds: the dynamic and versatile nature of scripting languages, and the grace and solidity of Java.
Groovy provides software developers the option of reducing production time and costs, but why is it not being used as much to develop Business Software? After writing several applications for the Enterprise, I can say with certainty that Groovy inherited all the strengths of Java, except that of speed. During process-intensive scenarios, Groovy can become haltingly slow… but in any other situation, it’s speed constraint is not even noticeable.
Benefits of using Groovy with Java
First, I’d have to say is the ability to use any library written from Java within Groovy. This is huge. It means that you can use anything from your own Classes and your own Libraries, up to invoking Java EE API’s and Libraries, like JPA or JavaMail.
Second, is the language’s versatility. It’s as versatile as Python, with features such as Array Slicing and Ranges, Super Strings (GString), and (my favorite) Builders. Builders are Groovy Utility Classes that allows you to create Tree-like structues in the code, which are both descriptive and flexible. These make use of Groovy’s Closures and the Meta-Object Protocol (MOP), and the result is code that naturally resembles the intended result. Look at this example of creating XML with Groovy, using the MarkupBuilder:
def writer = new StringWriter();
def builder = new groovy.xml.MarkupBuilder(writer);
builder.carList {
for (make in ["Honda", "BMW", "Cadillac"]) {
car(make: make) {
type("sedan")
year("2010")
used("false")
}
}
}
def xml = writer.toString();
Could this possibly be any simpler? And you can almost see the resulting XML jsut by looking the Code! It took 12 lines of code to generate the following XML:
<carList>
<car make="Honda">
<type>sedan</type>
<year>2010</year>
<used>sedan</used>
</car>
<car make="BMW">
<type>sedan</type>
<year>2010</year>
<used>sedan</used>
</car>
<car make="Cadillac">
<type>sedan</type>
<year>2010</year>
<used>sedan</used>
</car>
</carList>
And so many other things, like using SQL, for example, are just as simple when using Groovy. There are so many features that make Groovy an excelent language to support Business Software development, but I’ll leave that for another post.
In conclusion, I have learned from experience to trust Groovy as a supporting language (using Java as the main language) for serious software development, including Enterprise-grade applications. It can be as secure as Java, and although it can be slow, you can always code the process-intensive operations in pure Java. This post is the first one of what I intend to be a series on Groovy for Business.









You said:
“Could this possibly be any simpler?”
My answer:
Please, check C# or VB.NET or F#.
@georani.
Hmmm, I’ve been having a lot of trouble getting those to run well on Mac and Linux
@georani
For sure, there are many great languages that can be used to optimize development… but for developers coming from Java, Groovy is a great choice because of its integration *with* the Java platform, and its ability to leverage every existing Java API.
@hohonuuli
Exactly right!
and used = false?
“Could this possibly be any simpler?”
Yes…
def writer = new StringWriter();
def builder = new groovy.xml.MarkupBuilder(writer);
builder.carList {
["Honda", "BMW", "Cadillac"].each {
car(make: it) {
type(“sedan”)
year(“2010″)
used(“false”)
}
}
}
def xml = writer.toString();
(I know it’s not the point of this article, but I wanted to share it anyway)
Good point. Implicit iterators *are* an important part of Groovy.
[...] worlds: the dynamic and versatile nature of scripting languages, and the grace and solidity of… [full post] Mario J. Wunderlich WunderlichSoftware Blog groovyhighlightslanguagesprogramming [...]