5 years of Scala and counting – debunking some myths about the language and its environment

I started using Scala 5 years ago instead of Java after starting (but never quite finishing) to read the first edition of Programming in Scala. First only present in tests, my first attempts at Scala code would quite soon invade small utility classes and before I knew it take over all of my projects.

There are many rants about Scala out there. This isn’t one of them. I’m not here to complain, but rather to applaud.

This post is intended for developers curious about Scala or have heard about it some time ago but never quite got to look more into it, put off by a feeling of “well this is a show-stopper for me”. If you’re a Scala aficionado you are of course welcome to read it and share it.

It consists of 3 parts:
– my favourite “killer features”
– debunking some of the Scala myths that still float around in the JVM community
– some advice for learning Scala

Let’s get started!

Killer features

5 years without a NullPointerException

Well, almost. I have seen a few here and there but by no means can this handful of occurences be compared to the mountain of stacktraces leading to the infamous java.lang.NullPointerException that was polluting my server logs and my life.

NullPointerException

The mere thought of a NullPointerException now upsets me, bringing back memories of all those hours wasted starring at a screen and wondering where I or another programmer had erred. To err is human and no progammer should ever be caned for forgetting about the nullability of a reference. It is the responsibility of the language to forgeo this unfortunate turn of events by offering in its design an alternative that will just work (like Apple products did a few years ago).

I am not going to elaborate on Scala’s design choices for eradicating the NullPointerException (a combination of the Option type and of first-class functions) but suffice it to say that if you stick to the following simple rules you are almost guaranteed not to see a NullPointerException in your Scala code again.

  • NPE erradication rule #1: never, ever, ever (ever!) initialze a variable with null. All potentially undefined state must be modelled by an Option, period.
  • NPE erradication rule #2: always treat the undefined case right from the beginning, don’t cheat by calling a rogue get on an Option
  • NPE erradication rule #3: when using a Java library wrap the results it provides in an Option(unsafeJavaResult) because many libraries tend to sometimes, without so much of a warning, return null

And if you use Scala and don’t follow these rules and are plagued by a myriad of NullPointerException-s then you deserve it.

Empowered code organization

One class != one file

Scala does not care for how many classes you put in one file. And since those classes often happen to be orders of magnitude smaller than their Java counterparts, it is not uncommon to see many classes live in the same .scala file. Doing the opposite would honestly just feel weird and be a waste of time. You still use packages in Scala but the hierarchies aren’t as deep and the left-hand file browser of your editor as crowded with files as in Java (if you use a file browser of sorts, that is).

No matter what kind of editor you are using, the interesting result of this possibility of organizing code is that entire logical constructs can be self-contained in one single file which can be scrolled back and forth, hence avoiding to jump from one file to another which at the end of the day is a form of context switch (and as we all know, context switch is evil).

Shrink, Hide and Embody

Reduce

Scala makes it possible to apply John Maeda’s law of SHE (shrink, hide and embody), for example by nesting functions:

def someComplexFunction(parameter: Parameter) = {
  def theFirstStep = {
    // do something, possibly with parameter
  }
  def anotherStep = {
    // do something else, perhaps with parameter
  }

  theFirstStep + theSecondStep
}

Instead of:

def someComplexFunction(parameter: Parameter) =
  theFirstStep(parameter) + anotherStep(parameter)

private def theFirstStep(parameter: Parameter) = ...

private def anotherStep(parameter: Parameter) = ...

Outside of someComplexFunction nobody cares about theFirstStep and anotherStep (they are not being reused) so no need to pollute the class with them. By allowing to nest functions it is possible to reduce complexity and at the same time to contextualize intermediary steps of one function. Or in other words: when reading a class, I want to be able to focus on the big picture and not have to use an additional mental effort in order to filter out the noise.

This mechanism is much more powerful than the convention of placing “smaller” functions towards the end of a class and prepending them with the private keyword, since doing so still does not solve the problem of contextualizing – you still need to look around or use your IDE’s “where is this function being used?” feature in order to relate one function to another.

Traits for good and not for evil

One of the myths surrounding Scala is that by allowing for “multiple inheritance” it opens a pandora box which will almost certainly doom any large enough project. In practice I have seen very few deep class hierarchies in Scala projects. Traits are often used as a tool for composing related code. A neat example can be found in the /source:

