Ability To Leverage ColdFusion Bug Will Mean Errors Later
NOTE: The following code is BAD CODE. This is just an exploration of BAD CODE. DO NOT COPY THIS CODE.
I was just reading over on Jon Hartmann's blog about problems he's been experiencing with ColdFusion 8's new implicit struct / array creation. He's coming to find out that implicit structs do not get block-level processed in ColdFusion 8, but rather, as Barney Boisvert pointed out on my blog, that it gets compiled as a collection of individual commands. To quote Barney's example:
<cfset a[arrayLen(a) + 1] = { foo = 2 } />
... gets compiled into to:
<cfset a[arrayLen(a) + 1] = structNew() />
<cfset a[arrayLen(a) + 1].foo = 2 />
... which is why it causes errors.
As I was reading Jon's post, it got me thinking; the fact that ColdFusion compiles in this way would actually give one the ability to leverage the piece-wise creation of structs. Take a look at this example I put together:
<!--- Create configuration settings. --->
<cfset objConfig = {
Root = GetDirectoryFromPath( GetCurrentTemplatePath() ),
Tags = (objConfig.Root & "tags\"),
COM = (objConfig.Root & "com\")
} />
<!--- Output congifuration object. --->
<cfdump
var="#objConfig#"
label="Configuration Settings"
/>
As you can see in this demo, I am referring to the target variable, objConfig, within the definition of the very same variable, objConfig. This seems like some recursive nightmare (how something be defined as itself plus some other stuff???), and should in fact throw an error stating that "objConfig" is not defined. But, due to the way that ColdFusion compiles the implicit struct / array creation, the code above actually runs fine and when we CFDump out the config variable, we get:
The "sloppy" code ran just as someone might "intend" it to (even though it should work).
Aside from this being bad code to begin with, the real problem I see here is that once Adobe fixes implicit struct / array notation (hopefully in new version of ColdFusion), any code that "leverages" this bug will break. I'm not advocating in any way that Adobe even care about this kind of backwards compatibility - this is simply something that popped into my head when reading Jon's blog.
Want to use code from this post? Check out the license.
Reader Comments
I dub this: the Nadel-Schrödinger Struct Creation Method.
@Rick,
I had to look up Schrödinger. Are you equating my struct creation to half living half dead cat?? :)
That code makes my head hurt.
Oh my god. That is perhaps the most horrible CFML ever, despite the fact that it works perfectly on ColdFusion. No offense, Ben, but dear god.
If I ever see code like that in real life, I will beat the author senseless. Even if you live in Australia and it takes $2k and 30 hours to fly down there to do it.
And if Adobe maintains backwards compatibility for this horrible oversight at the expense of making it work like every other language in the world, I'll find as many of them as I can to beat senseless as well. It's horrible.
@Barney,
Ha ha ha ha, please don't think that I would write something like this :) I only did it for show, I swear!
Oh, I know. But people aren't very discriminating when they see stuff on line. The tongue-in-cheek just doesn't come across. Do you remember that URL "shortening" service I made? The first one, I mean, where it was deliberately coded to ensure you only got a shorter URL if your starting URL was like 500 characters long? My faith in people dropped significantly after the number of "sweet new service, I added it to my list" and "can I deploy that on my own site?" emails I got.
I guess my point is "be careful." You know it's silly. I know it's silly. Rick knows it's silly. The half-dead cat knows it's silly. But a lot of people will say "Ben blogged about it, it must be good," with no further thought.
Ben,
This is AWESOME! I just used this to help refactor my URL shortening application based on Barney's code! But for some reason it doesn't work on Railo any ideas?
Dan
P.S. Kidding! ;-) Barney lives close enough to me that it wouldn't take him more than a tank of gas to show up at my door to rip this code out of my hands and leave me senseless. Really funny but sad observation Barney.
@Barney,
I didn't even think of that. Good point. I have put a note at the top of the blog post trying to emphasize that this is bad code!
@Ben Wow! That is some clever code! I'll be using this on all my applications. This guy I worked with threatened to defenestrate himself if I used it, but I explained, "Hey, pal: I saw it ONLINE!"
I'll bet @Barney is sick with jealousy right now...
;-)
hmmm....
it depends.
CF seems to have screwed up the expectations of what should happen with implicit structs. I tried testing what would happen if I insert implicit structs into an array with an incrementor, but that seems to yield some strange results as well. Didn't test with implicit arrays, but I'm assuming the results would be the same.
http://fojasaur.us/weblog/post.cfm/coldfusion-implicit-structs-are-confusing
Seems as though
arr[i++] = i increments i but arr[i++] = {test=i } does not.
Wow alan, it's like you're psychic or something. How ever did you know?
Akismet, Ben?
@Barney,
Ha ha ha ha, wow! Taken care of.