Setting a page number from the URL, with fallbacks I have the following code, but it somehow looks a bit redundant.
As I am new to ColdFusion, I am not sure how to optimize it. Can someone suggest an alternative?

&lt;cfparam name="url.idInfopage" default="0"&gt;
&lt;cfif  not isNumeric(url.idInfopage)&gt;&lt;cfset url.idInfopage= 0&gt;&lt;/cfif&gt;

&lt;cfif isDefined("stElement.idInfopage") and isNumeric(stElement.idInfopage)&gt;
    &lt;cfset idInfopage= stElement.idInfopage&gt;
    &lt;cfif  idInfopage is ""&gt;&lt;cfset idInfopage= 0&gt;&lt;/cfif&gt; 
&lt;cfelse&gt;
    &lt;cfset idInfopage= url.idInfopage&gt;
&lt;/cfif&gt;
You could re-write it something like this:

&lt;cfif StructKeyExists(stElement,'idInfoPage') AND isNumeric(stElement.idInfopage)&gt;
    &lt;cfset idInfopage = stElement.idInfopage /&gt;

&lt;cfelseif StructKeyExists(Url,'idInfoPage') AND isNumeric(Url.idInfopage)&gt;
    &lt;cfset idInfopage = Url.idInfopage /&gt;

&lt;cfelse&gt;
    &lt;cfset idInfopage = 0 /&gt;

&lt;/cfif&gt;


Notes on the changes:


StructKeyExists is generally recommended over isDefined.
isNumeric wont return true for empty string.



If you have a lot of code like this, it might make sense to create a function, something like:

&lt;cfset idInfopage = getNumberOrZero('idInfopage',stElement,Url) /&gt;


Then implemented as:

&lt;cffunction name="getNumberOrZero" returntype="numeric" output=false
    hint="Loops through structs (args 2..n) looking for number; else return 0"&gt;
    &lt;cfargument name="Key" type="String" required /&gt;
    &lt;cfset var i = 0 /&gt;

    &lt;cfloop index="i" from=2 to=#ArrayLen(Arguments)#&gt;
        &lt;cfif StructKeyExists(Arguments[i],Arguments.Key)
            AND isNumeric(Arguments[i][Arguments.Key])
            &gt;
            &lt;cfreturn Arguments[i][Arguments.Key] /&gt;
        &lt;/cfif&gt;
    &lt;/cfloop&gt;

    &lt;cfreturn 0 /&gt;
&lt;/cffunction&gt;