27 Nov 2011
Thoughts on Scala from a newcomer
I generally hate language wars. Yet, time and again, we somehow end up with debates about cancers, EJB2 and what not. I have been programming for a fair amount of time now (both in the functional and imperative paradigms), and for the past two months, I have been using Scala at work. I thought I will share some of my thoughts on developing with Scala, from a newcomer’s perspective.
To be frank, I found Scala disconcerting initially. The Java-like syntax gave me a false sense of familiarity, while tripping me up in areas where it diverged significantly from Java. For example, for most parts Scala uses Java-like (or should I say C++ like) curly braces,
if statements etc. But you don’t use
[ ] to subscript an array. You use
val arr = Array(1,2,3) arr(0) // 1 arr(1) // 2
I also found the concept of type inference pretty abstract. It gives you a false sense of a dynamic language, but in essence it’s just the compiler doing the heavy work for you. If you are coming from a dynamic programming background, this could confuse you once in a while.
Comparisons with Java
Scala, being on the JVM, is (sometimes unfairly) compared to Java – the language it’s touted to replace someday (as one of the Java.next contenders). Once I wrapped my head around the concept of type inference, I found Scala to be far less verbose, and far more productive than Java. Even when I am using Java libraries, I can simply use
vals to declare my identifiers. With the use of Scala’s implicit Java conversions, interoperability between Java and Scala’s collections is seamless. However, I have a major gripe with the Scala compiler – it’s really slow. On a largish code base, this could quickly end up grinding your hard disk.
I have been learning something new in Scala almost every other day for the past month or so. I find Scala to be a very rich language with lots of ways of doing the same thing. I overheard someone mentioning the other day how Scala is just too complicated. To me, Scala’s complexity is not about the depth of the language, it’s merely about how people on a team end up embracing them. With Scala’s power, comes a greater responsibility for the developers to use it wisely. The same could be argued about overuse of meta-programming in Ruby projects. I am currently working on a fairly large Scala code base – inherited from a team who were obviously on some tight deadlines to meet. If you took a look at the code base, you will see that the coding style varied greatly between all the different people who worked on the project. My takeaway here is that it’s important to set coding conventions in place when you are working with Scala – because the language simply offers too many options.
IntelliJ community edition with the Scala plugin offers a decent development environment. IntelliJ does not use the Scala compiler for the syntax highlighting, so sometimes I have come across cases where the code looks fine in IntelliJ (no syntax errors), but fail to compile from the terminal. Sometimes, it also tends to be really slow – taking upto 4-5 seconds for the syntax errors to fade away after I fix them. Despite these issues, you get all the powerful refactoring and code hinting features in IntelliJ. As far as IDEs go, I think JetBrains have completely cracked the problem.
Simple(?) build tool
I found SBT to be a powerful build tool for managing Scala (and Java) projects. However, I found it pretty daunting to pick it up because it uses some bizarre conventions based on cryptic symbols. For e.g. you need to separate each Setting in the
build.sbt file by a blank like. However, the
import statements at the top of
build.sbt need not be separated by blank lines. The documentation (although there was plenty accompanying the GitHub project), was sparse in terms of my ability to understand and follow it.
Scala somehow seems to evoke strong emotions in people. Just look at the scala-debate mailing group discussions, like this one. Maybe some people are looking at Scala as Java.next. But, that would be an unfair way to judge Scala. If you are looking to just ramp up your team of Java developers onto Scala, and expecting some magical increase in productivity overnight, I don’t see that happening. But, once you get past the initial learning curve of Scala, it definitely gets more done. That requires some persistence, and actually spending some time coding in Scala to get comfortable with the language.
You can follow me on Twitter right here.