trait Controller
  extends Results
  with BodyParsers
  with HttpProtocol
  with Status
  with HeaderNames
  with ContentTypes
  with RequestExtractors
  with Rendering

A controller in an MVC application has many concerns to deal with. All the types above (starting with Results) are traits that cover one of those concerns (in the original source code this is all in one line, but on this blog this wouldn’t read very nicely).

case classes or the Swiss army knife of Scala

case classes are arguably the killer feature of the Scala language, especially when coming from POJO-land. Case in point:

case class Point(x: Int, y: Int, z: Int = 0)

I now get for free:

  • a Point class
  • a constructor taking at least two parameters and that can be called with named parameters
  • getters for x, y and z
  • a hashCode method
  • an equals method
  • a method for creating immutable copies of immutable instances

And just look at this REPL session (more on the Read Eval Print Loop later):

scala> case class Point(x: Int, y: Int, z: Int = 0)
defined class Point

scala> Point(x = 1, y = 2)
res0: Point = Point(1,2,0)

scala> res0.copy(z = 42)
res1: Point = Point(1,2,42)

scala> res1.y
res2: Int = 2

The members of a case class are immutable, because immutability is the future. They are so quick to define it makes your head spin. And yes, Java IDEs can generate all the getters, constructors, and so on with the simultaneous stroke of 3 keys. But let’s be honest: that’s all just noise that makes it harder to focus on solving a business problem.

And even more

Truth be told, I haven’t written Java in what seems like a very long time. Which is why it is easy to forget about some of the little things that makes writing Scala code more enjoyable than Java one: no need for semicolons, type inference, tuples, pattern matching, etc…

Here is a good overview of the most annoying things one quickly misses when coming back to Java from Scala.

Debunking some Scala myths

“There is no good IDE support for Scala”

When I started using Scala on a regular basis, IntelliJ IDEA was the only IDE that had any kind of support for the language that went beyond simple syntax highlighting. On my old MacBook Pro it crashed every 10 minutes and the presentation compiler kept giving me false positives. I got used to the having to restart the IDE every 10 minutes and learned to differentiate “working broken code” from “really broken code”, only trusting sbt and the Scala compiler.

Things are much better now and unless you are doing crazy type gymnastics IDEA is well on top of things when it comes to Scala projects. Eclipse picked up with the Scala IDE and I think others have followed as well. The ENSIME project provides support for many different text editors (emacs, vim, sublime, atom) and there is good support for Atom.

“Scala has so many complex features, novice developers have a hard time getting up to speed”

One of the criticisms of Scala is that it has all those language features that are complicated to understand and that make it hard for novice Scala developers to join an existing Scala project or to work with it in a team. I would argue that this has much less to do with the Scala language itself than with the architectural choices made by the people building the project.

It is hard to find the right balance between shipping features and building a maintainable code base, a “tech lead” (or whatever you want to call the role) has to keep an eye on quite a few things at once. In my experience reducing the usage of fancy libraries and exotic language features and to enforce a consistent way of doing things is key to building a maintainable codebase with many people involved. Of course you need to have a firm grasp on the language and the tooling itself in order to tell apart “useful” from “fancy” and “exotic”. If everyone on the team is new to the language and platform you’re in for an interesting ride.

Technological bareers can easily be found in any language or platform, even Java. Given enough load-time weaving, bytecode modification, proxies etc. promoted by annotations a newcomer will have a hard time grasping the “magic” and run into trouble. I’m currently working on a project that has one of its systems written in Grails, and the GORM (Grails ORM, i.e. Grails on top of Spring on top of Hibernate) is confusing everyone.

Have you seen my Stacktrace?

To the contrary I would argue that Scala projects are easier on newcomers because there is a general consensus to avoid doing things at runtime, meaning that newcomers can count on the compiler telling them where their code is wrong, rather than having to walk through incredibly long stacktraces, often not understanding what is really going on.

“The sbt build tool is a jungle of weird-looking arrows”

The build tool sbt has seen an amazing evolution over the past years. It used to be a rather frustrating experience to work with it, especially on larger code bases, where dependency resolution would take eons. Luckily sbt rolled its own dependency resolution engine which fixes this really annoying issue.

When it comes to the weird-looking syntax sbt build files used to have things got much better as well. Heck, I can’t even find some of the funny arrows in my current project build files to show off here. And if you want to dive into the depths of SBT there is now a book about it

