Hacker News new | past | comments | ask | show | jobs | submit login
Groovy, the Python of Java (pixelmonkey.org)
80 points by memracom on Nov 18, 2013 | hide | past | web | favorite | 41 comments

Although I understand the point, I think it's more accurate to describe Groovy as the "Ruby" of Java rather than Python. Groovy's philosophy and syntax are all very Ruby-like, while many things about it are completely orthogonal to the same in Python.

Where Python is usually trying to have just "one way" to do things, Groovy offers you a million. Python actually share's much of Java's annoying desire to keep the default namespace clean and hence make you import modules to do just about anything useful. While Python puts simplicity and cleanliness on a pedestal, Groovy is unashamedly pragmatic, letting you do any dirty thing you feel like or need to do to get your job done.

In complete contradiction to Python's direction of eliminating it (eg: forcing brackets on print() statements) Groovy gives you incredible amounts of optional syntax - add semicolons? if you want ... or not ... except sometimes the syntax gets ambiguous they are required. Use brackets on your function calls? If you want ... or not ... except sometimes the syntax gets ambiguous, so they are required. The author didn't even mention optional static typing which not only enforces type correctness on parts of the code you apply it too, but dramatically speeds them up. So much freedom makes it easy for Groovy code to end up messy and unstructured compared to Python.

I'm currently in a situation where I switch between Groovy and Python coding on alternate days. I definitely prefer different things about each. Groovy definitely seems more powerful when I just need to get a simple job done. It is incredible what you can cram into a single line.

I hope Groovy continues to prosper, but I worry it's getting lost among the hype around more "sexy" languages. I think it's by far the best language for many jobs.

I don't think it's about sexiness -- Python has brought many associations to mind over the years, but sexy has never been one of them -- just that there are only so many languages that can compete in the same ecological feeding space that (Python,Perl,Ruby,Groovy) are all pretty much butting heads against each other over.

If anything, Python seems to be dominating in large part because of its unabashed lack of sexiness (or what passes for it in modern programming circles: cleverness, and cuteness).

> Groovy gives you incredible amounts of optional syntax - add semicolons? if you want ... or not ... except sometimes the syntax gets ambiguous they are required. Use brackets on your function calls? If you want ... or not ... except sometimes the syntax gets ambiguous, so they are required.

When the syntax get ambiguous, it's up to the developer to know they're required, just like with parentheses around expressions using infix operators of various precedences. The compiler or IDE won't tell you - you just get the incorrect result from running the code.

> The author didn't even mention optional static typing which not only enforces type correctness on parts of the code you apply it too, but dramatically speeds them up.

Groovy's original use case was quick and dirty scripts running tests, Grails scripting, and more recently Gradle, none of which use the static typing in Groovy. Gradle even still ships with Groovy 1.x. Unlike Groovy dynamic typing codebase, only one developer wrote the static typing codebase and it's still a little buggy. Best wait until Grails uses it before trusting it.

If I recall correctly Groovy was directly inspired by Python. I don't have a link, but I remember reading that the creator of Groovy was inspired after seeing a book on Python.

Actually, it is more like "Groovy, the Smalltalk of Java" :)

Didn't James Strachan, the creator of Groovy, basically say that if he could do it over again, he'd create Scala?

(a little Googling later...)

"Though my tip though for the long term replacement of javac is Scala. I'm very impressed with it! I can honestly say if someone had shown me the Programming in Scala book by by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy." [1]

[1] http://macstrac.blogspot.com/2009/04/scala-as-long-term-repl...

Edit: I should add that I'm considering using Groovy with a micro-framework called Ratpack (http://www.ratpack.io/), so I'm only bringing this up as a part of the conversation if someone's trying to figure out what they want to learn next.

Clojure is the thing that fills the flexible, expressive language niche on the JVM for me. This is to say nothing about Groovy, I've never touched or looked at it.

I think Clojure is great. I even wrote -- a year or so after writing this post -- that it might be a great language as a mind expander[1].

