A StructFind Use!
By Raymond Camden
Expert Author
Article Date: 2007-09-21
While in general I would not think any ColdFusion function is useless, I've never really thought much of structFind().
StructFind simply returned value of a structure's key.
To me - this seemed silly. If you wanted the value of the key - why wouldn't you just get it?
<cfset s = structNew()>
<cfset s.name = "Paris Hilton">
<cfset s.gender = "female">
<cfset s.iq = 9 * randRange(1,10) * -1>
<!--- silly! --->
<cfoutput>#structFind(s, "name")#</cfoutput>
<!--- not so silly! --->
<cfoutput>#s.iq#</cfoutput>
Now one could make the point that it would be useful in cases where you don't know the key until runtime, but then you would just use bracket notation:
<!--- still not silly! --->
<cfset key = "gender">
<cfoutput>#s[key]#</cfoutput>
But yesterday on the cfaussie listserv, Mark Mandel shared an interesting, and useful, well, use for the function.
I quote him here: I use StructFind() a lot - simply because I tend to encapsulate struct's behind getter's and setters quite regualrly.
So Inside a CFC you would find me writing:
<cfset thisValue = StructFind(getStruct(), key)>
As ColdFusion doesn't support syntax like:
<cfset thisValue = getStruct()[key]> Interesting!
Has anyone else use the function like this? (Or in some other way?)
Comments
About the Author: Raymond Camden, ray@camdenfamily.com
http://ray.camdenfamily.com
Raymond Camden is Vice President of Technology for roundpeg, Inc. A long
time ColdFusion user, Raymond has worked on numerous ColdFusion books
and is the creator of many of the most popular ColdFusion community web
sites. He is an Adobe Community Expert, user group manager, and the
proud father of three little bundles of joy.
|