Tuesday, 29 December 2009

Time to change - Moving from Java to Scala

The end of the year is a good time to look backwards and ask yourself a few questions. The question I'm asking myself every year is whether or not it's a good moment to change my core programming language. The last time I answered 'yes' was eight years ago.

That time I was working for a software company where the lion's share of software code was written in C/C++ and nobody wanted to hear about Java. There were two major reasons for that, first of all, people generally were against any changes, secondly almost everyone used to think that Java is slow and not appropriate programming language for developing large scale and distributed computer systems. Finally choosing Java for developing next-gen company products was a bull's eye shot.

Now, eight years later, I feel again that it's a good moment to go forward. This time I'm moving from Java to Scala. In reference to scala-lang.org:
Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive. Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application [1]
A good way to present the difference between two programming languages is to show the same code written in both of them. The code below presents following steps:
  1. Create list of three decimal prices [2].
  2. Convert prices to probabilities, probability = 1/price.
  3. Print all probabilities to the console.
Scala code:
//Convert prices to probabilities
//and display them to the console
object PriceToProbability extends Application {
val prices = List(2d,3d,4d)
val probabilities = prices.map(s => 1/s)
probabilities.foreach(println)
}

Java code:
//Convert prices to probabilities
//and display them to the console
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PriceToProbability {

public static void main(String[] args) {

List prices = Arrays.asList(2d,3d,4d);
List probabilities = new ArrayList();

for(Double price: prices) {
probabilities.add(1/price);
}

for(Double probability: probabilities) {
System.out.println(probability);
}
}
}

Nice, isn't it? Although it's a very simple example, it illustrates how neat and concise Scala could be. Is a Scala for you? it depends on the business domain that you want to apply it for. My domain is mostly represented by risk management and algorithmic trading and many aspects of Scala fit there perfectly. For example functional programming is very useful for implementing various calculations and mathematical algorithms.

For those, who want to learn more about Scala, I'd like to recommend very good book, actually one of the best books I read this year. Despite the fact it's a technical book I would compare it to reading Harry Potter. Simple language, easy to understand, short chapters and can't stop reading it. Here is the link: Programming in Scala by Martin Odersky

Reference