What I Learned Today - I blame Ben Nadel for this post...

What I Learned Today , ColdFusion Add comments

Okay, the title is meant to be facetious, and attention grabbing. Just so we're all on the same page. But it's not untrue.

Through Ben Nadel's fantastic blog, I learned the practice of creating and scoping a LOCAL struct in a CF function (CFMX7 and CF8), and assigning any other local variable created in the function into that struct. That way I did not need to manage multiple variables, trying to make sure they were all locally scoped. It seemed more efficient.

Well, with the migration of my VPS to CF9 (and the impending upgrade of my work's servers to CF9), I found blog posts talking about how others that had used this practice were finding oddities with their code upon upgrading. It seems that this practice was somehow colliding with the new protected "local" scope within functions in CF9. The new scope allows coders to do exactly what I was doing, only without having to scope that local struct first.

In trying to figure out what changes I needed to make, I went asking around trying to find out if this meant that we no longer had to scope local variables anymore inside of functions, if unscoped variables would automatically get put in the new local scope. To hear and read the documentation, you certainly could come to that conclusion.

Thanks to Ray Camden, I have been straightened out. It turns out this is not the case, and I am not the first to ask him this. He was kind enough to make this topic an entry in his just as fantastic blog.

Bottom line, you don't have to scope things put in the local scope, but you must explicity call the variable in the local scope (local.myVariable). And all other variables must still be scoped for protection.

7 responses to “What I Learned Today - I blame Ben Nadel for this post...”

  1. Ben Nadel Says:
    As far as I have seen, the only issue that CF9 can cause is if you actually set the LOCAL scope to a non-empty struct value:

    <cfset var local = { foo: "bar" } />

    I believe CF9 will actually skip this line. Other than this style, you should be fine to proceed exactly as before.
  2. Frank Wheatley Says:
    We use the local structure as well. We are only having issue when the entire structure is returned (i.e. cfreturn local).
  3. Ben Nadel Says:
    @Frank,

    Really? What happens when you try to return the LOCAL scope?
  4. Frank Wheatley Says:
    It may only happen when we call methods from webservices. I have not spent a lot of time on it, though. I can replace the var 'local' with 'xxx' and it is fine. We delayed our CF upgrade because of this.

    I also have problems because I extended the ExtJS library and CF9 upgraded ExtJS. Now some of my cfgrid enhancements are not working.

    <cffunction name="xxxx" access="remote" returntype="struct">

    AxisFault
    faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
  5. Ben Nadel Says:
    @Frank,

    Hmm. Perhaps it's having trouble seeing the local scope as a Struct or something? Very interesting.
  6. DeepDown Says:
    As Ben mentions:

    "As far as I have seen, the only issue that CF9 can cause is if you actually set the LOCAL scope to a non-empty struct value:

    <cfset var local = { foo: "bar" } />"

    I can confirm this:
    http://dl.dropbox.com/u/305923/CF9-local-scope-trouble.zip

    This issue is really annoying in CF9, since it breaks a lot of the code I've written in the last 1,5 years, that depends on setting and initialising the local scope in one statement.
  7. tsuumhbqvwx Says:
    Fbu7MM <a href="http://qgfujnoydaxi.com/">qgfujnoydaxi</a>;, [url=http://njbpszywdbzd.com/]njbpszywdbzd[/url], [link=http://askvafcnyvrp.com/]askvafcnyvrp[/link], http://rmdrybwbnnpk.com/

Leave a Reply

Leave this field empty:

Powered by Mango Blog. Design and Icons by N.Design Studio