HOME | FERGUSON Digital Blog

StructKeyExists vs IsDefined

ColdFusion Add comments

I recently took over a corporate project from a team of ColdFusion Hobbyists. They had been performing code baby-sitting for the last couple of years and it was evident in the source code for their ColdFusion project. As you grow and learn more, your knowledge expands, you have more experiences, your perceptions change, and you receive insight or wisdom. Go back and examine old code. The longer a project sits in production, the more it requires updating. From day to day, especially across different versions of ColdFusion, there are bound to become better ways of performing actions faster, more secure, less tedious to manage, etc.).

Here is a problem that was inherited, the gratuitous over-use of IsDefined. I know, it's an old argument, but hold on tight because here we go again, yes StructKeyExists versus IsDefined. Simply put, StructKeyExists tests for a specific variable defined in a specific scope. IsDefined, without scope definition, will check for a specific variable as it searches through many different scopes. Here is the order of precedence IsDefined will take during the search:

  • VARIABLES
  • CfQuery
  • CGI
  • CfFile
  • URL
  • FORM
  • COOKIE
  • CLIENT

When you know exactly what scope you are testing for, StructKeyExists will always perform faster, except when using IsDefined for searching in the VARIABLES scope.

<cfset FORM.Test="ABC">
<cfset URL.Test="DEF">
<cfset VARIABLES.Test="GHI">
<cfif IsDefined("Test")>
   <cfdump var="#Test#">
</cfif>


The results will return the letters GHI, not because it was the last variable in the last scope defined, but because the VARIABLES scope is at the top of the list of precedence. If we had been testing for the existence of FORM.Test then we should have used IsDefined("FORM.Test"), but IsDefined will test its value against the other scopes anyway.

<cfif IsDefined("UserScope.Test")>
   Test 1
</cfif>
<cfif StructKeyExists(UserScope, "Test")>
   Test 2
</cfif>


This example will throw an error that Variable USERSCOPE is undefined for the StructKeyExists line because the scope is undefined in the search and IsDefined checked to see if it was VARIABLES.UserScope.Test and so on through the list. Even though you define the left side of FORM.Test as the scope, IsDefined will assume it to be a variable anyway if it can't find it in any scope. StructKeyExists fails in this example because the scope UserScope is undefined, which means the only scope it checked for is the one defined.

When the variable names become varied throughout a project, and it becomes difficult to always keep track of those used in one specific scope or the other, it would be easier for the programmer and faster for the page to execute to utilize StructKeyExists over IsDefined.

If you find this post useful please leave a comment and let me know how you used the information.

0 responses to “StructKeyExists vs IsDefined”

Leave a Reply



© Copyright 1997-2024, All Rights Reserved Coldfusion and MS SQL2008
Powered by Mango Blog.   Design by FERGUSON Digital
o:Event name="beforeHtmlBodyEnd" />