|
Re: Is Scala really more complicated than Java?
|
Posted: Sep 26, 2009 4:54 AM
|
|
So complicated means "composed of elaborately interconnected parts"? Well, I don't want to sound inflammatory, but any piece of software beyound "Hello world" will match this definition. So if we need the interconnected parts, at least we can hide them so that they are not visible at all times.
Ayman Saleem says "I think using more symbols rather than English-like words degrades readability", whereas Robert Evans gives a Ruby example, which is more concise at the cost of using more symbols. The regular Java guy will be at a loss what the #{} expansion does. It seems, though, that most people agree that complicated code will not look readable.
So I'll ask again, what does complicated mean:
* to the beginner: You could write code that closely resembles your Java coding style, and that's what Martin's example tries to show. Many of the operators have alternative readable method names, e.g. instead of "/:" and ":\" you can use foldLeft and foldRight; you can use explicit variable binding instead of the underscore character for closures, etc. Not only that, but you can skip much of the boilerplate:
def main(args: Array[String]) { println("Hello, Scala!") }
compared to the Java version:
public static void main(String... args) {
System.out.println("Goodbye, Java!");
}
Here's some accidental complexity you don't need to care about. This is what the beginner will think: Why do I need to type public? What is void? What the heck is static used for? And where did System.out come from?
That's just one example where the details are unnecesarily exposed, and don't get me started on JavaBeans and properties.
* to the expert: this is where Scala shines. Using powerful abstractions, you can write libraries, which produce very concise and readable code (http://www.artima.com/weblogs/viewpost.jsp?thread=252702). Note that the beginner might not understand why the code works, but it's still fairly clear what it does and how to use it. An unnecessarily verbose version is more complicated than the short one. One example is the specs BDD framework:
List("hello") must not have size(2)
Let me ask: do you understand what this does? How many people understand how it works, though? I guess many more people will answer affirmatively to the first question than the second. Scala hides the complexity in a layer, where the user does not have to deal with it over and over and over again. Here's another example usage of lift-mapper by Apache ESME (disclaimer: I'm a committer):
Message.findAll(By(Message.author, user), MaxRows(20), OrderBy(Message.id, Descending)).
Can you tell what this does? Does it seem complicated? Best of all, a typo will not break your SQL query in ways which are hard to debug during run-time.
* to the maintainer: since Scala is more flexible than Java, it's easier to adapt your existing code so that it matches new requirements. One example is how Lift was able to replace the Scala actor library with its own implementation:
http://markmail.org/message/d77hls7ffwghrxgq
Another concurrency-related example is a parallel version of map. In the "Happy birthday" example, can you write a Java version, which executes in parallel with as little modification of the original code as possible? With Scala, you can mostly substitute List.map with Mapper.pmap in this 10-line Scala class:
http://debasishg.blogspot.com/2008/06/playing-around-with-parallel-maps-in.html
With Java, the best you can do is use the ExecutorService (since Java 5). Now try to rewrite your examples in Scala and Java so that they use Google's MapReduce:
http://scala-blogs.org/2008/09/scalable-language-and-scalable.html
Try to imagine the Java version, which can handle different concurrent implementations and ask yourself: will it look complicated?
So back to the definition of complexity. In the famous words of Larry Wall, a programming language should make "easy things easy and difficult things possible". I'll add one more thing: you should make your software also easy to evolve and maintain. That's what I think the complexity of software development really is all about. With Scala, you're able to hide complexity from the beginner without making it more complicated for the expert to create a flexible implementation.
|
|