Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

CFC's - VAR scoped or LOCAL scoped?

Engaged ,
Dec 23, 2008 Dec 23, 2008
Ok, so I always thought it was good practice to VAR scope your variables at the top of a CFC method so that it is thread safe. Fair enough. However, I have just discovered today that you can do this:

var LOCAL = StructNew();

// These are now all local.
LOCAL.Foo = 1;
LOCAL.Bar = QueryNew( "id" );
LOCAL.Test = StructNew();

Now, this looks like a REALLY nice method of doing it and I had never seen this before. I have written thousands of lines of code. Would it be worth changing it to this new method or shall I leave it as it is?

Will future CF versions have a problem with this method?

Thanks,
Mikey.
432
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 23, 2008 Dec 23, 2008
Kapitaine wrote:
>
> Now, this looks like a REALLY nice method of doing it and I had never seen
> this before. I have written thousands of lines of code. Would it be worth
> changing it to this new method or shall I leave it as it is?

I wouldn't do that, but if you like it it is all right to start using it
into the future.

>
> Will future CF versions have a problem with this method?

Nope it is not a method. Local is just a var scoped structure. You can
put a anything you like into a structure.


The thing to remember, accorind to something I have read, is that in
version 9, all unscoped variables in a function will automatically be
var scoped and local. Thus saving us from the gyrations. But this will
not break this type of code, just make it unnecessary.


It is imporant to not there is nothing special about the name local. I.E.

var ian = structNew()

//These are now all local, since there are just keys added to the local
'ian' structure.
ian.Foo = 1;
ian.bar = queryNew("id");
ian.test = structNew();



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 23, 2008 Dec 23, 2008
What is the perceived advantage of creating a local structure and plopping all your local variables into it? To me it looks like extra work because you then have to type the structure name whenever you use the variable.

By the way, using the word var to keep your variables local to the function is equally important for functions written outside of cfc's.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 23, 2008 Dec 23, 2008
Dan Bracuk wrote:
> What is the perceived advantage of creating a local structure and plopping all
> your local variables into it? To me it looks like extra work because you then
> have to type the structure name whenever you use the variable.

I'm with you Dan. I've never felt this was much of a problem that
needed solving. But the world takes all kinds to make it go around, so
if this makes one feel good, I also see no harm in it.

The one *tiny* benefit, as I understand it, is that if you are
consistent in appending all your variables to the 'local' or 'ian' or
whatever structure you don't over look a variable and forget to var
scope it. This could, possible, I guess, be beneficial for simple,
throw away variables like loop counters and such.

Personally I like var scoping all my variables at the beginning of the
function. It is a convenient exercise that also reminds me to
initialize any that need initialization and to document the purpose of
each one.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 23, 2008 Dec 23, 2008
Actually, another advantage of the LOCAL structure method I suppose is that, should you remove references in the method to variables that were once var scoped, then there's no need to remove the var scope variable at the top. Sometimes in methods that are very long and need reworking, I end up with redundant locally var scoped variables at the top. While this wouldn't pose a problem with the code running, I suppose it would be confusing for someone else viewing it or viewing it later yourself only to find they are redundant and no longer needed.

Quite a good advantage actually! Cheers guys.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 24, 2008 Dec 24, 2008
LATEST
Would it be worth changing it to this new method or shall I leave it as it is?
Depends what you mean. Varring the variables foo, bar, test individually is effectively equivalent to varring the structure LOCAL, as you have done. It simply tells Coldfusion to clear up such a variable or structure when the method returns.

Will future CF versions have a problem with this method?
No.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources