Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Increment Operator Issue with ColdFusion 8 Arrays

DZone's Guide to

Increment Operator Issue with ColdFusion 8 Arrays

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

I spent a good 30 mins debugging a module in one of our products that has been running fine in CF8 for a while but crashed when run on CF9 recently. The problem turned out to be a bug in my code but it also unveiled this freaky bug with CF8. It turns out that if you try to increment the value of a variable used as an array index when assigning a structure or array to that index using literal syntax then the variables value is not incremented. Here’s an example of what I found!

The below code shows a very simplified version of the code scenario, bug and all. The bug is on line 17 where at the root node I had left in an increment operator that I had removed from all other assignments to the array within the loop and added to the end. In CF8 this was working fine because level was not being incremented. In CF9 level was being incremented, as is should have been in CF8, and the root node was being added one index past where it should have been at the end of the array. I have tested this with adding both a structure and an array using the literal syntax and in both cases level is not incremented. When assigning a simple value or a variable that references an array or structure level is incremented. Weird!!

<cfscript>
  path = []; // cached path out to root node
  ended = false; // ended = true once we reach the root
  nodeID = 1; // leaf node db id --would have been passed in
  level = 1; // levels up from leaf nodeID
  
  // very simplified version of the code
  while (!ended) {
    parentDept = getParentDept(nodeID);
    if (parentDept) { 
      path[level] = {
        id = parentDept
        // and other stuff...
      };
      nodeID = parentDept;
    } else {
      path[++level] = { // doesn't increment level in CF8!
        id = 0 
        // other stuff...
      };
      ended = true; // root node
    }
    ++level;
  }
  
  writeDump(path);  
  
  /*
   * dummy function for example - was actuall a call to db 
   * for the parent department in a staff directory
   **/
  function getParentDept(id) {
    if (id < 10) {
      return id + 1;
    } else return 0;
  }
</cfscript>


Source: http://www.profilepicture.co.uk/tutorials/increment-operator-issue-coldfusion-8-arrays/

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}