Quick Test - Script Based UDFs

This post is more than 2 years old.

I've been writing UDFs for a long time - CFLib was begun when ColdFusion 5 was still in development. But I have to tell you - this one surprised me. Without checking the docs or writing code, tell me what the following script will do:

<cfscript> function foo(name) { return "beer"; } </cfscript>

<cfdump var="#foo()#">

If you answered, "Will throw an error, oh hairy one!", then you would be.... wrong if you are using ColdFusion 9. From ColdFusion 5 to ColdFusion 8, the existence of a argument in the method signature has always implied the argument is required. The docs made this clear (and thanks go to Sam Farmer for digging up these links):

http://livedocs.adobe.com/coldfusion/8/htmldocs/UDFs_03.html#1193711
Names of the arguments required by the function. The number of arguments passed into the function must equal or exceed the number of arguments in the parentheses at the start of the function definition. If the calling page omits any of the required arguments, ColdFusion generates a mismatched argument count error.

However, in ColdFusion 9, the docs change:

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSE99A664D-44E3-44d1-92A0-5FDF8D82B55C.html
Specifying the required keyword makes the argument mandatory. If the required keyword is not present then the argument becomes optional.

So to make the UDF above work like ColdFusion 5-8, you must change it to:

<cfscript> function foo(required name) { return "beer"; } </cfscript>

<cfdump var="#foo()#">

Personally I don't think this is a huge big deal. Any code that required name would fail as soon as you didn't pass it. But it's something you want to watch out for.

P.S. If you are curious about the first version and what became of name - it existed as an argument but had a null value.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Dan Skaggs posted on 12/3/2010 at 5:08 AM

Yet another reason for me to continue to write CFCs in tag format. I think I must be one of only a handful of people in the entire CF space that detests cfscript in general and script-only CFCs specifically.

Comment 2 by Sam Farmer posted on 12/3/2010 at 10:08 PM

@Dan: So, how do you feel about script based code? ;)

One thing I like about ColdFusion 9 is the ability to use both tags and script based on developer/team of developers preference. Choice is good!

Comment 3 by Andy Sandefer posted on 12/5/2010 at 7:59 AM

I agree with @Sam that choice is good (personally I've been a tagger for so long that I'll probably continue to work that way).

And since we're talking about arguments, keep in mind that if you write everything in CFCs (and I pretty much do) and if you are writing your methods so that they can be consumed as web services then ALL of your arguments are assumed to be REQUIRED when your method is invoked from the outside world as a web service.

Comment 4 by dawesi posted on 12/6/2010 at 10:19 AM

I'd assume the code inspector would check this as part of the compatability checks... or does it?!

Personally I think this tweak in 9 is the beez neez. I've been a script monkey for years, so I'm onboard the new feature.