As simple as possible, as complex as necessary

Simpler param syntax in cfscript

11 September 2013

Adam Cameron has explored this at some length, but since one of the goals of this blog is to highlight anything which makes writing apps with CFML simpler, I feel the need to give it a brief mention.

<cfparam> is an essential little tool for making sure that a variable you are about to rely on exists with a default value.

<cfparam name="rc.ID" default="34">

My controllers are full of them, since that's where I do my "first line of defence" validation. (<cfparam> can also check the variable's type, but I've always found it easier to write my own explicit type-checking logic after the existence check rather than handle the invalid type exceptions it throws.)

Since ColdFusion 9, it's been possible to use <cfparam> in script, the documented syntax being:

param name="rc.ID" default="34";

But seemingly unbeknownst to all but the members of the now defunct CFML Advisory Committee, the following syntax is also possible:

param rc.ID=34;

(Warning: As Adam notes, Railo currently only supports this syntax for unscoped variables, so this example using rc.ID wouldn't work.)

Not only is this shorter by around 15 characters, it's also more consistent with the way argument defaults are specified in scripted functions:

void function meow( loudness=1 ){
	WriteOutput( "<p style=""font-size:#arguments.loudness#em"">Meow</p>" );

In a comment on Adam's blog, Mark Drew voices misgivings about this syntax, pointing out the potential dangers with poorly named unscoped variables. Personally I'm with Adam in that poor coding practices are just that.

Consume your syntactic sugar responsibly.

Posted on . Updated


  • Formatting comments: See this list of formatting tags you can use in your comments.
  • Want to paste code? Enclose within <pre><code> tags for syntax higlighting and better formatting and if possible use script. If your code includes "self-closing" tags, such as <cfargument>, you must add an explicit closing tag, otherwise it is likely to be mangled by the Disqus parser.
Back to the top