An Architect's View

CFML, Clojure, Software Design, Frameworks and more...

An Architect's View

Why I Like Scala

June 10, 2010 ·

I've not been blogging much lately because I've been heads down in a large ColdBox / ColdSpring / Reactor project and it's literally been taking all my time. What free time I ought to have has been consumed by FW/1 and managing the growing Railo Consulting business! The large project has been very interesting and it's drawing to a close as we approach launch. We've faced a number of very interesting challenges along the way and we've had to be pretty creative about our solutions.

One particular challenge has been the large, dynamic data set that the system's users represent and the fact that we have to export a subset of that user data to a fuzzy search engine that likes its data in XML (and, fortunately, its queries in JSON). CFML is very good at dealing with both XML and JSON but the vast amount of data and the huge size of the XML data packets being pushed around meant that CFML just simply wasn't the right language for that task. Sure, CFML can make individual user XML packets and post those to the search engine but what we really needed was a high-performance daemon that could run in the background, automatically publishing and re-publishing the entire database as needed so that we didn't have to deal with thousands of HTTP connections from CFML to the search engine during peak traffic, allowing us to scale more easily.

I've tried to pick up a new language each year for the last few years. I have experience with functional programming languages from back in my university days (and that formed the core of my PhD research back in the mid-80's) so I've dabbled with Haskell and Erlang in the past - but I've never had cause to use them commercially (unfortunately). In 2008, I focused on Groovy and liked the "dynamic Java" nature of it, stripping away all the "noise" in the syntax (such as semicolons!) so you can be much more productive than in Java. The Broadchoice Workspace - desktop collaboration - app used Flex / AIR on the front end and Groovy / Spring / Hibernate on the back end. It was a good experience (and I've recently done some more consulting on that project and it reminded me why I enjoy Groovy, all over again).

2009 offered an opportunity to learn a high-performance, statically typed language that supports both the OO style I've been using since the early-90's as well as the functional style I've known since the mid-80's: Scala. Like Groovy, Scala allows a nice, bare syntax (again, without semicolons!) although Scala is much heavier on operators than many other languages. Scala's function style, free of side effects, lends itself well to lock-free concurrent programming and that was just what I needed for the background process that scanned the database and published XML! In addition, XML is a native data type in Scala so it's incredibly easy to generate - and parse - XML directly in the language, without needing to revert to library functions.

Scala is statically typed, like Java, but it allows you to omit most of the type information since it infers it from the code. Whereas in Java you have to specify the type on both sides of a declaration like this:

SomeType stuff = new SomeType();

In Scala, it can deduce the type of the variable from the type of the expression you initialize it with:

val stuff = new SomeType()

Combined with the higher-order constructs afforded by closures (which you also get in Groovy) and functional programming constructs, the net result tends to be code that has very few type declarations anywhere in the code, except in function signatures. If you've written much Java, you'll appreciate how much less typing you have to do!

I've long recommended that CF developers learn additional languages. I've also long cautioned against learning Java as being both "too similar" and "potentially misleading" for CF developers (because writing CFML in the style of Java does both languages a disservice). I've recommended languages that are way off the mainstream like Prolog and Haskell because they make you "think different" and I believe that to be one of the best ways to improve as a developer. I think Scala is different enough that it makes a worthy target for CFML developers - in lieu of the far less mainstream Haskell - so you can experiment both with functional programming and with actor-based concurrency. Scala's structural type matching (via expression match / case construct) offers a little taste of Prolog too.

But don't just take my word for it: read why David Pollak came to Scala in 2006 and why he still loves it so much! David Pollak is the creator of the Scala-based Lift Web Framework (something else on my 'to learn' list!).

Tags: coldfusion · oss · scala

11 responses

  • 1 Sean Corfield // Jun 10, 2010 at 3:45 PM

    BTW, 2010 is "Learn Clojure" year!
  • 2 Garrett Johnson // Jun 10, 2010 at 4:17 PM

    In regards to Scala, do you find that it would be easy for developers to get in deep with out being Java junkies. I personally enjoyed playing with Groovy, but when it it came time to get remotely deep with Grails, not knowing much Java at the time was a real hinderance.
  • 3 Jaime Metcher // Jun 10, 2010 at 5:22 PM

    Hey Sean,

    Are there any Scala learning resources that stood out for you?

    Also - you're using Scala + Hibernate, right? So ditto for that - is there anything in the web jungle that you've found particularly useful?
  • 4 Sean Corfield // Jun 10, 2010 at 7:06 PM

    @Garrett, I'd say Scala is sufficiently different to Java that a lack of Java knowledge would not be the real hindrance... If you don't have any comp sci background, the Scala type system may be hard to get your head around. It's definitely worth the effort tho'.

    @Jaime, I'd start here:

    then consider David Pollak's "Beginning Scala" book.

    The nice thing about Scala is the REPL (Read-Evaluate-Print-Loop) where you can just type in 'scala' and get a console that you can type Scala code into and see it evaluated immediately.

    An interactive console like that can be really valuable for learning a new language (and most new-ish languages have one these days).

    It's one of the things I'm looking forward to about Railo 4.0 - because it will be able to execute outside a Servlet context, I plan to create a REPL for CFML, as well as the ability to write shell scripts in CFML! :)
  • 5 Sean Corfield // Jun 10, 2010 at 7:23 PM

    @Jaime and to answer your second question (sorry), no, I'm not using Hibernate with Scala. For the quick'n'dirty approach to DB queries I needed, I created a wrapper class for Java's ResultSet that let me do things like:

    db.doQuery( "select * from user where active = 1" ) foreach { row => sendNotification( row( "email" ) ) }

    which sends a notification to each active user. My wrapper class supports foreach / map and named column lookup (and a few other things).
  • 6 Eric Cobb // Jun 16, 2010 at 7:30 AM

    Sean, you mention that you caution against CF developers learning Java, which is something that I was actually getting ready to do. (I've got Head First Java sitting on my desk now.) At work we're strictly CF/Oracle, so I thought bringing Java in could really be of benefit to us, plus I've been wanting to get into it for a while.

    I have no formal Computer Science background, and I've been coding almost solely in CF for the last 10+ years. That's pretty much the extent of my programming world.

    One of my goals this year is to learn another language, preferably something that I can integrate into my daily job (vs. learning it at home and never doing anything with it). I had pretty much made up my mind to get into Java, then eventually start playing with Groovy.

    But your thoughts on Scala have intrigued me. For someone like me who's looking for a new language to get into, which of the three (Java, Groovy, Scala) would you recommend?
  • 7 Sean Corfield // Jun 16, 2010 at 9:31 AM

    @Eric, my main caution against learning Java is that because there is so much literature about Java and it's so mainstream, it's all too easy for a CFer with no other language experience to fall into the "Java way" of writing code and then start writing CFML like Java.

    Groovy encourages a much more dynamic style of development and with Grails draws more on the inspiration of Ruby (conventions, MVC, dynamic types, collections, closures) so a CFer who learns Groovy is less likely to fall into the "Java way".

    Scala is much more 'different' and I think can bring a radically new way of thinking about problems, and I think that is valuable in terms of helping CFers think outside the box.

    Scala is a harder language to learn than Groovy (IMO) and that's also a good thing because it challenges you to grow. Scala is very efficient (because it is statically typed - it just infers most of the types in the code). Groovy tends to be less performant than Java (and noticeably so in some situations), much as CFML tends to be less performant than Java.

    If you were planning to go down the Spring / Hibernate path (traditional Java 'enterprise' style), I'd recommend Groovy as being the easiest fit even tho' you won't learn so much in the 'new thinking' area.

    If you're willing to go a bit further from the mainstream, I think Scala will bring better performance and will enhance your skills a lot more than Groovy.

    Hope that helps?
  • 8 Eric Cobb // Jun 16, 2010 at 11:53 AM

    @Sean - Thanks! That's a good point about the 'new thinking' area, I hadn't given that any consideration. I was mainly interested in learning something new, that was still 'close to home' and could tie in to what I already know. Java just seemed like a natural progression to me.

    But the idea of enhancing my skill set with something totally different does have it's appeal. I can definitely see where having "a radically new way of thinking about problems" can be a huge benefit. I think I may start reading up on Scala and see what it has to offer.

    To be completely honest with you, I was actually under the impression that Groovy and Scala were both just application frameworks of some sort that ran on top of Java. My thinking was that I needed to already know Java in order to use Groovy and Scala effectively.

    Looks like I've got some more studying to do!
  • 9 Sean Corfield // Jun 16, 2010 at 12:26 PM

    There are a lot of languages that run on the JVM now and some of them are easier to learn than others. These days I don't see much point in learning Java specifically but I think you'd find Groovy 'close to home' if you wanted to go that route.

    We're in a world now where the JVM is pretty much the standard cross-platform runtime for server-based programming so you can learn Python (Jython), Ruby (JRuby), Groovy, Scala, Clojure and many others and they all interoperate so multi-lingual projects are not only feasible but can be very practical and attractive. There's even a project to bring Erlang to the JVM (as Erjang).

    Now that Clojure and Scala have brought semi-mainstream functional programming to the JVM and there are rumors of tail call recursion support in the JVM at some point, we might see other functional languages port to the JVM as well (which would be a huge win, in my opinion). Haskell on the JVM would be sweet.

    Prolog on the JVM would be sweet too (there are various Java-based interpreters but I'm not familiar enough with them to get a sense of either how complete they are as implementations of Prolog or how well they interop with other JVM languages).
  • 10 Eric Cobb // Jun 16, 2010 at 12:46 PM

    Sounds like I'd better get a move on and learn some of this stuff! ;)

    Thanks for all of your help!
  • 11 Paul Houle // Jun 22, 2010 at 1:26 PM

    I think Scala is overrated.

    I spent a lot of time wrestling with the type system in C# 3.5 and went looking to Scala to understand the theory that was underlying my practice.

    I did a small project in Scala and was happy, but once I started doing more I ran into some walls. There are quite a few little practical problems, but overall, Scala feels more like somebody's science project than a good programming language.

    There are many things in Scala that are "clever", such as the way constructors work and the ways it tries to sweep nulls and Exceptions under the rug that are really non-answers to problems that are probably worse than the status quo.

    Scala's answer to the static scope, for instance, appealed to me at first because it was a lot like an object model that I'd implemented on top of the PHP language -- the kind of system you can implement on top of a crappy language. not the kind of system you ~could~ implement if you were implementing a language from scratch. In fact, PHP is doing more than any other language to address the serious deficiencies in the static scope in languages like Java and C#.

    For a long time, Scala's type system has had a big "broken window" -- the type erasure that is fundamental to Java. Most of the things that I couldn't do in C# but wanted to do in Scala couldn't be done because of type erasure. Now, in Scala 2.8 they've just added a kludge that lets you explictly unerase types, but it's a kludge.

    Scala is built on a weak foundation that's going to crumble as more gets built on top of it.

    In the meantime, C# 4 has come out and it's got covariant and contravariant generics, one of the big things I wanted to get out of Scala. C# is a great language to learn if a Java programmer wants to expand his mind, but I'll warn you that you'll have a very hard time going back to Java -- C# has been making good decisions and moving forward for a decade, while Java has been, at best, treading water.