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

I'm a This-Scoping Fool

4 Comments

Hey, ColdFusion experts! Remember when you said "Just don't use the This scope in CFC's."?
And then I said "No, I can totally use it."
And then you said "No, you totally can't"
And then I was all like "I totally can!"
Yeah?
Well I was totally wrong.

(In case your wondering this conversation happened entirely in my head.)

We were having a weird issue with one of applications. It uses cookie based authentication that was written centrally into a cfc, and every once in awhile users were getting someone else information. We couldn't track it down. The CFC was all properly var scoped. However it turns out that it was using the This scope to return the results of each function. The CFC was instantiated in the application scope of the calling application. So things like this were happening:

This is dumb stuff from me. At the time I wrote it, like a year and a half ago, I assumed no one would instantiate the CFC to the application scope. But now time has proven me wrong. It just goes to show, that no matter how small your application is, or how narrowly you think it is going to be used, always follow best practices, unless you have a specific reason not to do so. If I had just set the proper response to be returned from the function instead of in the this scope, I wouldn't be rewriting code, and trying not to break existing calls to this central CFC in 20 or so applications by 10 or so different developers.

4 responses so far ↓

  • 1 Barney

    This actually has nothing to do with the 'this' scope, but rather just general thread safety issues. You'd run into the same problem if you used the 'variables' scope. The reason for not using the 'this' scope is that it's publicly readable and writable outside the CFC, wheras the 'variables' scope is not.

    When you use shared-scope objects, you must ensure that the only instance-level state is request-agnostic. If that's impossible, you need to use a request-aware scope to house the data a la a java.lang.ThreadLocal. Fortunately (or unfortunately) CF has that built-in with the request scope, which you can use from anywhere. Just be aware of what you're doing and clearly document the dependancy.
  • 2 Terrence Ryan

    Well I chalk it up to the this scope as I had var scoped all of the other variables, and thought I had made it thread safe. The bigger point there might be "There's more to thread safe CFC's then just the var scope."

    In any case it was horribly architected all around.
  • 3 Tony Petruzzi

    this (pun intended) is why I can't stand applications that throw their CFC into the application scope. Everyone thinks that you get a performance gain by doing it, I have never seen it. Start using good old cfinvoke. I can almost bet that you could of kept your CFC the way it was, by using cfinvoke and it would of worked just fine.
  • 4 Terrence Ryan

    I actually stand by CFC's in the application scope. You just have to write them correctly.

    But you are right about the cfinvoke for this particular CFC. That was the way it was *intended* to be used. However once you start sharing code, how you intend to use it, and how it is used aren't the same. That's when proper coding can save you a bunch of headaches.

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