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?)

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com