Friday, December 30, 2011

Duplicate CFArgument Gotcha!

Today I was debugging a component which returned a query for a paginated display.

This component has the page number and limit passed in, but also has default values set in the CFArgument tags.

At some point during development (probably while copy-pasting a bunch of cfargument tags for quick editing of names and default values) one of the arguments was left twice.

At appeared something like this:

<cfcomponent ...>
<cffunction name="queryrecs" access="public" output="No" returntype="query">
<cfargument name="someid" type="numeric" required="No" default="0">
<cfargument name="something" type="string" required="No" default="">
<cfargument name="somethingelse" type="string" required="No" default="">
<cfargument name="anotherthing" type="string" required="No" default="">
<cfargument name="page" type="numeric" required="No" default="1">
<cfargument name="limit" type="numeric" required="No" default="10">
<cfargument name="limit" type="numeric" required="No" default="10">

<cfset var pagerecs = "">
<cfquery name="pagerecs">
select ...
from ...
where ...
LIMIT #arguments.limit# OFFSET #(*arguments.limit#
<cfreturn pagerecs />

Notice the two limit arguments. This is really bad. When the method is called and the "limit" parameter passed in the first cfargument tag catches the passed in "limit" parameter and the second "limit" cfargument tag overwrites the passed in set value in the ARGUMENTS scope to the default value.

It's just something to be mindful of when things aren't working as expected.

That's a wrap!

No comments: