Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at the jQuery Conference 2010 (Boston, MA) with: Ruthie BenDor
Ben Nadel at the jQuery Conference 2010 (Boston, MA) with: Ruthie BenDor@unruthless )

ColdFusion 2016 Skips Over Undefined Elements With For-In Array Loop

By Ben Nadel on
Tags: ColdFusion

This morning, when I was writing up my post on generating a Java Thread Dump programmatically in ColdFusion, one thing I had to cope with was an Array that may or may not contain undefined elements. I wanted to use a for-in loop to iterate over the given array; but, I wasn't sure how ColdFusion would behave if the given array was "sparse". As such, I used CommandBox's multi-engine support to quickly test the behavior and see how and if it has changed over time.

First, I put together a test script so that I could see how many iterations ColdFusion performed when an array contained undefined elements:

  • <cfscript>
  •  
  • // Create an array in which several indices are "undefined".
  • values = [];
  • arrayResize( values, 5 );
  • values[ 1 ] = "defined";
  • values[ 5 ] = "defined";
  •  
  • // Check to see which ColdFusion engine we're running on.
  • if ( structKeyExists( server, "lucee" ) ) {
  •  
  • writeOutput( "Lucee " & server.lucee.version & " : " );
  •  
  • } else {
  •  
  • writeOutput( "ColdFusion " & server.coldfusion.productVersion & " : " );
  •  
  • }
  •  
  • // Iterate over the values in the array and indicate iteration.
  • for ( value in values ) {
  •  
  • writeOutput( " x " );
  •  
  • }
  •  
  • </cfscript>

As you can see, 3-of-the-5 elements in this array will be undefined. Now, when I execute this code in the various ColdFusion engines (using CommandBox), I get the following output (concatenated):

ColdFusion 10,0,23,302580 : x x x x x

ColdFusion 11,0,15,311399 : x x x x x

ColdFusion 2016,0,07,311392 : x x

ColdFusion 2018,0,01,311402 : x x

Lucee 5.2.9.31 : x x x x x

As you can see, ColdFusion 2016 and ColdFusion 2018 only perform two iterations, skipping over the undefined values. Earlier versions of Adobe ColdFusion and even the latest version of Lucee ColdFusion all include undefined elements during the for-in array iteration.

NOTE: You can use the isNull() function to determine if the iteration index value is undefined.

Once I figured out where the behavior changed, I looked up the ColdFusion 2016 release notes and saw that the old behavior was filed as a bug by the master-blaster, Adam Cameron. I am not sure that I entirely agree with the idea that the old behavior was a bug. And, it's interesting to see that Lucee does not offer compatible behavior. That said, skipping over undefined elements would certainly make most use-cases easier.

Anyway, this was mostly a note-to-self so that I could mentally catalog the change in behavior. Unfortunately, I'm still on an earlier version of ColdFusion at work; so, this change in behavior is somewhat moot. Still, it's good to know about.



Looking For A New Job?

Ooops, there are no jobs. Post one now for only $29 and own this real estate!

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader Comments

I am not sure that I entirely agree with the idea that the old behavior was a bug

it's a bug in that the behaviour of the tag version and script version should match. It doesn't.

I was not making any observation of which of the two behaviours I consider "right" (although now we're talking about it... <cfloop> was doing it right, and that's how for..in should work too).

Keep up the good work, Ben.

--
Adam

Reply to this Comment

@Adam,

Oh snap, I didn't even realize that the Tag version worked differently! Thank you for clarifying on that. I am mostly script these days. And, frankly, the use of undefined array elements almost never comes up for me (I just happened to be pulling something back from Java).

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
NEW: Some basic markdown formatting is now supported: bold, italic, blockquotes, lists, fenced code-blocks. Read more about markdown syntax »
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.