What scope is this?

Hi everyone,
After reading a thread about NOT using isDefined() I am trying to eliminate it’s usage from our codebase.

Subsequently I am trying to use structKeyExists() - in places where I can’t see a “quick / convenient” way to swap it for a ternary or Elvis operator.

However I am finding some places, where I cannot work out the “scope”, to use as the struct;
In the following example - I am not sure how to test for the variable fred, I tried variables and local… and could use some help please.

<cfscript>
    public function fred() {
        fred = true;
        retVal = false;
        
        // check for the existence of fred
        if(structKeyExists(local, "fred"))
            retval = true;
            
        return retVal;
    }
    
    writeoutput(fred());
</cfscript>

Gist here

@Gavin_Baumanis why not using isDefined, as long you are using literals strings, isDefined is slightly faster than structKeyExists and for sure not slower.

structKeyExists not always can replace isDefined in case you have defined no scope and you make use of scope cascading in any form you can break your code.
your example above is changing the flow of the code a 100% in case you had before isDefined("fred").

The only way you could switch in that case to structkeyExists, would be by doing this structKeyExists(getPageContext().us(),"fred"), but then please add the comment to the commit "i changed the code so it is harder to read and much slower". The only valid option if you don’t wanna use isDefined anymore is !isNull(fred).

2 Likes

Hi @micstriit

I thought I read in the thread about NOT using isDefined() -apart from it being deprecated - was that it was slower?
That it used structKeyExists() under the hood after traversing the scopes to find the variable?

@Zackster, wrote in the original thread;

IsDefined

does some string parsing
figures out it’s an url variable
then it’s going to do a structKeyExists on url to see if customer exists.
While structKeyExists only does step 3.

Always learning…
Gavin.

1 Like

@Gavin_Baumanis

It’s not in the Local scope because there is no var keyword, which is a best practice when defining vars to make them private within functions.

Try this:

public boolean function fred() {
  var fred = true;
  return StructKeyExists(Local, "fred");
}
echo(fred());

I’ve also found that TryCF sometimes has unexpected results with scoping. Here’s a test with expected results on my laptop, but on TryCF the Variables scope is empty.

Trycf does some on-the-fly manipulation of your CF code to change references to the variables scope so it’s not uncommon to see some weirdness there. I’m not sure what the purpose is other than attempting to hide some of the implementation details of how the code is being run. When you set something into variables in TryCF, your code is modified on the fly so it doesn’t go into the real variables scope, but instead into a struct inside the argments scope. You can see this in action here:

2 Likes

It is definitely in the VARIABLES scope.
Which, despite not being in the original snippet - I was certain… I had also tested…

Good to have an answer.

Can you please share the benchmark results or whatever evidence you have for that? I am curious because elsewhere the opposite has been asserted, that StructKeyExists is faster because it doesn’t search all scopes.

1 Like