Saturday, October 30, 2010

Scala Bowling - A better Java or a Research Project?

A short post in the hope that I'll get some input about Scala. I first looked at Scala when it first came about, around 3 years ago now I think. My first impressions were clever, conceptually robust, but schizophrenic. It is as though the language designers decided to shoe horn in every language feature they could think of. It was clear that it was way more powerful then Java, but for me it was too clever, too complex.

Well three years later I'm looking at Scala again. I've recently come back to Java, after spending awhile in C# land, and I'm sorely missing lambdas. A quick perusal of the usual Java forums convinced me of what I had already suspected. Java 7 and closures aren't going to see the light of day anytime soon. What Java needs is a re-write, and you could argue that Scala is it! So I downloaded a Scala plug-in for Eclipse, got "hello world" up and running and started pondering what to do next?

As chance would have it, trusty Ron Jefferies has come to the rescue again with his bowling game. His first attempt at Bowling with Scala is definitely imperative. A sort of Scala for Java programers style. As he anticipated the functional community were quick to come back at him with more functional suggestions, using pattern matching and recursion. Well Scala has all the functional paraphernalia so why not use it? There is a sort of unsaid assumption by the functional crowd that a pure functional solution is somehow intrinsically superior to an imperative one. As I mentioned in my post on Haskell the jury is still out for me. Sure state is evil, and where a functional style is a natural fit to the problem domain, then a pure functional style is a no brainer, but how often is that? My gut feel is that most of the stuff I tend to do (shuffling data between a database and a browser) is imperative in nature, well at least I tend to think of it that way. To me, pure functions are things you use in maths and best suited to mathematical analysis. I get the feeling that Ron also shares my misgivings, and yet again, the expert pure functional solution was found wanting when someone discovered a failing test.

So how should we use Scala? As a pure OO language where we exploit the benefits of immutability as much as we can? Or as a pure functional language were we exclude state entirely Haskell style? It was interesting to see that the expert pure functional solution came about as a direct port from Haskell. looking at the Haskell version and the Scala version, they are almost identical. Based on this evidence you could describe Scala as a pure functional language, that also so happens to support imperative OO programming.

The guys over at Object mentor may beg to differ. Their bowling examples, whilst functional in nature also exhibit an OO quality, yet they still make use of pattern matching. So their Scala style could be considered an OO/Functional hybrid. What is the budding Scala programmer from an imperative background to do? It seems to me that Scala is still a bit of an experiment, with people still working out the best idioms.

The option of using Scala as a better Java is always there of course, and if I get the chance to do so I will, but there will always be this nagging doubt of whether I'm doing it right? And whether a real Scala programmer would approve? I think languages should promote a style. C was very good at this, with everyone adopting the style of Kernnighan and Ritchie. Smalltalk also makes a very strong style statement, exemplified by the code in the Smalltalk-80 image. Scala doesn't seem to have this. It is though it is at least two languages, or perhaps three, all coexisting. A haphazard mix of paradigms, idioms and styles.

I guess I need to read the book by the inventor Odresky to see if he as a Scala style I can comfortably adopt. More important then the discomfort of "am I doing it right?", is the obfuscation of the problem domain that comes IMHO when applying a pure functional style to problems that are more naturally expressed imperatively. Should I always be living in fear of that next failing test that blows a whole in my cleverly contrived pure functional solution? Or should I be able to reason about the correctness of my program with a modicum of confidence by simply reading it?

I'm interested in finding out the thoughts of people who are further down the Scala path. All comments and pointers welcomed.