Quick Test - Script Based UDFs

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()#”> </code>

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):

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:

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()#”> </code>

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 developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

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