structKeyExists() vs IsDefined()

New Here ,
Sep 14, 2009 Sep 14, 2009

Copy link to clipboard

Copied


On a self submitting form (like a Contact Us)...

Why is <cfif structKeyExists(form, "submitform")> preferred over <cfif IsDefined('FORM.submitform')>?

What is the advantage/benefit of structKeyExists?

Thanks

TOPICS
Getting started

Views

10.2K

Likes

Translate

Translate

Report

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

correct answers 1 Correct answer

LEGEND , Sep 14, 2009 Sep 14, 2009

isDefined() will look through a bunch of scopes looking for a variable fo that name to exist, whereas structKeyExists() just does what it's told.

As a slightly contrived example, if you defined a variable variables.form.submitform, then isDefined("form.submitform") would return true.  structKeyExists() would return false.

It's less of a consideration if you scope your variables all the time, but isDefined() can still be defeated.

Using structKeyExists() also makes more sense if you're looking at a

...

Likes

Translate

Translate
LEGEND ,
Sep 14, 2009 Sep 14, 2009

Copy link to clipboard

Copied

isDefined() will look through a bunch of scopes looking for a variable fo that name to exist, whereas structKeyExists() just does what it's told.

As a slightly contrived example, if you defined a variable variables.form.submitform, then isDefined("form.submitform") would return true.  structKeyExists() would return false.

It's less of a consideration if you scope your variables all the time, but isDefined() can still be defeated.

Using structKeyExists() also makes more sense if you're looking at a struct and wanting to know if it's got a key of a given name (which is what you're wanting to do here).

Another consideration regarding isDefined() and structKeyExists() is that I have had mixed results getting isDefined() to work with session variables.  I've seen it say "no" when it means "yes", and vice-versa.

isDefined() is an imprecise function and I do not trust it, so I do not use it.

--

Adam

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

isDefined() is an imprecise function and I do not trust it, so I do not use it.

I would disagree. In fact, I would go as far as to say, where you thought isDefined() to be imprecise, the probability was 99.999999% that the problem was in the faulty logic of your code.

The standard practice is to scope the variable that goes as parameter into the isDefined function. There can be ambiguity if you fail to scope the variable. Another consideration is that the scope itself, usually a struct, may be non-existent. IsDefined permits that. That is another possibility for ambiguity. For example, if x is undefined, then the code isDefined("x.myVar") is good code, whereas structkeyexists(x, "myVar") wont even compile.

The point is that isdefined() would work even if the x is a non-existent or deleted  struct, a broken session, or a garbage-collected object. Therefore, it is the responsibility of your code to make sure such x exists in the first place.

Likes

Translate

Translate

Report

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 ,
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

isDefined() is an imprecise function and I do not trust it, so I do not use it.

I would disagree. In fact, I would go as far as to say, where you thought isDefined() to be imprecise, the probability was 99.999999% that the problem was in the faulty logic of your code.

We were able to reproduce this in a controlled environment in which there were no other [reasonable] contributing factors.  Plus other people have experienced it as well.

That said, it could have since been fixed, as our experiences were with an earlier version of CF (6-7-ish, can't remember), and I have never revisited it.

For example, if x is undefined, then the code isDefined("x.myVar") is good code, whereas structkeyexists(x, "myVar") wont even compile.

In this case I would additionally check for the existence of x in the variables struct first:

if (structKeyExists(variables, "x") and structKeyExists(variables.x, "myVar"))

This is likely to be more performant (albeit so marginally as to not really be much of a consideration) than isDefined() as CF will short circuit the evaluation as soon as the expression is false.

Still: you're entitled to your opinion, and you make as good a case as mine.

--

Adam

Likes

Translate

Translate

Report

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 ,
Sep 18, 2009 Sep 18, 2009

Copy link to clipboard

Copied

(Shrug...)  I use StructKeyExists() because it is intuitive to me what I'm asking for:  "does this particular slot exist in this particular structure?"  It asks nothing about what the slot, if it exists, may contain.  I use this technique simply because it clearly reflects the question I am then asking.  Use what works for you.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Sep 19, 2009 Sep 19, 2009

Copy link to clipboard

Copied

Why is <cfif structKeyExists(form, "submitform")> preferred over <cfif IsDefined('FORM.submitform')>?

That's a trick question. I have never heard of any particular preference for structKeyExists(form, "submitform") over  IsDefined('FORM.submitform') in the Coldfusion development community. If anything, I would bet that the majority of developers, including those who actually make Coldfusion, prefer isDefined('FORM.submitform') instead!

The two functions mean different things, and have two separate, albeit, similar, functions. One tests for the existence of a key in a known structure. The other verifies whether a variable exists on the stack/heap. What's your preference, lemon or lime?

Likes

Translate

Translate

Report

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
New Here ,
Oct 07, 2021 Oct 07, 2021

Copy link to clipboard

Copied

LATEST

IsDefined() works well when you have nested structures especially when intermediate structures may or may not exist. Example If you need the value of x in topLevel.level2.level3.level4.x IsDefined( 'topLevel.level2.level3.level4.x' ) gets you there concisely in an easy to read manor wereas you would need to do 4 structKeyExists() - which creates an ugly if statement. 

Likes

Translate

Translate

Report

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