Java programmer since 1996. J2SE, J2EE Expert, Technical Leader, Team Manager. Rich experience in infrastructure and architecture of distributed applications Pavel has posted 6 posts at DZone. You can read more from them at their website. View Full User Profile

Groovy as "Better Java"

06.08.2012
| 13979 views |
  • submit to reddit
You can use groovy in different ways - for scripting, Grails, quick prototyping, creation of DSLs etc. But I like Groovy first of all as "better Java". Really, look - almost every Java code is valid Groovy code, i.e. - if you do not remember how to do something in a groovy way, you can write Java code in a Groovy class, but if you remember - use closures, Groovy lists, and many other nice features The only thing, that does not allow me to use Groovy in a production code instead Java - is the lack of compilation time errors in many cases - calling to non existing methods or variable, etc.

Many Groovy frameworks is using this (see, for example Groovy XML handling ), but if I'm writing regular code, I want to see here compilation errors. Finally, Groovy 2.0 provides a feature of strict compilation time checking. 

Let's take such class:

 


It passes a compilation w/o any problem.But if we will add  @TypeChecked (this annotation may be used for a class or a method), we will receive compilation errors:



 This type checking is turned on automatically also if we are turning on static compilation for the class, using annotation @CompileStatic: 
 

So now I can use Groovy for production code, instead of Java! Full list of compile time messages for @TypeChecked you can see  here.
Published at DZone with permission of its author, Pavel Bernshtam.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Murali Mohan Rath replied on Thu, 2012/06/07 - 7:36pm

Groovy for production.. not yet. Some of my colleagues use Groovy for few scripts and their opinion is Groovy not for production use. The implementation is not yet stable for serious applications. I have tried Groovy as well, I liked the syntax. I appreciate the work done for static compilation. But few things I wanted didn't work out of box, e.g. generating JPA meta model classes from groovy models.. Once you hit a bug, it gets difficult to progress. There might be work arounds etc but I want it to work out of box when I use it for production apps.

My opinion groovy is a great language with nice and clean syntax but the implementation is not good enough for production use. 

 

 

 

Ken Wong replied on Fri, 2012/06/08 - 12:25am

I am missing the wonderful Groovy++.

 Pity of human self-pride and a not-made-here reinvention of wheel.

 

 

Pavel Bernshtam replied on Fri, 2012/06/08 - 2:28am in response to: Ken Wong

As I understand Groovy++ ideas (and may be the code too) merged to Groovy 2.0

Thomas Eichberger replied on Fri, 2012/06/08 - 5:36am

I don't know why people say that Groovy is not production ready - it is - and it has been for some years now.

For me personally it is definitely the better Java :-) 

Mitch Pronschinske replied on Fri, 2012/06/08 - 9:26am in response to: Thomas Eichberger

I posted a poll on this question just now.

Let's see what JL as a whole thinks...

darryl west replied on Fri, 2012/06/08 - 9:26am

Groovy has been production ready for years now.  

Greg Allen replied on Fri, 2012/06/08 - 4:17pm

Some folks don't seem to be able to contemplate a "dynamic" or un-typed language as being suitable for production. Perhaps some university courses drill this idea into students. Them don't see to be aware the kernel of their OS is only "loosely" typed, with void* pointers being chucked around widely. Meanwhile substantial parts of the production system, including monitoring system, are likely written in shell script and perl...

IMHO, python and groovy are already suitable for production. It is the ability to write simple, concise and readable code that is going to improve the reliability of you production system, not type-checking.

Bob Jones replied on Tue, 2012/06/12 - 2:22pm

Compile-time checking is for people who don't write real tests for their code. It's a laziness scape-goat ;)

Abby Lays replied on Wed, 2012/09/12 - 6:55am

Most scripting languages are dynamically typed. You can usually create new variables without declaring the variable type, and you can reuse variables to store objects of different types. Also, scripting languages tend to perform many type conversions automatically, for example, converting the number 10 to the text "10" as necessary. I also write about: sejururi ieftine in turcia prin Genius Travel

Onea Florin replied on Sat, 2013/03/09 - 10:28pm

Onea Florin replied on Sat, 2013/03/09 - 10:29pm in response to: Onea Florin

Oferte Bulgaria  , e ok cu java asta nu imi pare foarte bun dar e ok , nu vad de ce nu ar fi ok  o sa il folosesc si eu pe la proiectele mele care le am sa vad daca am unde mie imi place ceva mai vechi decat asta

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.