The Artima Developer Community
Sponsored Link

Java Community News
Groovy or Java: How Does a Developer Decide?

13 replies on 1 page. Most recent reply: Oct 2, 2006 10:58 AM by Jules Jacobs

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 13 replies on 1 page
Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Groovy or Java: How Does a Developer Decide? Posted: Sep 21, 2006 12:17 PM
Reply to this message Reply
Summary
A recent DeveloperWorks article compares Groovy and Java code side by side, demonstrating Groovy's conciseness. Given the tradeoffs between Java and Groovy, where have you used one versus the other?
Advertisement

In a recent IBM DeveloperWorks article, Reducing Code Noise with Groovy, author Scott Hickey, shows several code examples written in both Java and Groovy to demonstrate how Groovy can reduce the volume of code to express the same idea.

For example, he shows a Bike JavaBean in both Java and Groovy. The Groovy example takes advantage of Groovy's default property semantics. If you simply provide a type and a name for an instance variable, such as String manufacturer, Groovy will make that variable private and provide a public set and get method. This makes the code much smaller, if what you want is a private instance variable with public get and set methods.

In addition, he shows an example where Groovy's duck typing (invoking methods based solely on descriptor, irrespective of the type of object) obviates the need for an interface that was required by the corresponding Java example. In this case Groovy allowed you to provide the same functionality with one type instead of two.

In a sidebar, the author admits that although modern Java IDEs can facilitate the coding of the Java version, because code is read more than it is written, Groovy's more concise code is still advantageous.

IDEs such as Eclipse make it easy to automatically create getters and setters. These tools also make it easy to extract interfaces from concrete classes to facilitate refactoring. I'd argue, however, that code is read more often than it is written. Unfortunately, developers still have to wade through generated code and source files to discern what a program is really doing. With the high quality of Java tooling available, it is debatable whether or not there is a huge savings in the keystrokes to create the Groovy source, but at a glance, you can see that the Groovy code is more concise, easier to decipher, and less complex. In practice, this is a powerful advantage across the scope of applications that have hundreds of classes and thousands of lines of code.

In the conclusion, the author makes the point that Groovy helps reduce the "noise" level required by Java:

All in all, using Groovy removes a significant amount of noise that accompanies our typical Java programs. And that's a pleasant sound indeed!

The author doesn't mention tradeoffs of that noise reduction, such as the fact that duck typing makes it harder for IDEs to perform refactorings, or for JVMs to optimize for speed. Nevertheless, the greater code conciseness that Groovy offers is important. For what kinds of tasks have you used Groovy? Where was it a good fit, where not?


Alex Blewitt

Posts: 44
Nickname: alblue
Registered: Apr, 2003

Re: Groovy or Java: How Does a Developer Decide? Posted: Sep 21, 2006 2:24 PM
Reply to this message Reply
Seriously, who codes in Groovy these days? There's probably more LISP programmers out there.

Youngrok Pak

Posts: 1
Nickname: youngrok
Registered: Sep, 2006

Re: Groovy or Java: How Does a Developer Decide? Posted: Sep 21, 2006 6:46 PM
Reply to this message Reply
We are using Groovy now. Grails seems better than Ruby on Rails, Django, Turbogears, etc. For web application, I think, Groovy is better than Java.

Michael Campbell

Posts: 5
Nickname: mcampbell
Registered: Sep, 2006

Re: Groovy or Java: How Does a Developer Decide? Posted: Sep 21, 2006 7:00 PM
Reply to this message Reply
I wonder seriously about groovy's traction with Sun hiring the JRuby guys.

robert young

Posts: 361
Nickname: funbunny
Registered: Sep, 2003

Re: Groovy or Java: How Does a Developer Decide? Posted: Sep 21, 2006 7:25 PM
Reply to this message Reply
marginally off-topic. I've not paid attention to Groovy since Mike Spille eviscerated it lo those many years ago. Mr. Hickey is clearly a fan. If anyone has been keeping track of it, and could answer whether the current version deals with Spille's objections; that would be helpful. If nothing's been done, well...

Isaac Gouy

Posts: 527
Nickname: igouy
Registered: Jul, 2003

