the blog for developers

Clojure vs Scala, Part 2


Photo by PhillipC

There are two languages stirring up the Java World: Clojure and Scala. Clojure a Lisp dialect on the JVM, powerful and pure and the Scala language a tight integration of object and functional programming. Which should you learn? Matt is wondering:

The two leading candidates in the JVM/concurrent/multicore arena seem to be Scala and Clojure. [...] Scala is a multiparadigm language, supporting both object-oriented and functional constructs. It is statically typed, yet offers very nice type inference. Clojure, on the other hand, is a functional Lisp derivative with almost no OO constructs that is dynamically typed. Both are designed to enable concurrent programming. Which one do I choose?

Clojure

Clojure as the first contender to the Java throne came out of nowhere and conquered the blogosphere in a storm.

Clojure [...] is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming. [...] Clojure provides easy access to the Java frameworks [...] Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system.

There is some traction with companies already, FlightCaster who predict flight delays use Clojure for backend code:

The research and data heavy lifting is all in Clojure.

Some obvious reasons that speak for Clojure:

  • Big momentum, mainly in the dynamic language (Ruby) avant-garde and in Lisp circles
  • LISP community
  • STM, software transactional memory, an easy way to share data
  • Very clever immutable datastructures
  • Well thought Seq concept, tightly integrated with Java

Depending on your standpoint there are some downers with Clojure:

  • no objects (might not be a problem for you)
  • hard to read for C tradition developers, this does include the many (), prefix notation and functional programming style in Lisp
  • no Lisp skills in most companies, harder to recruit than Java or Scala

Clojures Java Integration

Although it does not possess objects, astonishingly Clojure has excellent Java integration. This might be because one of the main arguments of Clojure it makes against other Lisp dialects is the Java plattform. So it does make a lot of sense of excellent integration.

  • Java methods as functions in Clojure
    (map (memfn charAt i) ["fred" "ethel" "lucy"] [1 2 3])
    
  • Collections in Clojure implement Collection
  • Functions implement Callable and Runnable
  • “Core abstractions, like seq, are Java interfaces”
  • “Clojure seq library works on Java Iterables, Strings and arrays”

Scala

Scala is the rising star on the JVM. James Strachan, the inventor of Groovy writes: “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.” Though one must admit the Groovy back than is not the Groovy from today. The question remains: What is Scala?

Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive. Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application.

The main reasons for using Scala:

  • Big momentum, mainly among Java developers
  • Java community
  • Has objects (might not be a plus for some)
  • Very powerful type system
  • Object oriented and functional integration

Some downers:

  • Functional programming can be difficult to understand for a Java developer
  • The type system is more complex than the Java one (might be a plus for you)
  • Actor concurrency in Scala is nice, STM feels more natural to lots of problems (shared state vs. agents), Akka could help
  • Scala is very powerful, some developers might shoot themselves into the foot

The biggest promise of Scala nevertheless is power and terseness. As I’ve showed in “Top 5 Things to Know About Constructors in Scala”, Scala has much less boilerplate code. This example in Java:

 public class Foo() {
   private Bar bar;
   
   public Foo(Bar bar) {
       this.bar = bar;
   }
   
   public Bar getBar() { 
      return bar; 
   }
   public void setBar(Bar bar) { 
      this.bar = bar;
   }
}

can be written in Scala in one line:

class Foo(@BeanProperty var bar:Bar)

Which is important, to a degree that you should drop setter and getters if not needed. I wrote in “Go Ahead: Next Generation Java Programming Style”:

If you have simple data holders or data transfer objects, don’t write boiler plate code with getters and setters. I know this is heresy in Java, but just use public fields.

Scalas Java integration

Scala has good, but compared to Clojure a rather basic integration. One can implement Java interfaces in Java, extend Java classes and vise versa. Scala isn’t tightly integrated on the API side – only with some in this context clumsy, unneeded Meta-Programming with impliciets. Scala promises Clojure like Collection integration with Java in the future – we’ll see. From the Java side things also do not look as nice, Scala classes and traits have lots of ugly named methods, until IDEs know how to deal with this, code completion is spammed by Scala.

As a side note, Groovy has also deeper Java integration that leverages the dynamic nature of Groovy. For example the “as” keyword.
With as closures or maps can easily be coerced to a Java interface. This goes beyond the Scala way of implementing interfaces.

def readable = { it.put("12 34".reverse()); 5 } as Readable

Nice, wish to have this in Scala – the map version if as will be hard to make play nice with static typing.

Conclusion

Andrew Glower wrote last year:

Is Scala or Clojure poised for stardom? Can two languages co-exist as stars or does one invariably outshine the other? If history is any indication, then I’d venture to guess that the answer is that both can’t be stars– just like both Groovy and JRuby aren’t stars. They co-exist and, in truth, divide the market and have followers with strong opinions on both sides. Thus, if I had to guess right now, I’d say that Scala has the edge with some good momentum.

I concur. The conclusion might not be what you want to hear. Draw your own.

Clojure has a lot of appeal to me, it looks so clean, the Java integration has such a pure feel, hard to describe. I do not buy the argument that Lisp is so much more powerful than e.g. Scala – so this doesn’t influence me much. And Clojure as a Lisp dialect is way out of the mainstream. It will make it hard to recruit developers – do not listen to people who declare this a non issue. I’ve been recruiting Java developers for years, and it’s hard. It’s much harder for Clojure.

Scala isn’t as pure as Clojure, not as en vogue. But it is pragmatic and powerful. It has a lot of momentum now and is easily learned by Java developers – at least by those that are open enough. The good ones usually are. The main promise and reason for Scala is how much less lines of code you need than in Java. Less code, less bugs, easier to read – to some extent – and faster to write, leading to higher productivity. Who doesn’t need higher productivity with the same safety as Java?

Which should you use in your company? Daniel warns on Stackoverflow, in “Scala vs. Groovy vs. Clojure”:

It’s also a much more dynamic language than Java, in the sense that the language evolve. From the perspective of the language, that’s a good thing. From the perspective of users who plan on having thousands of lines of code written on it, not so.

You need to decide on your own. Which would I use? Scala. Which should you learn? Both obviously.

What is your plan?

About the author

stephan Stephan Schmidt has been working with internet technologies for the last 20 years. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own. You can find him on Google +

Discuss on Hacker News Vote on HN