Why Scala should be your next language (after Javascript)

I would like to make a case for picking up Scala as your second language after Javascript. My own experience has been fantastic so far, but I will draw fairly extensively on second-hand knowledge, as I have only brushed the surface of what it has to offer.

Scala has a longer ramp up, both in terms of overall learning and individual projects, but ultimately has a higher ceiling of performance, maintainability, and possibility.

What is this beast?

Scala is an object-functional language that compiles to Java bytecode. It was released in 2003 by Martin Odersky, author of the reference Java compiler.


Yep. While objected oriented (OO) and functional programming (FP) are often talked of as being at odds, Odersky views OO as orthogonal to a functional-prescriptive spectrum, with both ends interacting with objects in their own way.

Java bytecode?

Coming from Javascript land We rely on an engine such as V8 to interpret our code, and either Node or a browser to perform our IO tasks. The engine is responsible for translating our code into instructions at the OS level.

Many languages, such as C++, skip this step entirely and compile down to directly to these instructions. The downside to this approach is that you have to compile your code separately for each environment in which you want it to run.

Java takes a middle path with the Java Virtual Machine (JVM) which acts as an interpreter between binaries in bytecode and the system. This approach has been widely successful and the JVM is ubiqitous.

Scala piggybacks on the ubiquity of the JVM to be able to run just about everywhere. As a bonus, Scala can interact almost seamlessly with any existing Java code, which gives you access to an enormous library of existing packages.

Who uses Scala?

Scala has been around since 2003, with 2.0 landing in 2006, but it really didn't start taking off until Twitter announced that they would be using it for their entire backend.

Since then, a number of high profile companies have been moving to Scala, including Gilt, LinkedIn, Foursquare, Coursera, Swiss Bank, UBS, and Meetup.

What makes Scala awesome?

Scala and Javascript actually have a lot of similarities. The syntax of Scala will be familiar, as will the scoping rules. Scala’s package system will make a lot of sense if you’re familiar with Node’s module system. The Scala Build Tool (SBT) is a lot like NPM. Even the static typing system will quickly become unobtrusive.

So why bother with Scala at all if you're already a JS Wizard?

Standard Library

The first thing you are likely to notice is that Scala's standard library is robust and well documented. To be fair to JS, there is nothing unique here in the sense that all of these methods can be recreated in JS, but having them natively without needing import/namespace them is a breath of fresh air.

One thing that I particularly enjoy is that Scala allows you to do wildcard imports of methods. This means that where JS can end up littered with (for example) Math.someMethod statements, a single import scala.math._ at the top of a file gives you access to all of the math methods without any prefix.

Static typing

Scala is statically typed. This means that each variable can contain only one type of value (integer, string, etc.) for the duration of its existance. And each function specifies the type of its arguments, and the type of its return value.

Coming from JS this can seem overly restrictive, but it does come with a number of benefits. It catches a whole class of errors before you even run your program:

  • Did you return 1 somewhere in your function and true elsewhere? Caught.
  • Did you call a function with an argument of the wrong type? Not only will it catch the error before it even happens, but it will tell you what you did wrong and where. Contrast this to JS where you might spend an unreasonable amount of time debugging the guts of your function before even realizing that the wrong value is being passed in.
  • Did you change the signature of your function? Cool, you'll now get a handy list of errors showing you every single place in your program where you need to update the call to that function.

The result is that refactoring is generally safer and avoids some of the silent errors that are prevelant in JS.

As an extra bonus, you get all of the benefits of the type system with relatively clean syntax because Scala makes a lot of inferences for you and has sensible defaults. Some people familiar with Java have nightmares about the verbosity of the typing syntax. But you can largely avoid that in scala and instead get to write var a = 5 and it will assume that you wanted a to be an Int.

Everything is an expression / succinct syntax

This is actually one of my favorite parts of writing Scala, and one of the things that really encourages a functional style.

One area where this is particular noticable is with the if/then/else blocks. Where in Javascript you might do something like this:

var result;  
if (num < 10) { result = 'a'; }  
else if (num < 20) { result = 'b'; }  
else { result = 'c'; }  

In Scala you can instead write:

var result =  
  if (num < 10) { 'a' }
  else if (num < 20) { 'b' }
  else { 'c' }

Which doesn't look that different, really, but represents a very different way of thinking. Rather than defining our variable up front and running through our if/then/else block to mutate that variable based on conditions, we are thinking of the block itself as having a conditional value which we assign directly to our variable. Thus, rather than thinking in a series of chronological steps, we are using the block as a composable element that allows us to build our output.

Everything is an object / Scalable language

These two concepts are deeply intertwined, and are close to the core of what makes Scala so powerful and intriguing.

JS gets most of the way to "everything is an object", but has some oddities around how primitive values are handled. Scala embraces it fully.

This allows something very interesting. Take the expression 1 + 1. In scala this is merely syntactic sugar on top of this: 1.+(1). This is weird. Let's break it down.

The number 1 is an instance of the Int class. Rather than reserving the + and giving it special significance, Scala has instead adds it as a method of the Int class. Scala also supports infix notation which allows any method that takes a single argument to be written as object method argument without any dot or parenthesis.

Because the + is not reserved, it allows us to do things like adding together vectors or rational numbers or even algebraic equations in a way that seems natural to the language.

But this concept goes far beyond just the + and gets at the heart of Scala, the very name of which actually comes from the expression Scalable Language. It has been designed at its core to be extendible in a way that feels like a natural part of the language.

When to use Javascript

See this fantastic slidedeck comparing Node.js vs Scala's Play Framework

If I had to do it all over again I would absolutely learn Javascript before Scala just as I did this life. There are simply fewer barriers to getting going, and the lack of some of the more powerful abstractions in Scala gets you thinking more about the essence of programming at an earlier stage.

JS, and Node in particular, is very good when you want to move quickly and prototype. All of the power of Scala does come at some cost of complexity. Some of it is due to the language itself, but a large part of it is due squarely to the ecosystem, and the IDE.

Scala begins with the end in mind in terms of how you lay out your project and all of the practices it enforces. This can be rather cumbersome if you're just playing around with some ideas. The browser console is far superior to the Scala REPL. And ultimately the modularity (fragmentation, some might say) of the JS ecosystem supports a practice of adding only what you need when you need it.

In terms of long term production environment, Node does an excellent job as a middle layer between database and client as a webserver or API. It also does well in a microservices architecture system, where the lack of static typing is less likely to bite you during refactors.

Node is a bit of a one-trick pony, it just happens to be a really useful trick.

When to use Scala

There are a number of areas where Scala really starts to pull ahead:

  • Concurrent computing
  • CPU intensive tasks
  • Tasks where number precision is critical
  • Long-term maintainability of monolithic apps
  • Scientific computing

None of these may be relevant to you (or me) at the moment, but I predict myself writing more Scala and less JS as time goes on.

My motivation for learning Scala ultimately comes down to what I perceive to be a higher ceiling on what's possible, and giving myself room to grow as an engineer.