Skip to main content
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Michael Offner-Streit and Tanja Stadelmann and Gert Franz and Pierre-Olivier Chassay and Paul Klinkenberg and Marcos Placona
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Michael Offner-Streit Tanja Stadelmann Gert Franz Pierre-Olivier Chassay Paul Klinkenberg Marcos Placona

Structs Can Use The Empty-String As A Valid Key In ColdFusion

By
Published in Comments (2)

The other day, I accidentally stumbled upon an awesome feature in ColdFusion: You can use the empty-string as a valid key within a Struct. On its own, this feature may not seem that great. But, when you do a lot of data manipulation, having a struct that can map the empty string onto a value usually leads to much more uniform access in your data transformations.

NOTE: This also works in JavaScript - you can have a hash with an empty-string key.

It's hard for me to come up with a simple example that demonstrates why this is useful since I've been using it in rather complex algorithms. But, imagine that you needed to duplicate a large structure of nested data with various cross-entity relationships. During this duplication process, foreign keys in the old structure need to map properly to new foreign keys in the new structure. If some of the foreign keys are NULL, they will come back as empty-strings in the SQL (since ColdFusion's understanding of NULL is not great). Now, rather than adding logic to your algorithm that conditionally checks for foreign key validity, you can simply add an empty-string entry to your data-mapping and now you can seamlessly reference your map regardless of whether or not a foreign key is present.

What I said probably makes no sense without some code. So, let me give you the lamest example:

<cfscript>

	// The empty string is a valid key in the struct! Playa!
	idMap = {
		"" = 0,
		"0" = 0
	};

	// As a trite example, I am using an invalid key in the CGI object as the lookup
	// value in the idMap. If you access a key, on the CGI, that doesn't exist, an
	// empty string will be returned.
	writeOutput( "Mapped value: " & idMap[ cgi.invalid_key ] & "<br />" );
	writeOutput( "Mapped value: " & idMap[ 0 ] & "<br />" );

</cfscript>

As you can see, I have a struct that maps the empty-string to zero. I'm then using that struct to map some value out of the CGI object (which doesn't exist). Since the CGI key is invalid, it will return an empty string (the way SQL will return an empty string for NULL values in ColdFusion). The empty string is then mapped, via our idMap, to zero. And, when we run the above code, we get the following output:

Mapped value: 0
Mapped value: 0

Ok, you might be thinking I'm crazy to be excited about this feature. But, trust me - when you do a lot of data transformations, every bit of conditional logic that you can remove from the code makes the code much easier to read and maintain. And, if you can use the empty-string to create more uniform data handling, it's a surprisingly satisfying "win!"

Want to use code from this post? Check out the license.

Reader Comments

290 Comments

For your blog followers who missed it on Twitter:

.@BenNadel That's so #zen. If emptiness holds emptiness too, it even makes a face:

<cfset Variables.Zen = {"" = ""}>

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel