Dumbass Code of the Day – 10/08/2009

Posted: 8th October 2009 by Adam Presley in ColdFusion, Development
Tags: , ,

Welcome to another edition of “Dumbass Code”. Here’s a snippet that’s fun. This bit of code gets a list of product IDs, iterates over them, and if the product ID is greater than zero, and less than 999,999 then it is a “special” type of product and is not counted. In this case a “special” product is a shipping record, or handling, etc… And “not counted” is done by using the listRest ColdFusion method to remove an item. I hadn’t even HEARD of this method till now, and it turns out that it just removes the first element from a list. Okaaaayyy…

1
2
3
4
5
6
7
8
9
10
11
<!-- this gets a count of actual products -->
<cfset prodlist = ValueList(getProds.product_id) >
<cfif listlen(prodlist) gt 0>
	<cfloop index="thisprodid" list="#prodlist#">
		<cfif 999999 lt evaluate(thisprodid) or evaluate(thisprodid) lt 0 >
			<cfset prodlist = listrest(prodlist)>
		</cfif>
	</cfloop>
</cfif>
 
<cfset numprods = listlen(prodlist)>

Kind of odd eh? Seems like a lot of unnecessary processing to just get a count. How about something like this?

1
2
3
4
5
6
7
8
<!--- Get a count of actual products --->
<cfset numProducts = 0 />
 
<cfloop query="getProds">
	<cfif getProds.product_id GT 0 && getProds.product_id LT 999999>
		<cfset numProducts++ />
	</cfif>
</cfloop>

Hmmm.

Related Posts

Related posts:

  1. Dumbass Code of the Week – 10/05/2009
  1. ike says:

    I’ve used listrest() quite a bit, but that’s a totally nonsensical use for it… how about a query of query?

    select count(product_id) as mycount from getProducts where product_id 999999

    I guess it doesn’t save much coding tho… But whoever wrote the original needs some remedial work… what’s with the extra evaluate()s? It’s a number, it doesn’t need to be evaluated.

  2. ike says:

    Kooky! WordPress handles comments incorrectly. ;)

  3. NO idea why the evaluates are there. It blows my mind how many evaluates and unnecessary pound signs I find in old CF code. And yeah, a QoQ would do the job fine as well. Good to know someone has heard of listRest! I sure hadn’t!