Groovy is lightweight, dynamic, and provides a higher level of abstraction, but at the same time, you can mix it with Java. If you are interested in Aspects, you can do that with Groovy very easily using MOP or MetaObject Protocol. Groovy enhances Java instead of replacing it.

I hope I may join this discussion because for me it's the same question: "Why Groovy (or other dynamic and scripting languages)?

I've been searching quite long for real advantages of languages like Groovy but I still haven't found them. At least I can't imagine to use such a language for programming serious applications. Don't get me wrong, I would like to see some benefits in using Groovy where appropriate and perhaps better than Java. But a lot of the "features" of dynamic languages appear to me as disadvantages. For example all the useful compile time checks and IDE support (auto completion etc.) are missing or not really good. But most important for me is the big difference when you have to look at some piece of source code. Code in Groovy (and other similar languages) often may be much shorter or concise than the same code in a language like Java. But if it's not my own code and it's more than just a few lines, it's exactly this advantage which becomes a big disadvantage in my opinion. You can't figure out what types some variables might have, a lot of this useful "magic" going on isn't really easy to understand and so on...

So I see Groovy's place where you have to script a fast and small solution to a specific problem. Or if someone is new to programming and quickly wants to see results it could be nice. And it may come in handy when you don't have a powerful IDE to work with. Of course this is only my personal opinion. Perhaps someone finally can convince me when and why I should give Groovy a try. Or can "Programming Groovy" perhaps show me what advantages of Groovy I'm missing as a "normal" Java programmer?

But a lot of the "features" of dynamic languages appear to me as disadvantages. For example all the useful compile time checks and IDE support (auto completion etc.) are missing or not really good.

They're getting better, especially compiler checks. IntelliJ IDEA has done a terrific job leading the way. Autocomplete is fairly decent. As you learn to program more Groovily, though, you'll discover that you don't need to use autocomplete nearly as much simply because you write less code.

But most important for me is the big difference when you have to look at some piece of source code. Code in Groovy (and other similar languages) often may be much shorter or concise than the same code in a language like Java. But if it's not my own code and it's more than just a few lines, it's exactly this advantage which becomes a big disadvantage in my opinion. You can't figure out what types some variables might have, a lot of this useful "magic" going on isn't really easy to understand and so on...

Java is a butter knife. Groovy is a chef's blade. If you let a fresher in the kitchen, yes it's more dangerous to give him/her a sharper tool. From my perspective, the only advantage Java has here is when you're dealing with an inexperienced programmer because then the programmer will simply make less of a hack-job.

In some places our "chef" might realize that a portion of code is cleaner if static typing is used. You're allowed to do that in Groovy. You can go to normal Java ways when you need to.

I believe that people who write horrible Groovy code will also write horrible Java code. If some goofball starts passing around an indecriptive variable called flag (or worse, f), in Java I'm guaranteed that I'll know its type is boolean. This gives me a lowest common denominator for bad code in Java, somewhat like a safety net.

Follow a few simple principles like keeping methods succinct, and code will be easier to read.

Perhaps someone finally can convince me when and why I should give Groovy a try. Or can "Programming Groovy" perhaps show me what advantages of Groovy I'm missing as a "normal" Java programmer?

Why do you feel someone needs to convince you? If you're happy with Java as it is, then stick to it. But if you're hungry for a more powerful tool, Groovy's one of the best and most fun to explore.

Java is a butter knife. Groovy is a chef's blade. If you let a fresher in the kitchen, yes it's more dangerous to give him/her a sharper tool. From my perspective, the only advantage Java has here is when you're dealing with an inexperienced programmer because then the programmer will simply make less of a hack-job.

What a cool metaphor But that was indeed another problem I suspected to have when you use Groovy for bigger projects. On the other hand you're of course right. With most programming language it's possible for good programmers to produce good code and on the other hand for bad programmers to produce bad code. Perhaps I'll have to gain some experiences in practical use of Groovy. At the moment I'm still not convinced if it's better to leave it to programmers to produce better or cleaner code (or bad code as well).

Why do you feel someone needs to convince you? If you're happy with Java as it is, then stick to it. But if you're hungry for a more powerful tool, Groovy's one of the best and most fun to explore.

That's mostly because I don't belong to that kind of people who only like what they already know and don't even try to learn something new. And of course it's alway interesting to learn new technologies in particular if they could make daily work easier.

So you have convinced me in some points already. And I guess there's a lot of learning by doing like in many other areas of computer science. Which way would you recommend to get started with Groovy? Using it for daily scripting? Trying to integrate it in some parts of real application programming?

Originally posted by sreerupa basu: How good is unit testing with our old JUnit for .groovy classes?

Very good. I'm just a build monkey (release engineer) but we have a very large application here at The Broad Institute, and more and more developers are utilizing Groovy for their unit tests. Less boiler plate code and more abstraction means two things: It's much easier to construct meaningful, correct tests, which makes developers more likely to write said tests, and everyone's quality of life improves.

One test in particular went from hundreds of lines to less than 10, and readability actually improved! Groovy for testing is a win, pretty much hands down.

Daniel Jabonete
Ranch Hand

Joined: Apr 01, 2007
Posts: 84

posted Apr 08, 2008 20:55:00

0

I believed most of us here are in search (or hungry) of much powerful and at the same time fun to explore tools. But before integrating or introducing these tools in particular projects some justifications are needed, especially with the management. Some organization preferred or implemented a certain standard approaches. If it is just an enhancement it can be maybe consider later, if it is �mix� then that seems a red light.

Well, most of us just go on and learn new stuff and gradually forget the old stuff we haven't used in a while. This generally happens naturally. Does your memory throw an InsufficientMemoryError instead? [ April 11, 2008: Message edited by: Jim Yingst ]

How to get started? Fool around using the Groovy Console. This tool makes it pretty easy to quickly try new concepts in Groovy without an IDE. I had a lot of fun finding SwingBuilder examples online and dropping them into the Groovy Console. Voila! Instant Swing app!

How to introduce Groovy in a project? In some companies this may be an uphill battle. One of the easiest ways to start using Groovy is if you work on a project that's not doing as much unit testing as you'd prefer. You can easily set up Groovy unit tests and even choose not to commit your Groovy code to the repository if you think management would throw a fit. You find more errors in the code base and you get experience with Groovy - sounds like a win-win to me.

Marc makes a great point - unit testing is an easy way to sneak some Groovy into your project.

I've been really interested in Groovy and am currently on a project that doesn't use it. I'm maintaining an older application, and the app didn't have any unit tests at all when I started. I decided to write some units test using Groovy, and it's great! Groovy just looks so much nicer than Java, and it provides a lot of nice features that are great for unit tests. [ April 11, 2008: Message edited by: Josh Brown ]

thanks, i'm not "Terminator", but sometimes mark, that happy peoples are that one, that find "middle" beetween experience (application of learned stuff in reality) and lerning proccess... my oppinion -it can be wrong!