>I prefer to CFParam my vars with a default value of a zero len string or a 0
>for numeric values. Then I skip the isdefined and just test against the
>value. Well recently someone I know said that it's better to test if it's
>defined. Is there a pro or con to doing it my way vs. IsDefined ?
There is a difference between a value being undefined, and a value being zero.
Consider the following (contrived) example:
<cffunction name="convertToKelvin" returntype="Numeric" output="false">
<cfargument name="Celsius" type="Numeric" default="0" />
<cfargument name="Farenheit" type="Numeric" default="0" />
<cfif Arguments.Celsius NEQ 0>
...
<cfelseif Arguments.Farenheight NEQ 0>
...
<cfelse>
<cfthrow message="Invalid arguments"/>
</cfif>
</cffunction>
vs
<cffunction name="convertToKelvin" returntype="Numeric" output="false">
<cfargument name="Celsius" type="Numeric" required="false" />
<cfargument name="Farenheit" type="Numeric" required="false" />
<cfif isDefined('Arguments.Celsius')>
...
<cfelseif isDefined('Arguments.Farenheight')>
...
<cfelse>
<cfthrow message="Invalid arguments"/>
</cfif>
</cffunction>
(Using cfargument/default is equiv to using cfparam/default.)
Unless you can say with certainty that blank/zero is identically equivalent to
undefined, you should always check for existance (using either isDefined or
structKeyExists as preferred) instead of setting a default.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology-Michael-Dinowitz/dp/1430272155/?tag=houseoffusion
Archive:
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:336728
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm