Why would a Java coder invest their time in learning and using the Scala programming language? It’s a good question, as a functional language like Scala requires dedication to learning new ways of thinking about how you solve problems and build great software. The following key observations have really stuck with me throughout my journey within the Scala ecosystem.
Expressive and Powerful
Scala is a particularly powerful language for developers who are looking for a sharper tool. If we consider software to be executable (or automated) knowledge, then you’ll find yourself automating a lot of knowledge with comparatively little code in Scala. You’ll soon find yourself writing concise, clear code that doesn’t require the ceremony and boilerplate of more verbose languages — all without sacrificing on readability or maintainability. Indeed, both of these aspects of your code quality are likely to be greatly improved.
Core language features like type inference make programming in Scala feel lightweight, as though you’re programming in a dynamic language like Ruby. Scala’s type system doesn’t get in your way by asking for redundant type boilerplate the way a language like Java can. So drop the boilerplate without giving up the benefits of static type checking and you’ll have less code clutter while the compiler is still finding bugs in your code, rather than leaving that unpleasant duty up to your users at runtime.
Scala’s so-called functional combinators let you manipulate collections of data without having to write manual for loops or maintain intermediate collections for accumulating results. If you’re familiar with LINQ in C# or Java 8’s Streams, then you can think of this as a more powerful version of those concepts. Using functional combinators like map and filter often allows you to express things as simple one-liners, getting rid of the imperative steps required by lower level for loops.
By using Scala’s Option, Try and Either types you’ll also wave goodbye to null pointer exceptions. All whilst staying DRY and flatten out “arrow code”, making things safer and more readable. All the functional combinators that you use on collections work on these types too!
Developing “the Scala way” (i.e., idiomatic Scala) means you avoid relying on mutable state (variables). This means no more permutations of variables racking your brain as runtime complexity becomes harder and harder to reason about. By emphasising immutability your code really does become easier to understand, and that ultimately means less bugs. Combining immutability with futures and Akka actors makes your code easier to parallelise and scale out as you go “web scale”.
Some of the features I’ve mentioned here have begun to appear in Java (in particular via Java 8’s Streams and its Optional class) but their usefulness there is limited by the lack of core language features such as pattern matching. To take full advantage of these powerful features a more sophisticated underlying language like Scala really does help.
These and other features enable you to write code at a higher level of abstraction than an imperative language like Java, letting you focus on what’s really important: delivering value. These features might seem daunting if you’re not already familiar with them, but don’t be too intimidated. You don’t have to become a master all at once.
Scala is both a functional and an object oriented language. If you’re not familiar with functional programming concepts, you can quickly get started using familiar OO idioms and techniques. Later, you and your team can gradually adopt a more functional style by taking advantage of features such as immutability, pattern matching, higher-order functions and functional combinators.
Scala runs on the JVM, allowing you to mix Scala and Java classes — a huge benefit when you consider that there’s pretty much a Java library for anything. Has your team already built up a library of reusable components in Java? You don’t need to throw away or rewrite your existing Java investment in order to start benefiting from Scala. The same goes for popular open source Java libraries. Do you use Apache Commons? Are you fond of Google Guice or Mockito? Keep on using them!
Scala is in ThoughtWorks’ Adopt ring, which means they think you “should be using it now” and that “there is no doubt that it’s proven and mature for use.”