Scala: Productivity gains but complexity remains
- 08 September, 2014 10:23
Functional programming language Scala, which compiles to bytecode that can run on a Java virtual machine, has been praised for offering productivity gains over Java. However, the syntactical differences and error messages can sometimes be quite complex for developers to wrap their heads around.
One of the biggest benefits for developers looking to make the transition from Java to Scala is that the latter is substantially terser, said Tom Adams, SVP of Polyglot Engineering at Cogent. Adams is speaking today at the YOW! Connected conference in Melbourne about using Scala to develop Android apps.
“Scala is a lot more succinct than Java, so the code you write is shorter,” Adams said.
"You’ve got type inference, which means you don’t have to specify the types… and you don’t have to repeat yourself a lot.”
Boilerplate code can be greatly reduced in Scala, Adams said. For example, getter methods to retrieve the value of a private attribute must be manually written in Java. As there can be hundreds of attributes when developing large applications, this can be substantially increase the number of lines of source code.
“So in Java you might write a class — let’s call it Person. If you want to construct that Person with a name, then you’ve got to write a constructor. And then if you want to expose that name, you have got to write a getter. And then say you want to use toString, then you have to write that toString [method],” Adams said.
“You’ve also got to do things like, say you want to stick something in an array or some sort of collection class, then you have got to implement a bunch of methods like Equals and HashCode, and you have got to implement them correctly. It can be quite hard to do, but there is library support for that.
“In Scala if you use a case class, all that stuff is done for you. So the compiler will generate the getter and setter, the toString, the Equals and HashCode, and the constructors.”
Scala is also less verbose when defining variables, Adams said.
“In Java, if you were going to deploy say an array of or collection of people, then you have got to write ‘collection’ and put angle brackets for ‘person’, then you type ‘people=new’ or an array list, then in brackets you put 'person'. So you repeat the [data] type two or three times just in the definition of the variable you are going to use.
“In Scala, you would only have to type [the data type] once." And sometimes it isn't necessary to specify type because of Scala's support for local type inference.
Higher-kinded types in Scala also eliminate repetition when coding, as it abstracts away commonly performed idioms or patterns when writing code, Adams said.
The complexity of the Scala language, however, can make it hard for developers to wrap their heads around it, and they shouldn’t underestimate the initial learning curve, Adams said.
“The language itself is fairly complex, as there’s several ways to achieve similar things. There’s a character used that is the underscore and it has about six different meanings,” he said.
“You have to know what context you are in in order to know what it means. Does it mean an unbound variable? Does it mean a wild card import? There’s a whole bunch of different ways to interpret that underscore.”
Compiler bugs and cryptic error messages have also been an issue with Scala, Adams said.
“The compiler has had a few bugs and its error messages can be very hard to interpret, so you are not always sure why something failed.”
One of the main contributors to the Scala compiler, Paul Phillips, recently posted on GitHub that he is forking it to make several improvements such as deleting the “tons of useless and unmaintained compiler log messages”.
“The goal is to fix some of the innumerable problems with Scala while retaining a painless transition path for existing Scala code,” he wrote.