“The Scala compiler is sooo sloooow. Let me show you that XKCD comic to make fun of it”

No, I’m not going to display this XKCD comic here: since I have switched to a MacBook Pro Late 2013, compilation time is not an issue anymore. The compiler itself has gotten faster and sbt has good support for incremental compilation now. Of course it is still slower than Java, but it isn’t anymore the kind of slow that impacts productivity during development. Initializing all those Spring Beans or whatever it is that happens when a Java project starts these days (I’m really out of the loop here) takes much longer.

In case you were wondering why it is slower, the Scala compiler has much more phases than the Java compiler.

“I can throw all of my object-orientation knowledge out of the window”

Scala being a functional programming language does not mean that all the good parts of object-oriented programming should be discarded. To the contrary, since Scala is also a full-blown object-oriented language, all the good parts of OO can be used as well – and these are not mutually exclusive.

I have seen novice Scala developers get so much functional fever that they forget about the Open-Closed-Principle, cramming functions in object-s (Scala’s equivalent of static) and hard-wiring these all over the place, undermining the testability of the code.

“I need to become a mathematician and know all about Monads before I can get started”

Monad, mooooonaaaaaad

Ok, so this is a myth we have to blame the militant functional wing of the Scala community for (I mean I am sure they mean well, but it’s easy to scare people off with it). Don’t let yourself be lurred into reading one Monad tutorial after another, tutorial after another, trying to wrap your head around endofunctors and whatsonot. Unless you are a mathematician and you eat monads with maple syrup for breakfast, of course (I heard that that’s a thing).

But seriously now: don’t let yourself be scared off by “category type theory” and other fancy-sounding words. You do not need all of this theoretical background in order to learn and become a fluent Scala developer. I know, because I have not bothered to look at those things until 4 years with the language. And when I did it’s not like I had an epyhpany, throwing out all the code I had written until then. In fact, I was monading all over the place, just had no idea how the things I was building and using were called.

In the end, these are just words.

Advice for learning Scala

If you want to learn Scala, you have to be ready to want to learn a new language.

I think much of the early criticism Scala got in the Java community was unfair and in part due to many Java developers never bothering to learn another language than Java, staying quite comfortably within the bounds of that language and not venturing outside of that well-defined space. From that perspective, any other language is going to look evil.

To break the “language bareer” this book is quite interesting.

Get a book

When I started learning Scala, I first had a look at what is possible in the Programming in Scala book, which turned out to be a bit too long and detailed, so I skipped a big part and read Programming Scala which turned out to work quite nicely for me.

There are many good Scala books out there so just pick one, such as Scala in Action or Programming Scala.

And if the thought of reading a whole book puts you off you can always download Scala by example PDF.

Get a project

Let’s be honest, learning a new programming language requires work. It is much easier to have a purpose beyond learning the language such as a small project. It’s like learning a new (spoken) language, really: you will learn it much faster if you travel to and live a bit in the language’s country.

I found that many people started to learn Scala after starting a project with the Play Framework. This should come to no surprise – web frameworks, and the Play Framework is no exception, act as a gateway drug to a new programming language. What’s interesting then is that Play has both APIs in Java and Scala, so you can slowly transition from one to the other (provided that you architect your application correctly especially in terms of database access).

Use the force REPL

Before even getting started with working in an IDE, exploring Scala through the REPL (Read Eval Print Loop) makes things very easy. It is possible to copy-paste code snippets from websites into the REPL and evaluate them in their entirety. The REPL also has code completion built-in (by pressing the tab key) which makes it possible to explore a type. I highly recommend installing Scala and playing with it!

You don’t have to start all functional

Don’t put too much pressure on you. The advice that is outlined in the third chapter of Reactive Web Applications is to start imperative and to slowly transition to immutable, declarative code.

Take your time to get familiar with the syntax and only then start working on the paradigm shift. You will have a much nicer experience if you are comfortible enough with the syntax to know how to fall back to an imperative programming style if the declarative one doesn’t suit you just yet.

Don’t try too many new things at once

It is tempting to start working with many of Scala’s features at once: implicit parameters, implicit conversions, traits, infix notations, functions, higher-order functions, lazy values as well as all the features related to type-level programming to name but a few. But you don’t have to and in fact you may be better of to take one step at the time.

That’s it for this recap. I hope I managed to convince you that Scala is not evil after all and wish you much fun with it!


Subscribe to updates

