Using StructFind for fun and profit

There is a very weird debate going on in the CF-Aussie list about the merits of the ColdFusion function structFind(). StructFind( struct, key ), is an aptly-named native function that fetches the value of the specified key, in the specified struct. If, for some reason, the key does not exist, it throws an error. (The docs are wrong)

Why would you ever need this function? One use case would be returning the value a key in a structure that is the result of a function evaluation. Take this example:

view plain print about
1<cffunction name="loadData" output="false" access="public" returntype="struct" hint="I load a dataset">
2 <cfargument name="object" type="string" required="true" />
3 <cfargument name="key" type="string" required="true" />
4 <cfargument name="id" type="string" required="true" />
6 <cfreturn queryRowToStruct( findRow( arguments.key,, do( structFind( getDataMap(), arguments.object ) ) ) ) />
7 </cffunction>

See the structFind call in the middle? It is operating on a structure returned from getDataMap() and the key as passed in the argument named 'object'. ColdFusion doesn't permit getDataMap()[arguments.object] syntax, so without needlessly breaking up this method, structFind is the Way.

It is important to note structFind will throw an exception if the specified key does not exist in the structure. This is exactly what I want it to do. If for some reason the key passed does not exist in the structure, failing fast is the only option.

I believe the ColdFusion language should support additional dynamic syntax such as:

view plain print about
1structFun()[ keyname ]
view plain print about
1Component["stringPartOfMethod" & variableContainingOtherPartOfMethod](args)
As a matter of fact, I personally made the request on the CF8 wishlist (see # 49) for just such a thing. Until my wishes have been granted, give me structFind or give me death!

There are no comments for this entry.

Add Comment Subscribe to Comments