Why moving to Scala - Part I - Writing less code
blog.furiousbob.com
blog.furiousbob.com
I like the language, but sometimes it feels like it promotes a lot of "creative" coding. My concern is that the idioms for dealing with things like collections seem to change with some frequency as more and more features get added to the language.
It's possible that my concerns are misplaced, I've written < 10,000 lines of Scala so I'm certainly a neophyte. It's just been troubling to find so many suggestions for doing things like folding/reducing.
I don't mean "bad" developers as much as I mean ordinary "enterprise" developers.
The real concern I have is with hiring - what is the likelihood that you can take a competent Java developer (or any competent developer, I suppose) and expect them to make good decisions with Scala in a relatively short window of time (say 1-2 months)?
I do think it would have been possible though. In a lot of ways scala is a combination of the benefits of a compiled/type checked language (which should make things easier for developers of any skill level) + the concision of an interpreted language like python or ruby.
and the slide deck is here:
https://docs.google.com/present/view?id=dcbpz3ck_24f3v83ggz&...
For example, if you are doing a Java web project, most teams go with the Java EE platform simply because that is what is standard and so most of the things around Java in terms of the web are usually somehow in the vein of Java EE in terms of being big, kludgy, slow moving web frameworks.
So, even though nicer MVC frameworks like Play exist in Javaland, simply switching to Scala opens your eyes to the opportunities that exist in other frameworks like Lift and Play and scalatra.
In the end Scala is a nice mix of improving the language and revealing potentially better platforms at the same time.
The second problem is DDL hell. Too many changes to APIs, binary compatibility, dropped classes from 2.7.x -> 2.8.x -> 2.9.x.
That said I'm very happy with Scala as a language, but they should fix those problems.
entries = new ArrayList<Integer>() {{ add(3); add(4); }}
But yes, certainly not as elegant as Scala. Having said that, you don't encounter literal collections very often (except maybe in tests).
6) Named parameters are pretty easy to emulate:
new Window().width(100).height(50)
although the builder pattern is safer:
Window w = new Window.Builder().width(100).height(50).build();
7) No more checked exceptions: celebrating this is like saying that your code is shorter because you no longer bother checking for errors.
8) You can only omit parenthese in Scala when there is exactly one parameter:
x.foo a <-- legal
x.foo a b <-- illegal
x.foo(a, b) <-- legal
One of the many syntactic quirks that Scala suffers from
9) Comprehensions are neat but they suffer from a crippled performance limitation that forces you to revert to imperative loops, even in optimized mode. This technical problem is serious and there is no solution in sight at the moment.
There are genuine reasons to be excited about Scala but this article makes me think that the author just discovered Scala and they haven't really dug very deep yet. When you do, you start realizing that Scala's nice features are offset by quite a few annoying limitations that will probably doom this language to a niche.
1) There is a literal syntax for Java:
entries = new ArrayList<Integer>() {{ add(3); add(4); }}
Unfortunately if you have warnings turned on in your IDE, it will complain about the lack of a serialVersionUID on your new class. This can be worked around via the `@SupressWarnings("serial")` annotation, but that hurts overall readability & code line count. That makes this technique less of a win than it would seem.re 7. They're ok as long as they're helpful, but at the same time they prevent writing something like useful `map()`, unless it throws everything. How many times do you need to catch something and handle, even though the situation can never happen? (as in - code doesn't throw the exception, but interface says otherwise) How many broken functions, like throwing `sleep()` are there? How many times do you have to write catch / finally / catch / finally / ... when closing more than one resource reliably? And it still doesn't protect you from silly null mistakes. I believe that properly using Option and matching will prevent more errors than checked exceptions ever did... (I may be wrong of course)
6) Sure, now let's see all the boilerplate builder code you need to need to make that work.
7) Of course you can check for exceptions, you just don't need to add a bunch of throws clauses all over your code base to get the error bubbling up to the place where you want to handle it.
8) Doesn't look like a quirk to me. Looks pretty straightforward.
These clauses allow static verification. In other words, the compiler will remind you to handle the error cases.
If you fail to do this and the compiler doesn't nag you, you can bet that most developers will never bother doing it.
Nah, entries = Arrays.asList(3, 4);
Scala is pretty sweet with a lot of syntactic sugars, but it still it doesn't fundamentally make writing correct code any faster, especially for Java developers who are competent with their IDEs. Until Scala's compile time and IDE support improves 10x, I see no point of using Scala for production code.
Slightly more verbose Java syntax is more than made up by the instant/incremental compilation/autocompletion/refactor that works. Even as a Java noob (I'm more experienced in C++), I can bang out correct code the first time without going through any edit/compile cycles, most of the time. IDE is the new REPL and language snobs always forget that.
By focusing on the creation of the simplistic code we ignore the burden of maintaining ten lines of critical fluff for every intentional line of code.
val entries = List(Map("name"->"joe","age"->35),Map("name"->"joe","age"->32))
looks much more like PHP than like the Java example they give:
$entries = array("foo" => "bar", 12 => true);
And downvotes? wtf, php-haters?