ColdFusion is a bit inconsistent when using loops
Tags: Coldfusion
There are two things that I find a bit hard to accept here, one is a down right bug and a very hard one to ever change due to the problems of backward compatibility. The second is annoying when using ColdFusion 9 script, and would have been nice to have added it to the language.
<more/>
So lets look at the first problem. The loop and using the array attribute.
Prior to ColdFusion 8 we had to use something like this to loop over an array, which was kind of neat but awkward at the same time.
<cfset item = myArray[counter] />
</cfloop>
So let me explain this code a little, the loop is basically saying we are going to use the variable counter as an index in the arrays bounds. And I am then using another variable called item to get a handle on that element of the Array.
ColdFusion 8 introduced a quicker and better way to do this, however there is one mayor flaw with this. And that is that it is inconsistent with the rest of the cfloop and how it works. Bit before I
explain that lets look at another example of a cfloop, and hopefully this will illustrate the problem. Here is an example of looping over a structure.
Now do you notice how when defining this we are actually getting a reference to the item, itself in the above example. And not a count to its position?
And yet if we then swap back to doing a list, the code would look like this.
<cfoutput>#ListElement#</cfoutput><br>
</cfloop>
And again we have gone back to using the attribute index, and yet when it gets displayed Adobe have written it as if it is an actual element.
This is odd behaviour in ColdFusion, that actually is promoting ConFusion in the language. And it is going to be a hard road to have these changed to be more consistent in the language.
Looping over Arrays in CFScript in ColdFusion 9
So now that we have a much better syntax in ColdFusion 9, it was asked and it was never implemented by Adobe. But it would be damn handy to have it added as quickly as possible as part of the language.
Currently in cfscript to loop over an array in script you need to do something like this.
item = myArray[count];
}
What actually would have been far better to have in the language, and was proposed and ignored by Adobe engineers is this syntax.
}
This would be making the language a bit more RAD than it actually is, but Adobe ignored it for reasons unknown to us. And they promote that we have full script support as tag equivalent, but this is one case were they said well you can already do it the other way so why do we need to implement this!
I'll tell you why, its easier to write and easier to look at and quicker to understand what is happening is one thing. But being quicker to write also makes it more RAD in my eyes too.
Anyway I hope that there can be enough support given to push for the adoption of this little enhancement to the language.
-
Yeah, I found out the hard way when trying to implement the CF 8 "enhancement" to the cfloop tag that you can't get the index of an array item. IMHO makes the update to cfloop mostly useless.
# Posted By Spills | 11/12/09 9:02 PM -
@Spills - hmmm, yes and no. Most of the work that I do, this way of doing it saves time and is designed to already have the object aka item not index, at hand to do what you need to do.
But its confusing because people will see index and automatically think I can get / have the index number of the item.
The problem now is we are stuck with it in tags, but hopefully when it finally gets into cfscript it will make better sense to all.# Posted By Andrew Scott | 11/12/09 11:48 PM



TweetBacks