Concision in ColdFusion and Scala

Friday, October 16, 2009

I do not intend this post as some kind of Scala vs. ColdFusion shooting match. Rather, it’s about enhanced script support in CF9, but it was inspired by a nice post I read on the Scala language regarding a feature I’ve long-loved about ColdFusion: named and default arguments

I've been programming with ColdFusion and Java for a long time. Recently, I've started dabbling with Scala, and it's a language I'm starting to really like. One of the reasons I’ve taken to Scala is that in a number of ways it solves certain verbosity problems that ColdFusion solves as well. I decided to write this blog post after reading this article by Heiko Seeberger on named and default arguments in Scala 2.8. As I was reading, I thought, “Cool! Scala does something that CF has done for a long time”… something which has always irritated me about Java. And with ColdFusion 9, things get even better.

When we talk about verbosity, we’re talking about at least two things:

  1. language constructs
  2. language syntax

Sure there are others, but these two are what I want to discuss briefly right now. Let’s talk about constructs first.

Concision via Language Constructs

Argument Defaults

A traditional, pre-CF9 ColdFusion component (CFC) might look something like this:

<cfcomponent output="false">

<cffunction name="init" output="false" access="public" returntype="Order">
<cfargument name="id" type="numeric" required="true"/>
<cfargument name="items" type="array" required="false" default="#ArrayNew(1)#"/>
<cfargument name="ShippingMode" type="string" required="false" default="StandardShipping"/>
<cfset structAppend(variables,arguments)>
<cfreturn this>


Here we see one required argument and two optional arguments. The optional arguments have default values. This means you could create an Order in at least these ways:

<cfset items = ["boat","car","flower"]>
<cfset order = createObject("component","Order").init(1)>
<cfset order2 = createObject("component","Order").init(1,items)>
<cfset order3 = createObject("component","Order").init(1,items,"FancyShipping")>

Now, if you were to do this in Java, you’d have even more lines of code to write in your Order class. Java does not have defaults for arguments, so you’d need to write 3 different constructors. Heiko illustrates this using Scala 2.7 syntax in his article, but it’d look roughly the same in java, only worse. Your constructor signatures would roughly be:

public class Order {
public Order(int id){}
public Order(int id,String[] items){}
public Order(int id,String[] items, String shippingMode){}

Named Arguments

In the snippet above, I created the object by passing in 3 unnamed params. ColdFusion knows which arg is which based on position. But what if I wanted to create an order instance with the default argument for items, but “FancyShipping” for the ShippingMode? I.e. I want to pass params for arguments at positions 1 and 3, but not 2. Fortunately, ColdFusion makes this easy with named arguments:

<cfset order4 = createObject("component","Order").init(id=1,shippingMode="FancyShipping")>

In addition to making the code more readable, it affords you the ability to pick and choose which params you wish to pass, leaving the rest to the defaults.

Obviously, you can’t do this in Java, so you’d need to add a fourth constructor:

public class Order {
public Order(int id, String shippingMode){}

As Heiko points out, Scala 2.8 solves this Verbosity-by-lack-of-language-construct problem by providing named arguments and argument defaults. Ripped right from his post:

case class Order28(id: Long, items: List[Item] = Nil, mode: ShippingMode = StandardShipping)

And to create an order, you could do:

val order = Order28(1)

val order2 = Order28(1,item list goes here, OvernightShipping)

val order3 = Order28(1,mode=OvernightShipping)

Nice! As Sean Corfield pointed out, one difference between ColdFusion and Scala with respect to named arguments is that Scala allows you to mix named and unnamed arguments, as in the last example, whereas ColdFusion expects you to stick with one or the other.

Concision via Syntax

Looking at the Scala version (Please read Heiko’s full post to see it in all its beauty), we see how these two language constructs enhance concision when compared with Java. However, when you look at the ColdFusion code compared with the Scala code, you see that the Scala version is tighter and, in my opinion, simply nicer to look at. The primary reason is the difference in syntax between the two languages.

The ColdFusion version spends a lot of characters in tag names and attribute names. For instance:

<cfargument name="ShippingMode" type="string" required="false" default="StandardShipping"/>


Prior to ColdFusion 9, you could’ve written this with a function in cfscript, but it didn’t afford you all the language constructs of the tag-based version. However, with ColdFusion 9, we get “full cfscript support”, which means tighter syntax. Check out the same CFC, but in full script:

component {
public Order function init(required numeric id, array items = ArrayNew(1), String shippingMode="StandardShipping" ){
return this;

Admittedly not as concise as Scala, but I don’t care… it’s a long way from the tag based version and is quite good enough for me. In addition, creating instances of this CFC gets nicer, too:

order = new Order(27);

order2 = new Order(28, ["boat","car","flower"] ,"FancyShipping");

order3 = new Order(id=29, shippingMode="Good Enough Shipping");

Not bad, eh? A big hat tip to the ColdFusion team for giving us named arguments and argument defaults since waaaaay back. And another hat tip to them and the CFML Advisory committee for steering the language, syntactically, in a direction that aids in readability and makes coding more pleasurable.


sylvan.messer said...

I agree, I really like the full cfscript support in 9. Now I'll have to check out scala too though.

The groovy version is fairly concise as well :

class User {
Integer id
String shippingMethod = "kung foo"
List items = []

def u = new User(id:1)
assert u.getShippingMethod() == "kung foo"

u = new User(id:2,items:[1,2,3])
assert u.items.size() == 3

Marc Esher said...

Very pleasing to look at, Sylvan. Thanks for sharing.

sylvan.messer said...

DOH! what happened to the formatting... sorry :)

Marc Esher said...

Sylvan, interestingly enough, it shows up just fine on screen when I go to post a comment, and it shows up fine in the email that blogger sends. I think I may know the culprit, so I'll investigate.

billy said...

Great post, Marc. The CF9 script syntax, for me, is a big efficiency gain. I haven't hit Scala yet, but I've grown to appreciate Groovy, Ruby, and Python for their syntactical efficiency. With that said, though, I've tended to focus on language syntax vs. the language capabilities, tool support, and interoperability. In the context of Java, which is, of course, more verbose, Java's capabilities often trump superficial efficiency gains. For web programming, the CF9 script syntax is awesomeness.