TerrenceRyan.com

I'm a 35 year old redhead geek from Philly.
I'm currently a Developer Evangelist for Adobe.
Also the author of Driving Technical Change

Var Scoping ColdFusion Tag-Specific Variables

6 Comments

I came across this issue during a code review, and thought it might be of note. Specifically, use of cfstoredproc. StatusCode inspired this.

You know that rule about var scoping all variables called in a function unless they are specifically needed elsewhere? Well don't forget to do it to most of the "ColdFusion tag-specific variables." They are:

Anything that returns a simple value (string, numeric, Boolean, numeric) or an even arrays and queries can be var scoped at the head of the function with an empty string. (Although for every type but query I like to initialize it with the correct data type.) Structs however have to be initialized with a StructNew().

I omit from the list:

CFcatch only exists during the execution of a cfcatch block and is accessible only by code in that block. In any case, my normal test for variable scope leakage doesn't detect it, so I'm assuming it's a special case. If anyone knows different please let me know. Likewise CFerror only exists in page referenced by a cferror tag, which probably couldn't be in CFC call. (But I could be wrong.)

6 responses so far ↓

  • 1 Ben Nadel

    I could be way off here, but I could swear that I remember trying to "var" scope the CFFILE results:

    <cfset var CFFILE = "" />

    and it gave me some sort of error about dereferencing scalar objects as though they were structs with properties.

    Oh, and that makes me thinkg of something else... doesn't ColdFusion create backwards compatable structures? I think for a CFFILE tag it creates both a CFFILE and a FILE structure. Do both need to be var'd? Hmmmm.

    Perhaps the best thing to do would be to use the "result" attribute for any tag that can take it and var that.

    I haven't tested any of that though, so forgive me if I am misleading.
  • 2 Terrence Ryan

    I touched on this a bit in the post Ben, but I didn't make it very clear. Structs needs to be initialized as structs. So it should be : <cfset cffile = StructNew() />. That would prevent the error.
  • 3 Ben Nadel

    Terrence,

    Sorry, I think when I first read it, I didn't quite understand what you were saying. I see. I read it too fast.

    Any thoughts on FILE vs. CFFILE?

    Excellent post by the way.
  • 4 Ben Forta

    And don't forget that as of CFMX7 you can use RESULT to rename many of those hard coded return structures.

    --- Ben
  • 5 Terrence Ryan

    Ben Nadel: Can you give me an example of this file and cffile thing? I can't seem to reproduce it.

  • 6 bethb

    Also, "CFLOOP" and "CFSAVECONTENT"

Leave a Comment









Categories

Monthly Archives

Tag Cloud

coldfusion web development flex coldfusion builder appearances squidhead coldfusion builder extensions higher ed html5 flash builder air mobile android driving technical change adobe apptacular running a coldfusion shop adobemax06 movable type flash catalyst flash css blackberry adobemax07 adobemax08 hero finicky adobemax09 holy crap i’m a mobile developer centaur phonegap basecamp cfc whichelement unfuddle motorola metablog irrational characters ios git evangelism edge devices code reviews ant wharton subversion security rhino philly philadelphia multidevice knowledge@wharton jobs javascript java browserlab adobemax10 adobe tv unfuddlecfc svnauth.cfc semantic html semantic html responsive web design qnx nlb linux jquery mobile it github flexorg fireworks eclipse dreamweaver apps apple adobemax11