Wednesday, July 2, 2008

Groovy or Scala? Don't be a Boiled Frog!

Both!

Groovy is a fabulous language. Just read Groovy In Action and some of the other Groovy books. Just about every page brought a Wow response from this Java developer. And the Grails web stack builds on Groovy to provide a quick and powerful website building experience.

With Groovy I've written several scripts to do JSON processing. Groovy allowed me to choose an existing well tested Java class library and use it directly in my script. There was no sifting through CPAN-like libraries of varying quality and performance. And in Groovy I could throw in a few assertions and try-catch blocks. Try doing that in a shell script.

And Scala? It's harder to find documentation that is accessible to Java developers. But learning Groovy makes it easier, because there are some similar concepts.

Ted Neward has written some accessible Scala tutorials at DeveloperWorks, such as The Busy Java developer's guide to Scala: Functional programming for the object oriented.

Most of the Scala documentation that I've seen takes you to the deep end of the swimming pool very quickly. And some constructs are not well explained. For instance, what does => mean? And does it have multiple meanings in apparently syntactically similar formats?

But I find Scala intriguing, and I read on, and write small educational Scala scripts and programs despite the steep learning curve. If there were no new concepts, why would a Java developer bother learning Scala? (To paraphrase Ted Neward.)

Groovy has a gentle learning curve and the payoff in programmer productivity is rapid.

Scala (for me, a Java developer) has a steep learning curve. Is it worth it? The intellectual pleasure is substantial. Significant amounts of Scala documentation are based on Structure and Interpretation of Computer Programs which is itself a pleasure to read and keeps you thinking.

Probably type safety, versatility and performance alone make Scala well worth a close examination. Type safety doesn't eliminate the need for testing, but can catch subtle and silly bugs. Versatility: you can script it (like Groovy), delve into functional programming, and you've got all the Java libraries available to you (like Groovy).

Speed may be the killer feature. I've seen performance stats that indicate Scala is about as fast as Java. And with immutable objects and the Actor class library, it is possible to write safe multi-processor programs. This means that you can write software that can take advantage of multi-core processors.

Scala is already fast. And with its multi-threading capabilities it will get faster, along with other languages such as Erlang and Fortress.

As single-core developers we are like frogs in the pot of warm water heating gently to boiling point. For a while we are developing and deploying on machines using yesterday's high-end processors. But with Moore's Law stalled at the level of the individual core, we will fall behind the multi-core performance possibilities if we don't learn to program in multi-core environments in a scalable way. Using Java's existing multi-threading libraries is not a practical option for most developers. It's hard. And that's what Scala (and Erlang and Fortress) makes easier. Not dead easy, but easier.

Learn Groovy. Learn Scala.

Don't become a Boiled Frog!

2 comments:

J. Suereth said...

I'd agree with your complaints about scala. Now that I got past the "initial hurdle" I'm now functional in the language.

I'd also agree with the statement that groovy has a shorter learning curve for Java developers. I tend to prefer Scala mostly from the static features.

Anyway I hope you get over the initial hump, because Scala is very rich in depth. I'm hoping after I make it through this barrier I can somehow make a tutorial or something for new-comers (from Java) to the language.

Also, => just started making sense to me. It's used in three contexts.

1) Pattern matching - between the case statement and the block to execute
2) Defining (or declaring?) function types:
val f : (arg1type, arg2type, ...) => (result1type, result2type...) = ...
The trick there is you can have functions of NO arguments f : => A
3) Anonymous Functions:

list.foreach( item => doStuffWithItem(item) )
here I define an anonymous function that takes an item and executes the given function with the item. You can take advantage of Scala's type inference here, but you could also write:
list.foreach( item : TypeInList => doStuffWithItem(item))

You probably know most of that, I'm just practicing for my new tutorial :)

Unknown said...

Thanks for your feedback. Good introductory Scala tutorials are thin on the ground. I look forward to reading your tutorial. Send me drafts if you like.

Mal.