Does Scala’s Enterprise Penetration Threaten Java?

Created in 2001 and running on the JVM, Scala has started gaining traction in the Enterprise space because its use of functional and object oriented programming gives it an advantage over Java.

Scala LogoIt’s quite rare to see software disruption happening. It’s said that you don’t start recognizing a new Hollywood film star until you’ve seen them six times. The same may be true for new programming languages. A mention here, an article there and up it pops on your “Should I be noticing this?” radar.

Until a few years ago I’d always ignored functional programming, a style that’s subtly different from imperative languages such as C, C++ and Java.

Functional programming is a programming model that eliminates side effects and tracking states through the use of functions to do computations. In imperative languages, state is tracked and functions may return different results. Most programmers experience a touch of functional programming when they use SQL.

Then I learned LINQ (Language Integrated Query), a component of C# that’s very similar to SQL. It lets you process a list of objects in memory just like a table of rows in SQL. You can perform selection on a subset of objects and aggregate operations (summing, counting, etc.) on those rows’ attributes in an elegant and robust fashion. LINQ was my introduction to functional programming.

A few years back, I began hearing about Scala. Colleagues started using it, books appeared on desks, and I decided to get involved. According to the Scala site, from 2011 to 2012, its use in the Enterprise grew tenfold.

SCALA = FP + OO

That’s not an equation. It means that Scala includes both object-oriented and functional programming. Object-oriented programming is a form of imperative programming that models the world as objects. Well-known examples include C++ and Java.

Scala is short for Scalable Language, and some consider it a possible successor to Java. It’s definitely gaining traction — this is the disruption I mentioned earlier.

Developed in 2001 at EPFL (one of two Swiss Federal Institutes of Technology), Scala is an open-source language created  by Professor Martin Odersky, a German computer scientist who was the lead designer on javac, the main Java compiler (on Java versions 1.1 to 1.4) provided by Oracle. He has since founded Typesafe, a company to promote, develop and provide commercial support and training for Scala.

Why Scala?

Scala is one of several functional languages built on the Java Virtual Machine. Unlike C, C++ and many other languages, compilers for Java and Scala don’t generate machine code that runs directly on a processor. Instead, the output is bytecode, a portable intermediate language that’s converted to machine code by the JVM. So once a JVM is developed for a processor, it can run any bytecode.

In the .NET world the same is true with intermediate language code generated by C# and other .NET compilers that run on the Common Language Runtime, similar to the JVM. There’s even a version of Scala for .NET.

So what makes Scala say more attractive than the likes of Haskell, OCaml and Standard ML? Pure functional programming languages such as Haskell have specialist uses, but  are less general purpose than the imperative languages like C, C++ and Java.

Scala, Erlang, Clojure and Lisp combine both functional and imperative, but are dynamically typed and so not as efficient. Dynamic typing means that incorrect types can generate errors at runtime. Statically typed like Scala, C++ and Java don’t have this issue. The compiler detects type errors during compilation and may be able to generate more efficient code.

Compared to Java, Scala code can be a lot shorter and elegant, especially using functional programming constructs. It uses the method of concurrency that’s based on Erlang’s actor model, where messages are sent between processes. This means you don’t get tied down with locks and threads. There’s a separate open source library Akka that extends this to allow actors on remote hosts.

Perhaps the biggest attractions of Scala is the interopability with Java and its running on the JVM. This makes for a low bar to adopting Scala in a Java enterprise. Java developers can pick up the object-oriented programming quickly, then start learning the functional programming.

Users of Scala

In 2012, Coursera hosted a Scala course by Martin Odersky, launching with 50,000 students. Some 20 percent of them completed the course, double the normal percentage of online course completion. The other way to learn it is to buy a book and start working your way through it.

There are three different free IDEs available — though JetrBrain’s Intellij Idea is available in a commercial version as well. Currently Scala is at version 2.10, which was released in January 2013. Netbeans is behind by a couple of releases. There’s also an Eclipse-based IDE. As with all of its IDEs, the Eclipse version is a bit of a memory hog and best run on a 64-bit PC. For Intelliji Idea and Netbeans, Scala is installed as a plugin and is just a little bit fiddly.

You install Scala from the main Scala site. The screenshot below shows it running on the Eclipse IDE.

Screenshot of Scala Eclipse IDE

Example of Scala

This example taken from the Scala website prints out all prime numbers less than 100. The def defines a recursive function. Note the variable: type declaration which is the opposite way round to C, C++, Java etc.

/** Print prime numbers less than 100, very inefficiently */
object primes extends Application {
def isPrime(n: Int) : Boolean = (2 until n) forall (n % _ != 0)
for (i <- 1 to 100 if (isPrime(i)) println(i)
}

Make no mistake, Scala is different from Java in many ways, and there’s something of a learning curve. But functional programming isn’t going away, and this is a language to watch.

Related Links

Comments

  1. BY ai says:

    Unfortunately claims like: “According to the Scala site, from 2011 to 2012, its use in the Enterprise grew tenfold.” make no sense without context, as all it could mean that they then from 10 to 100 installations, I would love to see the numbers compared to java.

    Now since this is published on dice, I did very simple experiment that sheds some light on the true picture:

    - search for scala returned 216 job postings
    - search for java jobs returned 17392 job postings

    I think it is still really long way to go for scala, does threaten Java? Not at this scale.

    Although the proliferation of other functional languages will help the cause of scala and it may succeed where others will fail, too many times I saw that the initial benefits of dynamic typing turn into huge pains after project reach critical mass (i.e. when the increased amount of unit-testing that dynamically typing languages starts to exceed the benefits of dynamic typing vs statically typed where a lot of validations are free)

Post a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>