Why don't you just use Jython, JRuby, bsh, rhino, pnuts, ... Posted: Sep 22, 2006 8:09 AM
Reply to this message Reply
Given the tradeoffs between Java and Groovy ...
Seems like the "Reducing Code Noise with..." article is based on a blatantly false dichotomy. The Groovy FAQ achieves more balance :-)

"Why Groovy? Why don't you just use Jython, JRuby, bsh, rhino, pnuts, ..."
http://groovy.codehaus.org/faq.html#why-groovy

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Why don't you just use Jython, JRuby, bsh, rhino, pnuts, ... Posted: Sep 22, 2006 10:51 AM
Reply to this message Reply
> Given the tradeoffs between Java and Groovy ...
> Seems like the "Reducing Code Noise with..." article is
> based on a blatantly false dichotomy. The Groovy FAQ
> achieves more balance :-)
>
> "Why Groovy? Why don't you just use Jython, JRuby, bsh,
> rhino, pnuts, ..."
> http://groovy.codehaus.org/faq.html#why-groovy
>
What I noticed about that article was that it only talked about the conciseness advantages of Groovy over Java, but didn't talk about any disadvantages. It did only compare Groovy to Java, and not other scripting alternatives for the JVM, but I didn't think that was unfair. It wasn't trying to compare all scripting languages for the JVM with each other and Java, just one of them (Groovy) with Java.

I was excited about Groovy when the project was young, because it was a "native" Ruby-like scripting language for the JVM. I tried it at a very early stage, and discovered it was not ready for prime time yet. I haven't attempted to use it since. The two places I had in mind in those days for Groovy or some scripting language was unit tests and web controllers. I noticed this article at devWorks:

http://www-128.ibm.com/developerworks/java/library/j-pg11094/index.html

It talks about unit testing Java code with Groovy. Does anyone use a scripting language to do unit testing for Java code? If so, is it worth losing the ability to refactor the unit tests along with the code under test via IDE refactoring tools?

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Bad Example Posted: Sep 22, 2006 11:02 AM
Reply to this message Reply
IMO, any code showing a "stupid, do-nothing" Java Bean is a poor example. Cause it's lousy OOP. I could care less if Groovy is better than Java at writing a bad design. But maybe that's another discussion.

As for the implementation of RentABike, might I suggest that he use Java 5, and a Map instead of a List? Since linear searching a list of 1000s of bikes would be bad. And make a method for addBike() in case you want to change it later.

Then the code becomes about the same length as Groovy, plus it's slightly planned for the future: (Note - there may be typos here, I didn't really compile this!)


public class MapRentABike implements RentABike
{
private String storeName;
private HashMap<String, Bike> bikes = new HashMap();

public void setStoreName(String name) {
this.storeName = name;
}

public String getStoreName() {
return storeName;
}

public MapRentABike(String storeName) {
this.storeName = storeName;
addBike( new Bike("Shimano", "Roadmaster", 20, "11111", 15, "Fair"));
addBike( new Bike("Cannondale", "F2000 XTR", 18, "22222",12, "Excellent"));
addBike( new Bike("Trek","6000", 19, "33333", 12.4,"Fair"));
}

public String toString() { return "com.springbook.RentABike: " + storeName; }

public List getBikes() { return bikes.values(); }

public Bike getBike(String serialNo) {
return bikes.get(serialNo);
}

protected void addBike(Bike bike) {
// TODO check for duplicates???
bikes.put(bike.getSerianNumber, bike);
}
}

Isaac Gouy

Posts: 527
Nickname: igouy
Registered: Jul, 2003

Re: Why don't you just use Jython, JRuby, bsh, rhino, pnuts, ... Posted: Sep 22, 2006 11:18 AM
Reply to this message Reply
It wasn't trying to compare all scripting languages for the JVM with each other and Java, just one of them (Groovy) with Java.
I think that worked well enough for the older articles in the "Practically Groovy" series, because they were more 'how do we do X with Groovy'.

"Reduce code noise with Groovy" seems like straight advocacy, and for that I'd like comparison with other JVM (scripting) languages not with Java.

