As a developer, Java has been my go-to language for quite some time now. TI-Basic on my graphing calculator aside, it’s the first real programming language I learned. I used it all through college, and I’ve used it to create all sorts of cool things for a plethora of clients. As an object oriented language, it gets the job done in a variety of situations. For a long period of time, I was content with Java and always using an object-oriented mindset to solve problems. With classes, inheritance and a book on design patterns, the world was mine for the taking–or so I thought.
Then I found functional programming. Like a ten year old that’s magically won a full ride college scholarship simply by playing the claw game at the local mall, I didn’t have a full appreciation for what I had just been given. I started toying around with pure functional programming using Clojure and solving problems on projecteuler.net (more on that in a later blog post). More recently however, I was given the opportunity to use Scala on a project here at the Nerdery, and it’s made everything so much easier.
Let’s look at several examples of exactly how Scala surpasses Java. First off, it’s important to note that Scala, like Java, runs on the Java Virtual Machine (JVM). This means that Scala can run anywhere Java can run and can feed off of the numerous benefits of the JVM. Write once, run anywhere? Done. Garbage collection without even knowing what garbage collection is? Why not? Use Java libraries that developers everywhere have come to depend on from day-to-day? Not a problem; Scala can call Java code and vice-versa. The list of benefits goes on, but let’s look at the ways in which Scala surpasses Java.
Let’s assume, for the sake of consistency and simplicity that we are writing an online store of some kind. Naturally, we need products to sell. Each product should have an id, a name, a price, and a creation date. We need to be able to compare products to each other for equality and we need to be able to print/log products for the sake of debugging what’s in each property. In Java, your product model class might look something like this: Read more