If you’re in tune with the goings on in the realm of “Big Data,” you’ve no doubt heard of, if not worked with, a nifty distributed data processing platform by the name of Apache Spark.

If you’ve tinkered around with Spark at all, you’ll have at least heard of a programming language by the name of Scala. Spark is written in Scala, so it makes sense that Spark jobs would benefit from being written in Spark’s native tongue. In light of that, it’s likely that you have even written a few lines of Scala code in the process of tinkering with Spark. However, the Scala code that ends up as Spark Jobs tends to be rather simplistic, and that doesn’t do much to illuminate the immense potential of this ridiculously powerful object/functional programming gem for general purpose use outside of Spark jobs. That’s a shame because if you spend much time writing Java code, or managing Java projects, in your life outside of Spark, Scala just might change your life. No, I’m not at all kidding.

Now you’re asking yourself “what’s so great about Scala?”. Well, here we go.

Scala was developed by some disgruntled software engineers who enjoyed the benefits of Java and the JVM ecosystem but were intensely covetous of particular features from other programming languages like Haskell, Scheme, SML, and others. What ended up being produced is a general purpose language that is fully compatible with Java bytecode and runs on the standard JVM. Additionally, it includes first-class support and cool features from other languages: implicit, currying, type inference, immutability, lazy evaluation, pattern matching, algebraic data types, anonymous types, operator overloading, named parameters, multiple inheritances, and more. In the process of creating this incredible Swiss army ninja toolkit of a programming language, they also cleaned up much of what they didn’t like about Java.

Here’s a handful of things that you’ll come to appreciate almost immediately upon making the leap from Java to Scala.

It’s noticeably less verbose than Java.

I have yet to come across any snippet of Java code that doesn’t require at least 20% more typing than the equivalent code in Scala. Most of the time, you can write a Scala equal to your Java code that is closer to 50-70% less verbose. The big benefit is you end up with a far more concise, readable, codebase that produces the same result. You aren’t going to miss writing “public” on almost every method or coding up those getters and setters or explicitly casting types repeatedly either. It’s also worth mentioning that with every keystroke added to the codebase the potential for bugs increases — less code translates to fewer bugs and less code to sift through to find the ones that do creep in.

It encourages better, less error prone, more testable, code.

Through Scala’s type inference and compile time to check on pattern matching, you’ll be able to avoid explicitly casting/typing variables most of the time, which will all but eliminate ClassCastException headaches. By encouraging the widespread use of options instead of using null, Scala code is not subject to the dreaded NullPointerException that has plagued virtually every Java application ever written. By encouraging the exclusive use of immutable values, Scala code is almost always thread safe. Thanks to multiple inheritances via traits (think Java interfaces that allow concrete method implementations, not just method signatures), you can write more modular, reusable, and more atomically testable code. With some effort on your part, you can still screw all of this up, but Scala gives you the facilities and patterns to do things right without many excuses not to.

First-class functions!

Being a “functional” programming language, Scala has first-class support for functions — i.e., functions that exist outside the confines of a class (functions inside of classes are called methods). These functions are objects in their right and can be passed as arguments to other functions, as is standard practice in other modern languages like Ruby and Javascript. This opens the door to a lot of nice shorthand and tricks that you could only daydream about with Java.

First-class singletons!

Need a singleton in Scala? No DI frameworks or goofy init schemes required to make sure you truly only have one instance of your singleton classes. You just define it as an object instead of a class, and it gets instantiated lazily the first time referenced. These singleton objects get used quite a lot as “helper” companion objects for classes that contain what would be static methods in Java. Dependency Injection is also available, should your singleton need to set-up, database connections and whatnot, from config at init time.

It has a competent native build tool.

Scala has a Scala-based build tool, called SBT (Scala Build Tool), that uses Scala code as the configuration and has facilities to do all of the things you’ve come to expect from using other build tools like Maven and Gradle. If for whatever reason, you insist on sticking with Maven or Gradle, that works too.

It has a REPL!

Like Ruby, Python, and Groovy, Scala has a super handy interactive REPL (Read-Eval-Print-Loop) from which you can quickly test your code as you develop. If you’re coming to Scala from Spark, it’ll look pretty darn familiar — Spark’s interactive shell is just the Scala REPL with a bunch of Spark packages already imported and a few things instantiated for your convenience. It’s relatively trivial to build similar extensions to the REPL that are unique to whatever application you might be working on. Having this kind of interactive environment to play around with as you code, rather than having to run a full build-test iteration for every little code modification, is immensely productive.

Ok, so what are the downsides to using Scala vs. Java?

The only real argument I’ve heard for not using Scala over Java is the learning curve involved. Really? Sure, there are some complex concepts that a Scala developer is sure to be exposed to, much more so than with Java, but if that’s a major problem for you as a software engineer… I hate to break it to you, but you picked the wrong career path. From my perspective, making the jump is a no-brainer. There’s nothing you can do in Java that you can’t do in Scala. There is much you can do in Scala that you can’t do in Java, and you can get it done faster, better, with fewer bugs, using fewer keystrokes, in less time. That’s a win by knockout in my book.

There’s no time like the present to get your feet wet. Just about everything worth studying to get yourself up to speed with Scala is freely available on the web. Here are my recommendations:

Scala for the Impatient – http://www.horstmann.com/scala/index.html
Horstmann www.horstmann.com
Addison-Wesley 2016 “Currently the best compact introduction to Scala” —Martin Odersky “The book is a joy to read. Probably the most concise reference for …

Official Scala Tutorials – http://docs.scala-lang.org/tutorials/
Tutorials – Scala Documentation – docs.scala-lang.org
How do I find what some symbol means or does? How does yield work? What are Scala context bounds? What is the difference between view, stream, and iterator?

Share this:

Leave a Reply

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