nes

Posts: 137
Nickname: nn
Registered: Jul, 2004

Re: Groovy or Java: How Does a Developer Decide? Posted: Sep 22, 2006 1:50 PM
Reply to this message Reply
It is just the idea of providing good defaults at the programming language level. Constructing a new language like Java with good defaults is not trivial to do, because every user has his own preferences. It is not until a language has been in use for some time that certain patterns start to show up over and over.

If 80% of your users write classes with getters and setters, that should be the default. Maybe Groovy should be renamed ‘Java with defaults’. The problem with Groovy seems to be the maturity of the implementation and tool chain. I guess some prefer it over similar projects because of the closeness to Java. Tough call: “to groove or not groove”.

Roger Voss

Posts: 27
Nickname: rogerv
Registered: Aug, 2005

Groovy at No-Fluff-Just-Stuff conference Posted: Sep 23, 2006 7:57 AM
Reply to this message Reply
Just attended a Java No-Fluff-Just-Stuff conference last week and was interesting from this perspective. Last year's conference had a significant Ruby theme (with Dave Thomas, Bruce Tate, and others) talking up Ruby and Ruby on Rails.

This year there were no Ruby sessions and no presentations had much occasion to mention Ruby (or JRuby or Rails).

Instead there were sessions on Groovy the language and another on Grails. A session on embedding scripting languages dealt mostly with Rhino and Groovy as thematic examples. A session on Java embeddable rules engines (JESS, Drools, etc) used Groovy as the example rules script language. At a panel discussion where the question was asked what is the most significant thing on the horizon, one panelist said that 2007 would be the year of emergence for Groovy as a significant tool for Java developers.

Despite Sun's endorsement of JRuby it is Groovy that was center stage as the scripting language for the JVM at this conference.

Groovy is further along than JRuby, as for instance, it already does byte code generation and indeed Groovy scripts can be compiled to a .class file as opposed to executed-only. Hence you can write Java classes using Groovy and Groovy features while still generating a .class file that is incorporated into an application .jar file (will still need the Groovy .jar runtime at deployment).

There is also a feature that works similar to .jsp where a script file can be deployed, but is compiled and cached. When modified, that is detected and the script file gets automatically compiled again.

Grails is also quite far along. It could perhaps reach 1.0 milestone in a few months. So in 2007 both Groovy the language and Grails should be at 1.0 release status.

Roger Voss

Posts: 27
Nickname: rogerv
Registered: Aug, 2005

Re: Groovy at No-Fluff-Just-Stuff conference Posted: Sep 23, 2006 8:01 AM
Reply to this message Reply
I should also mention I've been using Groovy for a few months so am not reguritating things I heard about Groovy from the conference.

I use it with a NetBeans plugin and there is of course a Eclipse plugin available.

I do mostly testing and prototyping with Groovy.

I'm currently exploring the feasibility of replacing my Spring XML files with Groovy scripts which would use Groovy's very cool markup language feature.

Isaac Gouy

Posts: 527
Nickname: igouy
Registered: Jul, 2003

Re: Groovy at No-Fluff-Just-Stuff conference Posted: Sep 23, 2006 8:22 AM
Reply to this message Reply
Roger Voss wrote
> I use it with a NetBeans plugin and there is of course a
> Eclipse plugin available.
>
> I do mostly testing and prototyping with Groovy.

What answer would you give to Bill Venners' question "is it worth losing the ability to refactor the unit tests along with the code under test via IDE refactoring tools?"

Jules Jacobs

Posts: 119
Nickname: jules2
Registered: Mar, 2006

Re: Bad Example Posted: Oct 2, 2006 10:58 AM
Reply to this message Reply
A Ruby version of MapRentABike:

class MapRentABike < Hash
  attr_accessor :storename
 
  def to_s; "RentABike: #{@storename}"; end
 
  def add_bike(bike)
    self[bike.serialnumber] = bike
  end
end

Flat View: This topic has 13 replies on 1 page
Topic: Groovy or Java: How Does a Developer Decide? Previous Topic   Next Topic Topic: Why Add Syntax When You Have Ruby?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use