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?)
Archived Comments
I have never used StructFind(). However, the other day on the CFInsider, I requested the ability to use *just* that syntax:
getStruct()[key]
Got my fingers crossed for the fugure.
Yes, I use it all the time. It makes for nicer, tighter syntax.
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
Hmm. StructFind(getStruct(),key) seems inherently bad to me. I don't object to StructFind(), but to getStruct(). If you have to call a function to get at the structure, shouldn't you be checking to make sure that you do indeed get back that structure?
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.
"As ColdFusion doesn't support syntax like:
<cfset thisValue = getStruct()[key]> "
But it does support syntax like:
<cfset thisValue = getStruct().key>
@Rick - it seems bloatingly paranoid to check the type you are expecting every time. And if the wrong key was passed, would you prefer to quietly move along and give an unknown incorrect result, or blow up so everyone knows it's wrong?
@Aaron - But then you can't be getting they key dynamically, like: <cfset theKey = "name"> #person[name]#
Err... that should have been #person[theKey]#
@Ray - Thanks for the comment ;o) Including my spelling mistakes and all ;o)
@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.
is the variable name 's' in your example for struct, or something else? =D
If you are doing this a lot:
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
I really must think before I post, I'm always double posting!
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
I was writing another comment and it got way too long.
For the record, I am pro-structFind.
http://www.nodans.com/index...
Dan Wilson
I just tried running this in ColdFusion 9:
<cffunction name="getStruct" access="public" output="false" returnType="Struct">
<cfreturn {foo = 1, bar = 2} />
</cffunction>
<cfoutput>#getStruct()["foo"]#</cfoutput>
and it worked, so it must have been fixed during the 4 years since this post was written.
Thanks for sharing that Jesse.