CFARGUMENT/CFPARAM and DEFAULT

Say I have code like this (intentionally left incomplete):

<cffunction name="myMethod" …>
  <cfargument name="item" … required="true" />
  <cfargument name="childList" … required="false"
    default="#getChildList(item.id)#" />
  … do some stuff …
</cffunction>

Now you'd hope that CF wouldn't actually execute the getChildList
method unless it the variable was missing, but from the structure of
code, you might expect it to anyway.  Fortunately, CF will NOT
execute the default expression unless the variable is, in fact, missing.

Now consider this code:

<cfparam name="childList" default="#getChildList(item.id)#" />

In this case, the default expression will execute, regardless
of whether the 'childList' variable already exists.  A little
inconsistency in the language that has the potential to bite you, if
you're not careful.

2 responses to “CFARGUMENT/CFPARAM and DEFAULT”

  1. barry.b

    thanx Barn..

    - you're right – I didn't put it together like you just did and I've just taken them both at face value, not realising how inconsistant they were.

    I susect this is so interwoven into the language now that it's too late to change … good catch

    cheers
    barry.b

  2. Chris P.

    Kind of like: You should never use IIf() with a DE() that won't always validate.
    But, kinda not.