I was pretty exclusively a Java developer for twelve years before making the switch to Scala. Yes, it was difficult to learn some of the syntactical differences, but now that I have I don’t want to turn back. No, this is not a baited question to get a flame-war going, but an honest attempt to get some real feedback by those that believe Scala is more difficult than Java.
Let me lay some ground-rules before we get this discussion going though. First, complex topics in Scala are complex, there’s no debate there, but that doesn’t make Scala more difficult than Java, there are just more possibilities to what you can accomplish. I say that the argument cannot be made that Scala is more complex if there is not a side-by-side comparison to Java code that is much simpler to understand. This means we have to leave the advanced concepts of implicit conversions, multiple inheritance, and so much more that Scala can do, but are features that bring it beyond the capabilities of Java. Yes, of course you can make an argument that in switching to a language like Scala you will eventually run into these scenarios, but I am speaking purely of the introductory programmer and the “difficulties” between Scala’s syntax versus Java’s.
Now, can someone give me a scenario of code in Java that would be more complicated to understand in Scala?
This comment has been removed by the author.
The way you are going about this is a little unfair. Because Scala is a multi-paradigm language, and, in a sense, Scala is a superset of Java, any Java code could be written in the same way as Java, and then made even simpler.
However, you could ask the opposite question. Could you write a piece of code in idiomatic Scala whose corresponding Java code would be easier to understand? The answer to that is yes. Although the Java code would certainly be much more verbose, it would be easier to understand, or, more precisely, would require much less training to understand.
A Scala programmer would come across good Scala code that only expert Scala programmers would understand. This is not the case for Java.
I don't think Scala's designers would dispute that. However, they would argue that in spite of that, all taken together, Scala is a large improvement over Java.
I might agree, but the issue is debatable.
@pron, much depends on your definition of what is "easy". To a degree I will grant the inherent verbosity of Java makes reading intent in code easier, that only works to a certain extent. I can't tell you how many times I've looked at more complex projects and it takes hours to understand the intent of what's going on in the code as there's just so much verbose code to read through before you can understand what's going on. Yes, each line of code can be readily understood by pretty much any developer, but the mass of code you have to read through to understand the larger intent is so much greater that I would hardly call these scenarios "simpler" than their implementations in Scala. It's a fine line between verbosity simplifying and verbosity decreasing readability by shear volume of code.
I do agree that Scala code can be more complex, but only in so much that it exceeds that capabilities of Java. Further, I would ask the question, is simplicity the ultimate virtue we seek in a programming language? I'm all about simplicity, but there are things that rank higher for me. I would say number one on my list is readability of code followed by capability of the language.
"is simplicity the ultimate virtue we seek in a programming language?"
No, but you brought it up. You ask about Scala's complexity.
Readability is, in my opinion, indeed a very important trait, and that is what I was talking about. In some cases, Scala is less readable than Java, especially, as you've said, when taking advantage of the language's more advanced features.
Sometimes (perhaps more often than not), Scala is more readable.
But can someone design a language that is always more readable than Java? Absolutely. In fact, Scala is almost that language, except that its designers have decided to forgo that goal in favor of making Scala more powerful. Also, they realized that making Scala more powerful and complex can make application code more readable at the price of making the library code less so.
This is a perfectly fine tradeoff, it's just that now they have some explaining to do, to all those who claim (and the claim does have merit) that Scala is overly complex.
But I wouldn't worry. Scala is doing just fine.
@pron, indeed I do agree with the majority of what you said. However, I would say that the argument that Scala is more complex still only has merit in the functionality that Scala provides above and beyond what Java does.
Presuming that a development firm was interested in using Scala but had no desire for the functionality that exceeds the capabilities of Java and wanted to develop code in exactly the same way they always have there is nothing stopping any Java developer from quickly switching to Scala to produce the exact same functionality apart from slight syntactical learning. In this vacuum I would suggest that Scala is no more difficult than Java.
@Matt
"Presuming that a development firm was interested in using Scala but had no desire for the functionality that exceeds the capabilities of Java… there is nothing stopping any Java developer from quickly switching to Scala."
I've thought about that, but I am not so certain, as even a new developer has to read some library code, say in the event of poor documentation. Not only that, Scala's advanced features make Scaladoc hard to understand. With Java, often all you need is the API's Javadoc. With Scala, especially when some nifty DSL is used as an API, the Scaladoc is often just as confusing as the code.
@pron, right, it's not really a practical scenario as other variables will inevitably come into play. The point of this discussion was to say that the core of Scala functionality (the functionality that matches to Java) is not more complex than Java. It seems like far too many people hear "Scala is complex" and think that it means that it's an all-around difficult language to learn and thus they run the other way. Complex topics in Scala are complex, but simple things in Scala are just as simple as they are in Java…often even more so as Scala is much more logical and consistent in the way it is structured.
"simple things in Scala are just as simple as they are in Java…often even more"
With that I completely agree. But perhaps something can be done with Scaladoc to have it capture more clearly the use of the documented API.
@pron, I do agree that a lot more work still needs to be done in the ScalaDocs. I definitely like a lot of the improvements they've made over JavaDoc (how awesome is it to search at your fingertips?), but there is still a lot of work to be done to properly document the functionality and represent it in the best way possible.
I don't like Scala cryptic elements like methods named ":" or ":::" or the omnipresent "_". I prefer natural words like in Python. Scalas authors tend to shorten the code at the expense of readability.
In the Scala vs. Java complexity debate is often forgotten that the complexity in Java is in the frameworks and their magic. You write an annotation and the framework does the rest magically behind the scenes via reflection. But when something goes wrong, you have no idea how to fix it.
The complexity discussion should not be focused on the language but on the whole technology stack.
when i first started learning Scala a year ago, i had almost no knowledge of functional programming concepts, only Java and Python experience. at that time, i didn't understand people were able to read and understand that weird looking Scala code.
one year later, I have no problem at all in reading other people's scala code.
so, is it complex? yes, probably, but what isn't if you're not willing to learn. why do people expect to immediately understand everything? let's show some java with generics to someone who doensn't know what generics are …
to conclude, I would say that if i'm able to learn scala, then it can't be that complex at all. i'm not a genius, i'm not a math geek, just a simple programmer.
@daemon, I agree that some of the methods in Scala can be cryptic until you are used to them. However, I would prefer reading:
o3 = o1 : o2
instead of
o3 = o1.doSomethingCleverWithAReallyLongName(o2)
Sure the latter is more verbose, but if you are consistent in the types of methods you are injecting then there's just a bit of a learning curve to understand what that method is.
@Stefan, you make a good point. I think that is the cause of some of the complexity debate. Most of the other JVM languages focus on making striking similarities between conventions with Java for ease of use. Scala didn't really focus on that, which I think is a good thing. Scala is much more centered around logical structure and good language design than maintaining consistency with a flawed existing language.
Honestly, the Scala *language* isn't such a big problem. It's the libraries. When any random programmer in a large organization can just start creating the +#$: operator and using it everywhere it creates a slippery slope to unreadable code. In my opinion, things that would be intuitively readable in Scala are not simply because the author of the Actor library thought it would be cute to define the "!" operator instead of some readable variant like "send", not to mention "!!" and "!!!". The language is not so hard to learn. The libraries are almost cryptic because Scala encourages it to a fault.
I would love to see a set of traits like ReadableActor, etc which translated all these strange operators into English and a mode that enforced use of readability. I feel a bit like I'm complaining to a brick wall though. All you ever hear from Scala people is "you don't get it." Actually, for the problems I'm trying to solve (scaling code across large teams at large organizations where you HAVE people who are not rocket scientists), I do get it.
@jonathan, I do appreciate the added complexity that operators as methods can create, and I don't want to down-play that, but I think it can actual reduce complexity as well. For example, in Akka Actors the !, !!, and !!! methods are cryptic when first start using them, but once you've become used to it I find it much more readable and quicker to scan than other more verbose method names. However, it can definitely be overused and improperly used. I think rather than blaming Scala for allowing such functionality we should simply encourage more responsibility from API developers to use it properly.
The idea that any kind of social pressure is going to restrict wanton use of this feature is naive at best. Even if people agreed unanimously, they aren't going to agree on which cases are best for the =+* operator.
I think the smart thing to do would be for Scala's creators to admit they are having an acceptance problem here (which they are) and add alternative English operators for the existing core Scala operators and further add a mode that requires use of those English versions as well as a mode that restricts or eliminates the ability to do this.
As a language for elite and academic programmers, Scala rocks. But driving a language where you can redefine practically anything into the enterprise is going to be a nightmare. It's really kindof sad to me that there isn't a -readable mode for Scala because I could get pretty excited about it. I *do* understand why it's better than Java.
@jonathan, the acceptance problem is really pretty low from my perspective. It seems there are a few people that don't like this feature and complain about it, the rest just learn to use it and eventually come to love the syntax. Personally I think it's just learning curve. Once you've become used to it, it's really no big deal. Further, I haven't seen very many APIs that do evil things with operator methods and of those that do, none are ones I would want to use.
Success of an API is dependent upon the acceptance of the users. If the users have difficulty understanding and using the syntax then ultimately the project will fail.
I'd second Jonathan's observation about the use of implicit operators not being accepted in larger organizations/codebases. I have seen in practice at one of the largest internet companies that it is not allowed at all for precisely that reason.
I'll also throw my 2C in that personally I like having the operators around. But that's secondary to adoption which is relatively slow and challenging given how powerful scala the language actually is.