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:
- Create list of three decimal prices [2].
- Convert prices to probabilities, probability = 1/price.
- 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
I completely agree with you, and I'm conviced that Scala is (somehow) the future of the Java platform for the well known reasons : full object oriented, functionnal, types, etc.
ReplyDeleteStill, I am and we are stucked with plain Java in our organisation (software vendor) because of 2 major difficulties :
1) How to migrate our libraries with minimal cost ?
2) How to keep the power of our Swing IDE (Netbeans) without spaghetti-mixing the Swing code and the Scala code ?
For the moment, I did find any clean solution to solve these two major issues...
Do you have any idea about it?
Regards,
Nicolas
i'm goin to give it a shot.
ReplyDeleteNo need to migrate existing codebase to Scala. Just use them from Scala for your new projects.
ReplyDeleteI think that there is no simple answer how to migrate from Java to Scala smoothly and at the minimal cost, but technically it should be easier than from C/C++ to Java. The reason for that is because Scala runs on Java VM, so it can reuse Java Code. However reusability of Java libraries is only one of the migration issues. How to migrate Java Swing UI or GWT app? Maybe do not migrate at all, keep it as it is and use Scala where we really need it. Though, Java is still a very good programming language and I think that there is a space for both Java and Scala for a long time yet.
ReplyDeleteIt's one thing to pick up on the OOP side of Scala - program with it as a much improved Java. It's another thing to really learn the functional programming side of Scala and concurrency programming with Actors (though I find that easier in many respects than threads and locks).
ReplyDeleteGoing from C++ to Java was a breath of fresh air. Everything was easier and more productive. Going from Java to Scala - well, depends. To truly embrace Scala would be to learn it's totality - its an expansive language with a lot of terse and mystifying syntax to those coming over from Java.
With the migration from C++ to Java - there was never anything mystifying or difficult to groc.
So I'm not sure Java-to-Scala is the same smooth path migration as was C++-to-Java.
I got so put off learning the functional side of Scala that I took a hiatus and went off to code a project in Google's Go language, which is a very simple, concise C-inspired language that has goroutines and channels. Google has an example program that spins up a 100,000 goroutines within a single process and harvest a value from each. Rather impressive.
ReplyDeleteHowever, the Scala Actor library has 'react' as an alternative to 'receive'. When using 'react', many actors may be multiplexed to a few threads (or a single thread) - ala Go goroutines.
Also, Scala has more facilities for enforcing immutable objects. Go is rather limited and so one relies more on convention and being aware of how data is being used between goroutines over channels. Go, like C, offers plenty of rope to hang oneself as pointers to data can be passed over channels.
I also have recently made this switch and don't plan to look back! The comments above are right - don't port "legacy" code, existing libraries can be used in the new Scala code. I have had good success with this, even implementing objects in Scala which implement an interface and get passed to the legacy code. Also of note if you're doing web pages - the Lift framework is outstanding. It does a lot of the 'heavy lifting' (Ajax, Comet, etc.) and makes it quite easy and especially to connect to a Java backend. Good luck with it!
ReplyDeleteobject PriceToProbability extends Application {
ReplyDeleteval prices = List(2d,3d,4d)
val probabilities = prices.map(s => 1/s)
probabilities.foreach(println)
}
Who does this kind of coding in any projects? Either we get values from Database/XML/File System and we add elements to Map or List in a loop. Which Scala wont help much.
The above code is good for any mock ups or prototypes where we hard code values...Apart from this not much gain...
And comming back to probabilities.foreach(println) who uses this kind of code? even System.out.println(map) can do that job if required.
Why do you need to covert List to Map and increase memory usage? Just loop print the list.
ReplyDeleteHello Sreenath V,
ReplyDeleteAlthough it's not a production code, it presents some Scala features not available in Java:
- First-class function (s=> 1/s)
- Immutable lists, prices.map() returns new list with probabilities.
- Different style of loops (foreach), println is a simple form of function literal.
Scala is not a pure functional language and imperative programming can be used too.
Your example makes massive use of closures. Is it really fair ?
ReplyDeleteIf the game is to count code lines, wait for JDK 7 (with closures) and use project lombok, then think again.
I'm not saying that Scala isn't great, but is it enough to switch ?
Interesting article, and I must admit Scala looks very interesting, but I also have a bit of problem seeing the huge advantage with using Scala over Java. Sure its less code, but unless you work in a start up company the cost of changing to a new programming language can be quite huge. I am just not sure that Scala has enough selling point to make companies choose it rather than just wait for JDK 7.
ReplyDeleteHi Olivier,
ReplyDeleteI still think that Java is a very good and productive language and I'm not going to switch from Java to Scala completely.
I want to switch to Scala in areas where I can see some benefits. By that I mean faster, cheaper and better quality of software development and maintenance. The example for that could be the probability engine that calculates probabilities and certainties of events to happen. For example, probability of stock price moving up.
With respect to Java 7 I’m a bit tired reading about closure proposals and waiting for it. Also I think that the backward compatibility is the killer for Java that makes some language improvements difficult to introduce. Sometimes a new language is the only one solution.
Anyway, the time will show, what direction the market will follow. It’s not just about which language is better, there are big companies behind Java, thousands of Java libraries, tools and enormous amount of lines of code to maintain and eventually Scala can be marginalized by the market.
I've read about Scala and loved it. The articles by Bill Venners are great (I learned design reading his articles back in the JavaWorld days). But again, closures will bring Java back in the game. The realistic approach Sun recently took looks great. You should definitely read the lamba-dev mailing liste at http://mail.openjdk.java.net/mailman/listinfo/lambda-dev
ReplyDeleteScala misses a killer feature. You switched to Java because of the GC and the pointer arithmetics removal. Sure "3 to 5" meaning "3.to(5)" is very clever, but it's not enough.
Great post! I personally think the "case" to move from C++ to Java about 9 years ago to me :) was stronger one than moving from Java to Scala, simply because the 'need' and benfits is not as great compared to C++. Also how many real world projects will spend money to change to Java, or even new projects that are yet to be implement will businesses be willing to take the risk because the talent pool of Scala is still pretty small? Scala is great I am sure but adoption in the mainstream is going to be ALOT slower than Java over C++...
ReplyDeleteHello,
ReplyDeleteI completly agree on the approach but did you consider groovy in the list of the potential choices for the "switch" ? and what were the reasons to drop it against scal ? Because Groovy shared of the goals exposed you were looking for, is very well knowned and supported (ide, frameworks...) along with excellent performance.
Regards
Hello Guizmo,
ReplyDeleteScala is a statically typed language and it’s a significant difference for me between Scala and Groovy. I like Groovy as a scripting language, e.g. on testing side using EasyB and Behaviour Driven Development, however as a programming language to develop scalable, reliable and high-performance applications I prefer Java and Scala. Scala is something I was looking for years: object-oriented, functional and imperative, statically typed, flexile and concise as scripting languages such as Ruby/Groovy.
Hi Daniel,
ReplyDeleteThanks for the answer. When you talk about "scalable, reliable and high-performance applications", i would like to take some kind of a real world example if you allow me. Let's take people who are ok to use existing java code but does not want to code in java by themselves. Let's say these people want to play around doing a bookmaker or a betting exchange web site. There would be things like Terracotta as useful middleware resources but regarding the language, would you advice them Scala or Groovy in that context ?
Alex
Both betting exchange and sportsbook are complex systems based on a variety of technologies and programming languages and simple answer to your question doesn't exist. Generally speaking a wide range of programming languages are in place in different parts of a system.
ReplyDeleteI have been fiddling with scala for nearly an year now. Needless to say, scala is better than java. Some people seem to suggest that with 'closures' in java 7 - all programming complexities would be solved for ever. Scala has closures and much more(higher order functions, pattern matching, traits, much expressive type system...). It combines OOP with FP. If some one should compare java and scala - it should be in the OOP aspects. As it's author - Martin Oderskey says - "Scala is a deep language". Scala support for actors, xml etc are provided as libraries - yet when using these features nobody feels that they are using some library. Scala is a much better OO language than java - scala get's rid arcane concepts like 'static' and every operation is a method call.
ReplyDeleteWhen java came, it was touted as the true component language and it was said that software would be composable from components - yet we see none of that even after 14/15 years. Software is still a craft - starting from scratch - Not anywhere close to becoming and industry. With scala traits - maybe we would be moving towards that direction. It does not mean that - I hate java or something - I started my career as a java developer. But human species always strives to make things better and same with scala. Neal Gafter said this very correctly: "Will Scala be the next great language? Only time will tell. Martin Odersky and his team certainly have the taste and skill for the job. One thing is sure: Scala sets a new standard against which future languages will be measured."