A use for structFind!
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
As a matter of fact, Mark showed this technique to me in //dalnet/#Transfer about 6 months ago when I was complaining about ColdFusion VS <cfset thisValue = getStruct()[key]>
DW
I know it's more elegant than using a temp variable and checking the result ... but it's also lazy. If there's no chance that the function will fail and not return the struct and there's also no chance that the key doesn't exist in the structure, that's one thing, but it still encourages bad practices that the n00b who maintains the code after you might not catch. And what if getStruct() is expensive? It's all too tempting to do something like:
<cfif StructKeyExists(getStruct(),key)>
<cfreturn StructFind(getStruct(),key)>
</cfif>
Instead of doing it the longer but safer way:
<cfset var theStruct=getStruct()>
<cfif IsStruct(theStruct) AND StructKeyExists(theStruct,key)>
<cfreturn theStruct[key]>
</cfif>
Maybe I'm just being curmudgeonly, but it just rubs me the wrong way.
<cfset thisValue = getStruct()[key]> "
But it does support syntax like:
<cfset thisValue = getStruct().key>
@Aaron - But then you can't be getting they key dynamically, like: <cfset theKey = "name"> #person[name]#
@Rick - well, if you're getter has a return type of 'struct', then yes, you know it is a struct.
If it wasn't a struct, it would produce a run time error, which you would want when doing unit testing.
That's also the whole point of having encapsulated getters and setters - you know what comes in and what goes out.
Doing that sort of double handling on variable unnecessary seems like a lot of work for not very much payoff, I'm afraid to say.
myValue = StructFind(myFunction(), myKey);
Surely it would be better to have a function that returns just the single key? Seems like a lot of wasted data transfer seeing as you can't do anything else with the rest of the struct after that code.
Dom
If you wanted a getter for a struct that you only wanted to get single keys for, it might be better to do:
<cffunction name="getStruct">
<cfargument name="key"/>
<cfreturn variables.myStruct[arguments.key]/>
<cffunction>
Then
<cfset myValue = getStruct(myKey)>
My tuppence
For the record, I am pro-structFind.
http://www.nodans.com/index.cfm/2007/9/21/Using-St...
Dan Wilson

getStruct()[key]
Got my fingers crossed for the fugure.