But Clojure is so dramatically different from both Java (the language) and dynamic languages more broadly (e.g. Python/Ruby), that it's a bit of a leap for workaday programmers; it's certainly no clean-up man.

[1] http://www.pixelmonkey.org/2012/06/13/clojure

> and dynamic languages more broadly (e.g. Python/Ruby)


It's a sign of the times when HN posters say "dynamic languages" and manage to ignore the Elder Language, the language from whence all dynamic languages sprung. And that language is...

The Land of Lisp http://www.youtube.com/watch?feature=player_detailpage&v=HM1...

(but I'd argue it wasn't until InterLISP and Smalltalk that all the pieces were there)


From a practical perspective, Clojure is pretty close to Python in day to day usage. The largest difference to adapt to for me (and a couple others that I worked with), was the absence of the imperative-style for loop/yield keyword combination in Python. To get something similar, you tend to need to rewrite into something with lazy-seq/loop+recur. Everything else (for work doing web development, data processing, and internal API development) is pretty close with favorable points to each depending on the task.

If you were already writing pure functions in python and then creating data flows using generator expressions or list comprehensions, moving to Clojure is very natural. If you aren't, and if your job is to move bits from one place to another performing transformations along the way, you were already having issues in Python and might want to consider the approach regardless of the particular language employed.

I really want to learn Clojure, but somehow my brain refuses to engage with Lisp-style syntax. I just cannot read it at all.

Groovy is perfect for me, because it has a familiar syntax, but it allows me to do much more in a much more readable way than Java would.

Groovy isn't just a language that runs on the JVM, it is a super set of Java. Valid Java is valid Groovy, so if you want to convert an existing project from Java to Groovy you just have to change the build scripts, and change .java to .groovy. Intermingling Java and Groovy classes feels natural because their syntax's are so similar.

Clojure is my favorite JVM language, too (alongside Kotlin), but I think Groovy can fill a different role even if your go-to language is Clojure. Groovy is great for writing non-lispy DSLs; one of those is Gradle, that's quickly becoming the de-facto standard JVM build tool, slowly but surely replacing Maven. Also, I think Clojure is better for "serious" projects, while Groovy is better for throwaway scripts (not that you can't do serious projects in Groovy or "non-serious" ones in Clojure).

> Groovy can fill a different role even if your go-to language is Clojure. Groovy is great for writing non-lispy DSLs

You can just stick the required syntax atop Clojure, using parsers and macro forms. For an example, see my https://github.com/gavingroovygrover/grojure

I did the transition from Java to Groovy (because of Grails) which helped me to become familiar with closures and a more functional style of programming, which eventually got me interested in Clojure.

I still think Groovy is valuable as a "better Java than Java" but it's kind of hard to explain to newcomers how much ahead Clojure is. It took me a while to leave my prejudices against the parens and try to understand why it was so powerful and why the LISP family of languages is still alive. It's not by mere coincidence.

I've tried Clojure. I just can't do it, I don't find it terribly expressive. I like functional idioms and immutable data but I just find Clojure to be a never ending sea of parenthesis and brackets that bury the expressiveness of the language.

What's needed is a Groovy-like syntax atop Clojure to get the best of both worlds: Clojure's power and Groovy's readability.

As I was pointing in some other post, I am always looking for some thing better than Java. My problem is Very Very Good IDE. Without that I won't work with any language no matter how much great it is. (Because my programmers not not smart). They live and think in Java and Eclipse. Can someone let me know where does Groovy Stand with when compared to Java + Eclipse?

Groovy works extremely well with Intellij Idea. I often mix Java and Groovy in projects and for the most part it's seamless. I'll start something in Java and as soon as I run into something that should be done with a closure or functional idioms that would require 100 lines of java instead of 2 lines of groovy, I just change the file extension from .java to .groovy, write it as it should be and move on.

This is Groovy's biggest advantage, but it is often left out of discussions.

Have you tried IntelliJ IDEA? They have a free community version that you can use: http://www.jetbrains.com/idea/download/

