How I Use cfparam
TechnicalBen Nadel has written two posts in the last two days about the use of the cfparam tag in ColdFusion and that got me thinking about my own use of said tag. Unlike other ColdFusion tags, the use of cfparam typically isn't critical to a template or an application so I never really thought much about why I use it the way I do. I just do. His posts got me wondering whether I could quantify my rationale.
Managed Expectations
I use cfparam to document any incoming variables and initialize them. This applies to variables in the form, URL and attributes scopes. It may also apply to request and local (variables scope) variables that I know I'm going to need, but I'm less consistent about applying cfparam to these. By grouping all of my cfparam tags for these variables at the top of my template, I can know what I should be expecting soon as I open the template.
Cleaner Error Trapping
In most of my templates, basic error trapping is the second thing I do after initializing my incoming variables. By using cfparam, I only need to check whether the variable contains a valid value. Not whether it's defined. Instead of:
I only need to worry about the latter condition:
I find that less cluttered and easier to read.
Simplified Template Management
Assuming I follow this methodology consistently, I no longer need to wonder, while I'm coding line 1438 somewhere near the bottom of my template, whether the variable I'm getting ready to reference was actually passed. I don't care. All I need to know is that it's available because I defined it at the top of my template and I know its value is valid because I checked that right after I initialized it. Now all I have to do is use it.
Mitigation of Risk and Effort Reduction
If I forgo the use of cfparam then I have to either initialize it somewhere else or use isDefined() every time I need to reference that variable. If I don't, I'm going to get errors and that will lead me down the path of searching for every use of the variable in the template and ensuring that every one of those uses is properly wrapped in a call to isDefined().
That's just too much work. I think I'll keep using cfparam, thank you.




Loading....