Liked this post? Subscribe to the mailing list to get (monthly) updates on what I'm doing here.

16 Comments 5 years of Scala and counting – debunking some myths about the language and its environment

  1. Ogg

    I think that’s a mistake in this phrase:

    ” little things that makes writing Scala code more enjoyable than Scala one”

    The 2nd “Scala” occurence should be “Java” i guess …

    Reply
  2. Pingback: Bookmarks for November 12th through November 13th | Chris's Digital Detritus

  3. Nirmalya Sengupta

    But for the that tool named *sbt*, I am in complete agreement with all other views expressed here. I still don’t see a very convincing reason to begin to use *sbt*, other than the fact that Lords of the Scala world use it and hence, I must use it.

    Reply
    1. David Perez

      I’ve used before Ant, Maven, Gradle, and SBT is easier to understand and very powerful and flexible.

      It allows completion, and has a very useful command line.

      I’m very happy to use SBT. It has become the main entry point for all administrative utilities of our servers and development needs.

      Reply
      1. Stephen Carman

        I think it’s easier to understand once you understand it. Getting past it’s initial barrier is difficult.

        Reply
  4. Pingback: Professional Development 11/09/15 – 11/29/15 | The Software Mentor

  5. Mike S.

    As silly as it may sound, posts like this that basically state, “I used it, and it’s good” are really useful.

    Most of the time people satisfied with something, or even happy with it but not gushing endlessly over how wonderful it is, keep quiet. Thanks to the internet and human nature, you’re often left with only the haters and the fanboys posting opinions.

    I don’t use Scala professionally, but I want to second the recommendation for Venkat Subramaniam’s “Programming Scala” book. His video presentations on Scala are also informative and entertaining.

    Reply
  6. Pingback: Chronicle Map ? ZeroNights — Episode 0070 « DevZen Podcast

  7. Josh Graham

    Thanks for the great write-up! I agree with the sentiments, having been meandering to-and-fro between Java, Scala, Clojure, and Ruby over the past 10 years.

    I’m not sure about the “context switching” correlation in the single-file point. Yes, context switching isn’t good, but I don’t agree that scrolling versus navigating is any less context switchy.

    Again, thanks for spending the time to put down these thoughts. They will help many!

    Cheers,
    Josh

    Reply
  8. Nicolas

    I completely share the point of view, especially on the NPE state. I use Scala for 3 years on my side-project, TeamMood, and I did not have any NullPointerException since then.
    And when you are familiar with the Option type, it’s hard to go back to Java, even with its Optional type, but that’s another debate 🙂

    Reply
  9. Steve

    If it’s NPEs that you are concerned about one would think you would gravitate more towards Kotlin, the compiler actually really helps you in that respect. Kotlin appears to take all the sore spots out of Scala and leaves a lot of the productivity goodies.

    Reply
      1. Steve

        True, Scala has gotten a good amount more adoption now that Apache Spark has come on the scenes. It is interesting to see where Kotlin goes (the plugin is enabled by default for IntelliJ users) plus current Java developers can dabble with Kotlin since the interoperability is pretty much seamless, whereas Scala you pretty much need to go all-in with the ability to pull in from Java code but not vice-versa. I am curious to see where enterprise adoption lands, I think Scala might be a bridge to far with interoperability issues, plus the seemingly continual backwards compatibility issues across Scala releases – Kotlin seems like a good compromise between not being cutting edge (and thus dropping features at a later date) and delivering a fair amount of productivity enhancements. So yes, we will have to see where it goes since Kotlin is only at 1.0.1 ;).

        For a comparison I was playing around with comparing the two last week here: https://goo.gl/np7fvI the syntax is pretty similar with the major differences being around the Optional, and Java interop as seen here: https://goo.gl/IZgrnX

        Reply
  10. ergomesh

    This is great. What discussion we should start next is how to get Java lead developers to understand the benefits of Scala and give them compelling arguments to allow their developers to use Scala.

    Here are the arguments that have been blocking me from implementing further Scala code.

    1: Its just a fad and will go away
    2: No one in the organization knows this and can’t support it if you get hit by a bus
    3: Compiles to slow ( covered already)
    4: play 1 to play 2 specific argument (total rewrite of libraries currently developed)

    I still am working heavily in Scala but have been forced to backtrack to Java on new applications. It is painful to retreat to Java given all the benefits of Scala described in this article.

    Thought?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *