Thoughts on Scala

October 30, 2013

For about a month now, I've been working on a backend service in Scala. This service is simply a REST API: It fetches a string from redis, parses it as JSON, extracts some keys, serializes it as JSON, and returns it. Pretty simple stuff. I've also just finished the Coursera class Functional Programming with Scala. I'm still a beginner to Scala, but here are some my thoughts on the language thus far.


After working with languages like Python and Javascript for a while, I got used to not having to worry about types too much. I didn't have to enforce strict data formats all the time, and enjoyed the fact that JSON and Python dictionaries were pretty similar. With Scala, I no longer have that pleasure. JSON serialization/deserialization was much more difficult to do. In Scala, there are way too many JSON libraries, with varying degrees of easiness. I started with Lift JSON, then moved to Play JSON, but usage was similar for both. To read JSON in Python, all I had to do was this:


With Lift JSON, I had to do this:

val listItem = (parse(jsonData) \ "key1").extract[List[Map[String, String]]]
val dataThatIWant = listItem(0)("key2")

And that was if listItem was a nice list of maps. If it wasn't, well, then I would have to create a case class for the items in the list, and maybe throw some Options in there. Yuck. The same thing applied to creating a JSON object - if I just wanted a simple Map with some more Maps and Lists, I would usually have to create some case classes. No more arbitrary dictionaries anymore, everything had to be well defined in advance.


An option is simply a construct that can either have a value or be null. The usual way to handle options is to pattern match, but I found this to be too verbose in situations where I knew the value was present. I'm still not sure about the usefulness of options right now, but some people love them.

Functional Programming

I never really did true functional programming until now, but I have to say, after completing the Coursera class, I really enjoy FP. I had a lot of practice doing recursion, and now I can write recursive functions without blinking, whereas before I would have to think a little. Immutable variables was hard to work with at first, but once I got the hang of it I saw that everything could be done immutably. Higher order functions and comprehensions are all pretty intuitive (flatMap might be the only weird one).


I'm not a big fan of optional dots and parentheses, since it sometimes makes the code hard to read. I have the same complaint about Coffeescript.

Overall, I really like Scala and functional programming. There are some oddities, but nothing too crazy. To continue learning, I've signed up for the next class in the series - Principles of Reactive programming, taught by Martin Odersky again. It basically teaches the Typesafe stack, which is great since I'm already using the Play framework. I'm also reading Martin's Programming in Scala, and reading through various Scala projects on Github. Scala is a cool language, and infinitely more tolerable than Java.