Saturday, February 1, 2014

Why I prefer Scala over Groovy

Over the last couple years I've had the opportunity to develop software in Groovy and Scala. Now when coding in Java I always feel like it is so verbose, not a good use of my time, and just down right painful. Programming in Groovy or Scala is worlds better than Java (version 6). I want to briefly give an overview of both Groovy and Scala and why I prefer Scala.

A few benefits of both Groovy and Scala:

  • Closures
  • Functional Programming
  • Built in Singletons
  • Concise Syntax
  • Traits/Mixins

A few benefits of Groovy:

  • Dynamic language
  • Can easily test private methods
  • Seamless integration with Java libraries
  • AST Transformations

A few benefits of Scala

  • Static language
  • Pattern Matching
  • Options
  • Immutability built into language

I find that Groovy is really just a better syntax for Java with a little functional programming sprinkled in. Groovy has some AST transformations which allow you to create singletons however it does not allow for constructors. Groovy does not do anything to allow you to easily write multithreaded and functional code without side effects because it promotes POJOs with mutable state. If you try to refactor dynamic Groovy code you will not get a lot of IDE help and will have to rely on tests to catch everything. If I'm trying to figure out what methods are available on a Groovy class reference by a def then I will need to actually go and find the type which leads to a lot of switching back and forth. I also find people creating interfaces which don't specify proper parameter types or return types on the methods. I'm sorry but I don't want to have to dig into the code or tests to figure out how to make a call. Groovy also provides you a convenient question mark (?) operator which allows you to not have to do null checks however I find they litter the code.

Scala is harder to learn if you already know Java but promotes a different programming paradigm (a better one). Scala is another programming language and not just better syntax on top of Java so it does has a bit of a learning curve. Scala gives me built in singleton support with it's object type. Immutable object and collections are first class citizens in Scala language and are easy to create and work with. Immutability gives me the ability to not have side effects when doing functional programming and writing multithreaded code. It also makes immutable message passing in Akka easily implemented. I get good IDE refactoring and method suggestion support for Scala. With functional collection mapping and Scala's Option construct it eliminates the possibility of null pointer exceptions. Pattern matching gives me the ability to quickly react to and transform immutable messages.

One thing in Scala which may be a gotcha is the integration with Java packages like hibernate is not the seamless because they expect immutability. However there is usually a replacement Scala library or different way to do the same thing which works just as well or even better. One of the things I hear puts people off of Scala is IDE support but I use IntelliJ Idea and the support is very good.

We use Maven so there is always a plugin to mount in your POM for language support. One of the valid complaints I have for Scala is that it compiles quite slowly. I'd prefer the slow compilation over getting exceptions at runtime/testing time.

Reactive programming is the new thing which is not blocking and holding onto threads when you don't have to. I believe Scala is much better suited for this with for comprehensions used the Spray, Play, and Akka frameworks.

Both Groovy and Scala have trade-offs but in the end Scala offers me more so I prefer Scala.