Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with:

CFLoop Attributes Evaluated Only Once

By Ben Nadel on
Tags: ColdFusion

For index loops in ColdFusion that are looping over an array or list, I always put the ArrayLen() or ListLen() in the TO attribute. I always felt a little uneasy about it because I thought maybe the function was getting evaluated for each iteration of the index loop. My fear was that this was adding additional processing overhead for each iteration.

Well, I finally got my out of shape butt off the proverbial couch and actually tested what was going on:

  • <cfoutput>
  •  
  • <!--- Create an initial array of items. --->
  • <cfset arrItems = ListToArray( "ben,dave,mark,mike" ) />
  •  
  • Original Length: #ArrayLen( arrItems )#<br />
  • <br />
  •  
  • <!---
  • Loop over the items in the array. For each iteration, add
  • an item to the array to see if the loop will keep going.
  • In the TO attribute, use the Min() method to ensure that
  • the loop doesn't go on infinitely.
  • --->
  • <cfloop
  • index="intIndex"
  • from="1"
  • to="#Min( 10, ArrayLen( arrItems) )#"
  • step="1">
  •  
  • <!--- Output the iteration values. --->
  • Iteration #intIndex#. Array Length: #ArrayLen( arrItems )#<br />
  •  
  • <!--- Add an Item to the array to see if we keep looping. --->
  • <cfset ArrayAppend(
  • arrItems,
  • "sarah"
  • ) />
  •  
  • </cfloop>
  •  
  • </cfoutput>

Turns out, the loop only iterates 4 times; the attributes only get evaluated that first time. When you stop to think about it, that makes a lot of sense; it is just a string. And if you take Conditional loops into account, it makes sense that the condition="" attribute is a string, not an evaluation. That way it can do a sort of delayed evaluation on the condition.

Man, understanding the basics is so important.

Tweet This Groovy post by @BenNadel - CFLoop Attributes Evaluated Only Once Thanks my man — you rock the party that rocks the body!


Reader Comments

Hi Ben,

An "oldie but a goodie"... funny enough we just had a discussion about this at work, what with JavaScript evaluating at each iteration of a for loop it stems to reason that CF would do something similar. The Docs here are of course the best reference.

A conditional loop is defined with a condition = "expression" with an expression one expects evaluation per iteration.

In an index loop the docs clearly state for from,to "starting value", and "ending value", to me this clearly means a fixed value or a variable/expression that is evaluated once.

Your test just confirms it.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.