[]
Groovy and Ruby
[ tirsen ] 14:58, Wednesday, 17 March 2004

As some may have noticed I'm a big Ruby fan.

Some wise person once said that learning a new programming language will change they way you think, if not it's no point learning a new language. I've done quite some bit of C# lately, but that hasn't really changed the way I think. It's basically more or less the same language as Java. Ruby fundamentally changes the way I think though. It's dynamically but strongly typed (strongly typed as: in Ruby "6" + "2" will give you "62" but in a loosely typed language like Perl or TCL that would give you 8) and it's got plenty of modern language features that really boosts your productivity.

People that think you only need one single language and that would give you everything you need should really try out some of these scripting languages for a while. In the beginning you're trying to do stuff like interfaces and abstract classes but after a while you just realize it's pointless. The compiler won't help you or protect you from yourself, there's only one way you know whether your code works or not: run it, test it. (In fact, this is the case in Java too, but the compiler gives you a false sense of security. I would say...)

I think Martin Fowler explains it best in his blog entry about enabling vs. directing. Ruby and other dynamically typed languages are enabling, they help a good programmer do a better job.

For this reason I really think it's great to see the Groovy JSR being submitted to the JCP. It might not be the only scripting language for the Java platform, but it's one that's particularly close to my heart. I think Java has always been a better platform for multi-language stuff than .NET so it's good to see this untapped resource being put to use.

Groovy's got a bunch of really nice Rubyesque features like closures, syntax for advanced data structures, inline strings and so on but it's also very tightly integrated into the Java platform. You can compile a Groovy script directly into a .class file and that's just going to be like any class in Java and vice versa you can reuse any Java class and library within Groovy. As James points out this binary compatability is way more important than syntactical similarities (although I must admit, I much prefer curly braces to the Pascalesque begin/end in Ruby). Groovy also avoids the magical implicit proc-variable and yield that is quite confusing and quite pointless in Ruby. In Groovy a closure is just like any other object, and you just invoke it. You can also do magical stuff like overriding invokeMethod. That sort of stuff is actually almost absurdly useful when doing framework stuff like remoting libraries, persistence or transparent proxies. (Yes, think poor mans AOP.) Unfortunately I don't see continuations making it into the language due to some quite significant JVM support for it. Continuations are really useful for implementing asynchrous processes like web applications without threads and can also be used to implement logic programming.

(And, yes, to Hani's dismay I will probably represent ThoughtWorks on the expert group once we get the paperwork sorted out. :-) )


Comments

I am a Ruby novice. What is the "the magical implicit proc-variable"? Thanks.

--Gary Blomquist, March 17, 2004 06:05 PM

I presume he refers to the:

def foo(a, b)
yield(a, b)
end

foo(1, 2) { |x, y| puts "#{x} -- #{y}" }

the closure passed to foo is the magical implicit proc variable which control is yielded to in foo via the yield keyword

--Brian McCallister, March 17, 2004 09:20 PM

Brian, that is indeed the magical implicit proc thing I was referring to. :-)

--Jon Tirsen, March 18, 2004 10:43 AM

I share your thoughts about the "adding languages may change your way of thinking". But it doesnt fit in every case. Like you said, adding C# as a java programmer wont help you in any way. Its just another syntax variant of java (roughly).

I programmed PHP 4 years long and even i didnt used it the last years, dynamic typed languages (losely or strongly typed) are so much different in using. Perhaps you can even see the PHP knowledge inside my Java code somehow.


Perhaps i should take a look at groovy, just for the sake of knowing the facts. Hey, this piece of software is only on my radar because of hani, what a marketing machine. Reading about a project on the bileblog is better than a news on TSS.

--Marc Logemann, March 20, 2004 11:43 PM
Post a comment









Remember personal info?