Sunday, November 18, 2012

Should You Learn Scala?

It depends on what you want to accomplish*, but the answer is probably yes.

By way of background, Scala is a programming language originally developed at the Swiss university EPFL in an attempt to apply recent innovations in programming languages research to a language that can gain mainstream traction.  Researchers keep coming up with clever ways to it easier for programmers to write correct code and to write less code, but much of the time only academic programmers benefit because these features are implemented in boutique languages (for instance, OCaml and Haskell) that are not in widespread use in industry or open source projects.

On its own, without considering features such a libraries and interoperability, Scala is a nice language.  It is "multi-paradigm," supporting object-oriented programming and functional programming idioms.  Scala allows you to define types associated with both data attributes (with classes behaving like C++/Java classes) and behavior attributes (with traits corresponding to Java interfaces).  In the way of functional programming idioms, Scala functions are first-class (which means you can pass them around as values) and Scala also supports anonymous functions and currying (partial application of multi-argument functions).  This combination of features make it possible to write Scala programs that are quite concise and elegant**.

Scala's libraries and interoperability make it a force to be reckoned with.  Scala runs on the Java Virtual Machine (JVM) and can interoperate nicely with Java code.  This means that you can use Java libraries directly from Scala code.  It is also possible to call Scala code from Java (see here).  There is a nice post about how you can take advantage of Scala parser combinators, which use higher-order functions to help you write concise parsers, to write parsers for systems otherwise written in Java.  Not only can you do all of the things in Scala, you can easily write part of your program in Scala and the rest in Java.  (This is huge for mainstream adoption potential because so much of the world's code is written in Java and so many developers are taught in school!)

Learning Scala will make you more marketable.  Many companies are using Scala these days, including LinkedIn, FourSquare, Twitter, and Quora.  The founders of Quora spent a couple of months building a Scala web framework in order to use Scala.  Martin Odersky, the man behind Scala, is now behind the startup Typesafe for supporting Scala development.  There are some nice Scala web frameworks out there, including Lift and Play.  Scala is also gaining traction in the research community.  Many more people cared about our research on the Jeeves programming language for automatically enforcing privacy policies once we implemented it as an embedded domain-specific language in Scala.

Scala is also relatively easy to learn, especially compared to other strongly statically typed languages (OCaml; Haskell).  I have previously said that Python is the most marketable language for beginners, but I am beginning to change my mind.  Like Python, Scala has clean syntax, nice libraries, good online documentation, and lots of people in industry using it.  Unlike Python, Scala also has a static type system that can prevent you from doing bad things (whereas in Python the bad things just happen when you run the program).

If you are teaching a course using Java, you may want to consider switching to Scala.  I have recently had positive experiences teaching Scala to MIT undergraduates during recitation in 6.005, the software construction course taught primarily in Java.  During three recitations I introduced how to use functional idioms, how to build an evaluator for a simple language by using data types and pattern matching, and how to build an incredibly concise parser for the tiny language using parser combinators.  You may find my notes here, along with the accompanying code examples.

If you are considering the merits of Scala over other languages, you may also be interested in reading my Quora responses to the questions "What makes a good programming language?" and What are the advantages of Scala over C++ and Haskell?.

* If you are building scientific applications, a language with more math support (Matlab; Mathematica; R) may be superior.  If you just want to do a weekend project writing a Craigslist scraper, Python may be better because it is easier to learn and has great web scraping libraries.
** Okay, Scala programs are not as beautiful as OCaml or Haskell (I have another post on ML vs. Haskell) but it's so much more practical in many ways.

13 comments:

Clement Levallois said...

2 questions:
- Would your recommend a book or textbook for somebody coming from Java?
- and a book / or any source that would focus on the advantages of Scala for server side programming (I use JSF, why should I be interested in Scala?)
Thx a lot!

Jean said...

Here are some useful links for learning Scala from Java:
- http://www.ibm.com/developerworks/java/library/j-scala01228/index.html
- https://blogs.oracle.com/sundararajan/entry/scala_for_java_programmers

You may be interested in learning about the Play MVC framework:
http://www.playframework.org/
There is a book with an early access edition about it:
http://www.manning.com/hilton/

Clement Levallois said...

thx!

christi parks said...

Hello, sir i would like to ask that what is the scope of java training, what all topics should be covered and it is kinda bothering me … and has anyone studies from this course http://www.wiziq.com/course/1779-core-and-advance-java-concepts of core and advance java online ?? or tell me any other guidance...
would really appreciate help… and Also i would like to thank for all the information you are providing on java concepts.

Mark Lewis said...

Great post. I love that you mention Scala as an option for introductory programming. I have been using it as the exclusive language of instruction for CS1 and CS2 at Trinity University since Fall of 2010 and I love how it works. Since there wasn't much material out there for that purpose I wound up writing a lot myself and it turned into this, http://www.amazon.com/Introduction-Programming-Chapman-Textbooks-Computing/dp/1439896666.

Nghia Huynh Trong said...

I do know that Scala is based on Java. So I would like to ask you whether I need to learn Java first, if not is there any diffculties?

Jean said...

You should not have to learn Scala first to learn Java. It may be useful to know some Java if you want to access Java libraries.

Randy said...

Hi Jean, really great article. I was just wondering what your thoughts are on how well scala stacks/compares against c#? Do you think scala will actually be a "successor" to java as some are claiming? And if it is a successor to java, which language do you think has a brighter future, c# or scala? also, which one do you think is more object oriented or has a better OOP model, c# or scala? thanks and sorry for all of these questions.

heena goyal said...

Hi,
Very informative information provided.
Is online training reliable?
I have never taken any but these days if you want to study from the best teachers online learning is best. So i have decided to do my online java training course from http://www.wiziq.com/course/12145-the-6-week-complete-java-primer-with-training-certificate as one of my friend recommended me this site. I don't know how is it going to work for me so if anybody have studied from this site please let me know .

sudheer said...

The information which you have provided is very good and easily understood.
It is very useful who is looking for java Online Training.

Anonymous said...

Indeed Java dev should now look to Scala. This article has covered some difference between Scala and Java and also shared few resources to learn Scala.

prabodh said...

I am from C++ background and do not know Java. DO you think I should go for Scala or learn Core Java first and then think of Scala?

Bala Murugan said...

This really useful this blogs i like it,CompTIA CASP Training VA
|CEH Training Fairfax VA