We develop custom software, sometimes from scratch, sometimes it will be included in existing ecosystems. In both cases, the customer oftentimes wants to fix which technology we should use. We take the customer’s requests into consideration a lot, but try not to give him what he wants, but the best overall solution given the parameters of the project. Oftentimes we recommend Scala as a programming language, despite the fact that customers want Plain Old Java.
In this post, I want to answer the question why we think that Scala is the language to choose (for many of our projects). The intended audience for this post are technical managers who are involved in the decision about what technology to use. Therefore I won’t discuss Scala’s cool features but focus instead on the business-level questions of how using Scala rather than Java might affect the success of a project, a team, or the company.
Choosing Scala has the potential to shorten the time to market and to reduce development/maintenance costs. This is possible because it helps to improve the correctness and maintainability of a software and it enables developers to be more productive. Scala is used widely by well known companies and there are popular proponents of the Scala language. Going with Scala is not as convenient as choosing Java, but it provides the opportunity to achieve a competitive advantage.
Scala Elevator Pitch
At first I want to give a quick introduction to Scala and highlight some of its main characteristics.
- Scala runs on the JVM and benefits from this proven and stable platform in operations
- Scala is interoperable with Java, so the massive ecosystem of Java libraries can be used
- Scala is a purely object-oriented programming (OOP) language
- Scala is also a functional programming (FP) language and encourages immutability. Programs written in a functional style are easily scalable (and still reliable) with a growing number of CPU cores
- Scala lets you write concise, readable code. The time a programmer spends reading code compared to the time spent writing code is said to be at least a 10:1 ratio, so writing code that’s concise and readable is a big deal.
- Scala uses the actor model for concurrency to keep multicore processors humming (the actor model was used/popularized by Erlang to support distributed, fault-tolerant, soft-real-time, non-stop applications like routers and telco systems)
- A Java developer can be productive using Scala from day 1, but the language is deep, so as you go along you’ll keep learning. Scala will change the way you think about programming (and that’s a good thing).
- Created by Martin Odersky (the father of Javac), first public release 2003
- Backed by Typesafe (develops Play! Framework and Akka, has received $14M in Series B funding in Q3/2012)
- Used to power the busiest websites in the world, including Twitter, Netflix, Tumblr, LinkedIn, Foursquare, and many more.
Why should we choose Scala?
I want to highlight some characteristics of software projects that are supported by Scala very well and that support the long-term success of a product:
- Software correctness and reliability
- Developer productivity
- Maintainability of the software
In the following I’m going to address each of these points in greater detail.
Correctness - write software with fewer defects
An important software quality characteristic is correctness, which is well supported by some of the characteristics of Scala.
- Functional programming (transform an input to an output) with immutable data structures eliminates/reduces side effects, which are often the reason for defects. With FP it’s easier to reason about program behaviour, becoming even more important in a multi-threaded environment: immutable objects are thread-safe by their nature (which has the additional positive effect that it allows lockless programming and therefore improves scalability).
- The declarative style (what shall be done) in contrast to Java’s imperative style (how shall it be done) leads to a higher information/noise ratio. The code is more concise and compact, there’s less bloat and infrastructure code. Fewer lines of code contain fewer defects.
- Concurrency is easier to get right with the actor model provided by Scala (and Akka). In Java concurrency is still a pain and there are very many Java developers who don’t know the important basics well enough (yes, the Java Memory Model is a fairly complex thing). In multi-threaded applications this can also lead to bugs that appear only under load and which are therefore hard/expensive to fix.
Value: Correct software saves money: there are no unsatisfied customers, less bug fixing is needed, there’s more time/money to develop new features.
Productivity - go faster, keep code simple
Yes, in my opinion Scala makes a Java developer more productive. Besides several language features like case classes an important reason for this are the higher abstractions that functional programming provides - remember how moving from assembler to higher level languages led to higher productivity. Besides my personal experience/opinion here are two resources on this topic:
- The paper of Gilles Dubochet, in which he reports that he found that it is, on average, 30% faster to comprehend algorithms that use for-comprehensions and maps, as in Scala, rather than those with the iterative while-loops of Java.
- The survey created by Graham Lea regarding the perceived productivity gains of Scala developers , which shows that Java programmers who learn Scala generally feel more productive pretty much straight away.
I also want to point out that - in my opinion - it’s very important regarding productivity that a team agrees on a certain feature set that can be used, because it’s also possible to write complex code that’s hard to read (which is possible in Java as well, but in Scala there’s more room for this). Also code reviews and/or pair programming must be used to make sure that the agreed coding features/style/standards are used.
Value: Higher productivity directly reduces costs, improves time to market and has a positive impact on the motivation of programmers.
Maintainability - take care of the important stuff
Maintainability becomes more and more important later in the life cycle of a software, after the first production deployment. How can Scala help to improve maintainability?
- The functional/declarative programming style is more readable because it’s said what is done in contrast to how it’s done.
- Immutability and functional programming make it easier to reason about program behaviour, because there should be no side effects
- Scala needs significantly fewer lines of code, is more compact and has a higher information/noise ratio. So when reading Scala code developers have to remember/translate less and therefore it’s easier for them to read the code.
- Of course the code should only use concepts known by the reader (as already stated above).
Value: Scala allows to reduce maintenance costs.
Adoption of Scala
Because it’s often good to know others who previously made the decision under debate, I want to give an excerpt of companies that are already using Scala (source: Wikipedia):
- Twitter, LinkedIn, Foursquare, Yahoo!, Walmart, The Guardian, eBay Inc., Novell, UBS, SAP AG, Siemens AG
Typesafe also provides a list of case studies on their website where you can see how companies are using Scala, Play! and Akka to drive their businesses.
Perhaps it’s also helpful to know that Scala is promoted by Thoughtworks in its Technology Radar as a language to adopt.
Finally, for the Spring Framework fans of you: did you know that Rod Johnson, the man behind Spring became of a convert of the Scala language in 2012, and liked it so much that he joined the Typesafe board in October? Johnson believes that in 2018, Scala will have “found its niche” as the leading enterprise language, for demanding applications that require scalability. He doesn’t believe Scala will be as big as Java was in its heyday, nor will any language ever come close to that.
Cons & Critiques
There is common critique and disadvantages mentioned that I want to address in the following.
Yes, there is a learning curve. Especially functional programming concepts are not (well) known to many Java developers. It can take some time to become familiar with such a new way of thinking, even if the syntactical constructs are not hard to learn. In other words: it’s possible to be productive very early, but there’s still much that can be learnt (productivity vs. mastery).
A popular critic of Scala is Stephen Colebourne, the creator of Joda Time. I met him at JAX 2013 in Mainz/Germany and talked to him about his doubts regarding Scala. His main point was that it’s possible to produce unmaintainable code with Scala. Basically, I agree with him, and I also don’t like method names made up of symbols like “<++” that you can’t google and where you don’t immediately know how to call them. This is why in my opinion it’s very important for a team to agree on features and a style to use and to be very disciplined regarding that (Scala has built-in support for this with feature imports). Code reviews and/or pair programming must be used to make sure that coding style guidelines are met.
IDE support is suboptimal
It’s true that IDE support for Scala is not as good as for Java. Though, it’s good enough to allow for better productivity compared to using Java, in my experience, and it’s continually improved. When I first used Scala in a project for a client back in 2009, IDE support was really, really bad (crashing IDE, false negatives regarding compilation errors and so on). Even at that time, when I had to go back to Java it was painful, just because Java is missing many of the convenient features of Scala that make programming much easier (and code generation of IDEs cannot completely compensate this obstacle). Since 2009 IDE support has dramatically improved, so it’s stable and working, just not as rich as for Java.
Yes, compiling Scala code takes more time. Because Scala has a higher information density (is much more concise), one can’t compare lines/second compile times. There are ways how to deal with longer compile times: sbt (simple build tool) helps with its incremental compilation, so that only changed code is recompiled. Another advice is to split the application into several modules which can be built/compiled independently. Also it’s important to use some of the Scala features impacting compilation speed consciously.
In my experience the longer compile times are a bit annoying to developers, but do not have a significant impact on productivity.
There are much fewer Scala developers than Java developers
That’s true, but a Java developer is productive in Scala soon, so you can hire all those (good) Java developers. For example, in our most recent Scala project (also using Play 2 and Akka) we started with 2 Scala developers and 3 Java developers (2 experienced, 1 junior). The former Java-only developers worked on features / user stories from day 1 and were on speed after ~ 1 week (with increasing producitivity).
Providing an environment in which developers can learn something new should also make for a more positive environment for those developers, so they will be happier with their jobs and less likely to leave the company. Likewise, a shop that uses a new and advanced language such as Scala is more likely to attract good developers (which Paul Graham has named as “The Python Paradox”).
You won’t want to write Java again
True, I don’t have anything to hold against this ;-)
Before closing this post I want to quote some well known people.
No other language on the JVM seems as capable of being a ‘replacement for Java’ as Scala, and the momentum behind Scala is now unquestionable. Charles Nutter, co-creator of JRuby
I can honestly say if someone had shown me the Programming Scala book by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy. James Strachan, creator of Groovy
If I were to pick a language today other than Java, it would be Scala. James Gosling, "father" of Java
Scala allows to improve the correctness and maintainability of a software and improves developer productivity. This of course has the desired effect of faster time to market and cost reduction. Scala is used widely by well known companies and there are popular proponents of the Scala language. It’s possible to read about critiques regarding Scala, some of them are true and some of them are not - finally they don’t have such an impact that they significantly affect the advantages of Scala. Going with Scala is not as convenient as choosing Java, but it provides the opportunity to achieve a competitive advantage.