[thelist] CFSET vs CFParam

jeff jeff at members.evolt.org
Fri Oct 20 17:18:49 CDT 2000


steven,

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:  From: Behalf Of Steven Wood
:
:  In all the CF pages that I've written, I've never, ever used
:  the <cfparam> tag.  Maybe I'm missing something important,
:  but I do my CYA'ing by always checking to see if variables
:  are defined before referencing them... unless I'm <cfset>ing
:  the variable directly above the block in which it is referenced.
:
:  According to the responses that you got from your original
:  question, I guess I'm probably coding more <cfif> statements
:  than I might actually need.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

i have a saying with regard to the IsDefined() function

"the IsDefined() function is pure evil."

this mindset is from a maintenance standpoint.  there are only super rare
instances where you have to use it.  most cf programmers won't encounter a
time when they'll actually need it.  many will use it because it just seems
easier to do it that way to begin with.  however, when it's time to maintain
the code it's difficult to see exactly what it is you're trying to define
and what that actual variable might be.  these are the instances where
you're combining a string and a variable to create a new value and need to
check it.

when it comes to straight variables and not an instance where you're
creating new ones, then you should never, *ever* use IsDefined().  you'll
get much more satisfactory and predictable results by using <cfparam>
setting it's default to an empty string if it will hold a string value or
setting it's default to 0 if it will hold a numeric value.  then, when you
need to double check things you can either use the Len() function to check
it's length for verification that there is data, or the Val() function to
check that it's a number.

for a string value:

<cfparam name="action" default="">

<cfif Len(action)>
  do stuff if action has a length
</cfif>

or for a numeric value:

<cfparam name="category_id" default="0">

<cfif Val(category_id)>
  do stuff if category_id is a number and not equal to zero
</cfif>

bottom line, <cfparam>ing all values you'll be using in a template is just
good practice.  it makes for less chance that a user that messes with your
urls is going to get an error screen.  it makes it easier to understand
everything that's happening in the template (provided you're being a good
toady and defining the variables and their default values at the top, or in
application.cfm if they will be used across multiple pages like "fuseaction"
or "startrow").  it also affords you the ability to reduce the complexity of
your code and having to double check the existence of variables prior to
checking their values.

good luck,

.jeff

name://jeff.howden
game://web.development
http://www.evolt.org/
mailto:jeff at members.evolt.org





More information about the thelist mailing list