An Architect's View

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

An Architect's View

Polyglot projects?

June 23, 2011 ·

In the LinkedIn ColdFusion Groups I recently asked the following question:

For the last three or four years, my CFML projects have general had other languages mixed in on the backend (Groovy, Scala, Clojure). I was wondering how many other people are leveraging the Java/JVM interoperability in CFML to take advantage of other languages?

What sorts of things have you done and why?

And if not, why not?

Several folks replied that they either didn't use other languages with CFML or they use a bit of Java. Several people also asked me to explain why I chose those other languages and how I integrated them with CFML. I replied to the group at some length but today on Twitter Aaron Greenlee asked me the same question and encouraged me to blog it for everyone to read. So, here it is...

Two things have popped out at me from the comments so far:

  1. Most folks are finding CFML provides everything they need.
  2. When most folks step outside CFML, it's generally to Java and is mostly just using a Java library.

I guess I'm not too surprised. CFML is a very capable language and it's (mostly) very easy to call Java libraries from CFML when you do need more than CFML itself provides.

So let's take my three "other" languages in turn and examine why I used them...

Groovy

At Broadchoice we were building a backend for a Flex-based AIR app. We'd decided to deploy on EC2 for scalability. CF8 didn't allow cloud deployments so we wanted something free and open source. Since we were using BlazeDS, our choices (back then) were really Java or Groovy to go with Spring and Hibernate. Java is a fussy, high ceremony language so that was out as a rapid development language. Groovy is a dynamic language that allows you to be as verbose as Java or as concise as you want. We picked Groovy. When we built a mobile web app for the system, we went with CFML, Model-Glue and ColdSpring - with Spring as the parent bean factory, which allowed us to reuse all the Groovy code exactly as-is, as the Model of the application. Since we were going FOSS, we actually used Railo Community Edition (free but not open source at the time).

Scala

At World Singles we have a large CFML, ColdBox, Reactor, ColdSpring application. We use a custom search engine that requires we mine the MySQL database behind our main app, convert data to a particular XML format and POST it to the search engine. The process has to run "constantly" and has to move (at times) very large amounts of data. The team had already tried - and failed - to use ColdFusion 8 for this before I joined and had a .NET application (that sort of worked). Our new platform was going to be Linux, so .NET wasn't going to work. We prototyped with Railo but confirmed that would cut it either (for this piece). So it was Java or...? Groovy isn't really fast enough for this (the price you pay for a dynamic language most times) and I certainly didn't want to suffer the verbosity of Java. Which meant Scala - statically typed, fast, hybrid OO / functional.

Clojure

I like the functional style - that's a whole other discussion - but Scala, whilst concise, has a very expressive strong type system. It gets a lot of its power for the type system but... well... there are a lot of subtleties and I think a strong CS background is required to really get the best out of Scala. That means it wouldn't work for our whole team. I wanted something functional, but something that behaves more like CFML in terms of being a dynamic scripting language. That pretty much narrowed it down to Clojure. The question was: would the other team members take to something that, whilst simple, looks so alien? The answer so far is a tentative "yes".

Why functional?

Pure functional code has no side effects, just "pure" (aka mathematical) functions from input to output, and all the data structures are immutable. That means that there cannot be any thread safety issues by definition, and the runtime can take better advantage of multiple cores by executing code "in parallel" - which is safe when there are no side effects!

The other aspect of functional code that I like is the ease with which you can apply transformations, reductions and filters across (large) data sets - and especially the ease with which you can reuse and compose all the little pieces of functionality.

CFML is still far and away the best web templating language I've ever seen and for basic CRUD and control logic it works very well. Once you get outside that comfort zone, CFML can still serve you well - and may well be "good enough" - but each language excels at different things, despite being general purpose languages, and there are times when mixing languages makes it easier to solve the problem at hand.

As for integration, CFML to Scala is much like CFML to Java. CFML to Clojure can be just like CFML to Java - if you compile all your Clojure code ahead of time to .class files - but the typically way to leverage Clojure is as a dynamically loaded scripting language (which is then compiled on demand to Java bytecode in memory). I have a small CFML library that creates an instance of the Clojure runtime, then loads Clojure source files and attaches the namespaces (packages) to elements of CFML structs, using a wrapper with onMissingMethod() to make it seamless to call Clojure functions from CFML: clj.clojure.core.count( clj.clojure.core.range( 1, 100 ) )

Hope that answers all the questions in sufficient depth?

Tags: clojure · coldfusion · groovy · scala

7 responses

  • 1 mous anony // Jun 23, 2011 at 11:57 AM

    I think the reason you choose to use all these languages is just because you got fed up with CFML.
  • 2 Greg Moser // Jun 24, 2011 at 6:35 PM

    Great post, really helped me to clarify a lot of things that I had assumed but was never certain about. This has also convinced me to branch out and start searching for the "best tool" type solutions.
  • 3 Sean Corfield // Jun 29, 2011 at 10:57 PM

    @mous, easy to hide behind anonymity and snipe - how about you own up? I use CFML for View/Controller because it's the best solution there. I like Clojure for the model because of the data processing and thread safety benefits.
  • 4 Sean Corfield // Jun 29, 2011 at 11:00 PM

    @Greg, glad it helped. Feel free to ping me if you branch out and have questions about languages...
  • 5 Jas Panesar // Jul 4, 2011 at 10:40 AM

    Thanks for sharing Sean, nice read!

    I was thinking of a project once that would let you code in any language almost that could be glued together by CF.. like you said it's about inputs and outputs.
  • 6 roger tubby // Aug 7, 2013 at 10:53 AM

    Sean - coming in late to this thread so I hope it hasn't dropped off the edge of your current interests.

    Do you have any advice on how to interface Scala with ColdFusion (8/9/10)?

    I'll be moving our CF8 websites to CF10 (or maybe Railo) and I'd like to start using Scala (or perhaps Clojure) for some of our financial data modeling.
  • 7 Sean Corfield // Aug 17, 2013 at 10:35 PM

    @Roger, with Scala you just compile to .class files and package as JARs, then treat it the exact same way as Java.