I switched to their Python IDE (PyCharm) and have never looked back.

Groovy has a great eclipse plugin that makes it pretty much seamless from Eclipse - it "just works" the same as if you are programming Java. Your programmers will feel right at home.

Yeah, the Groovy plugin for Eclipse is pretty decent. The "intellisense" function can be a bit slow at times, and I think there are just a few more times that it is unable to find the relevant auto-complete, as compared to working in Java. But other than those two minor nits, it mostly "just works" as you say. I'm a fan.

the vmware/springsource folks have their SpringSource Tool Suite (STS) which is a whole slew of plugins for eclipse. I think you'll be surprised at the quality of integration. Regardless, IDE would not be a major concern for me when it comes to using groovy for an old-fashioned java dev team.

In using Groovy for web development, checkout our product (www.crudzilla.com)

here's a short video demoing coding in Groovy and other jvm languages:


We use JSR-223 to support the various languages

I saw you make this same comment on another page. I'd like to learn more, but your website is very scant on details. All I can tell is that it allows me to code in the browser and has quite a few screenshots, but how about a technology overview?

There is high quality documentation bundled with the product. We might eventually put it on the website for users.

On the product home tab there is the link to the user guide and design document.

How about JRuby?

...why do we have both a Ruby-like language and a Ruby implementation running atop the JVM? Sounds like a lot of fragmentation and wasted resources.

They are two totally different use cases.

Edit: I guess I should explain this a little:

Groovy: A superset* of the Java language, you can (generally) rename a .java file to get a valid .groovy file. Groovy uses the Java libraries -- a Groovy string is a Java string. A Groovy object can subclass a Java object and vice versa.

Groovy is perfect for Java developers who want a gentle learning curve for new language features and want fine-grained interaction with other existing Java (and JVM) libraries and components.

JRuby: A full* implementation of JRuby and libraries that runs on the JVM. A Ruby string is not a Java string. JRuby can call Java and vice versa, but there's some impedance mismatch because of differences in the object libraries.

JRuby is great for Ruby developers and/or people who need to run Ruby-based technologies in the JVM environment (e.g. Rails, Asciidoctor, etc.)

Most people knowledgable of both technologies would not view this as (unnecessary) fragmentation. There has also been some work on the JVM that has benefitted both languages -- most notable the InvokeDynamic byte code additions to better support dynamic languages (benefiting Groovy, JRuby, JavaScript, and others.)

* There are some corner-cases where valid Java is not valid Groovy. Similarly, there are some C-language extensions in Ruby that aren't' available in JRuby. But in both cases the exceptions are rare enough to be relegated to a footnote.

Java 8 will make Groovy completely irrelevant: the lambdas will make Groovy closures redundant, and the bundled Nashorn Javascript will take away Groovy's use case for quick & dirty testing scripts.

I think it'll make them more efficient, and therefore more relevant. Groovy has many, many other improvements over Java than just closures.

> It is also the world’s first programming language that was written to target an existing community of programmers.

That is not true. Java was written to target C++ programmers.

There was a time when I would have wanted this - I was a python guy for years. But I've found I can be just as dynamic in scala (perhaps even more so, given implicits), without having to give up type safety. E.g. his example looks almost identical in scala:

    List("Rob", "Christopher", "Joe", "John").filter{ 
      _.size <= 4 
    }.foreach {
      println _

Or even more concise:

    List("Rob", "Christopher", "Joe", "John").filter(_.size < 5).foreach(println)

I always though that Groovy was inspired by Ruby.

It was, and it looks/behaves almost like Ruby as well. It also shares Ruby's/Perl's TMTOWTDI, sort of the antithesis of Python.

Not to mention the name is a homophone.

I thought Jython was the Python of Java?

Jython is Python on Java, in Java.

uh groovy is a clusterfuck for cats who learn about closures on the corner, hanging out with brendan eich or somesuch. gradle does seem like a nice buold system. regardless, java is some sort of cosmonaut condom, worth using mostly for the big software written in it behind a firewall of jvm free physical hardware.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact