Simpler param syntax in cfscript
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.
Comments