Skip to main content
Ben Nadel at the jQuery Conference 2010 (Boston, MA) with: Ralph Whitbeck
Ben Nadel at the jQuery Conference 2010 (Boston, MA) with: Ralph Whitbeck

compare() And compareNoCase() Can Accept NULL Values In ColdFusion

By
Published in

As the ColdFusion language continues to add new operators and new functionality, we have to make sure that we continue to evolve our understand of the runtime behaviors so that we can reduce syntactic noise and make our code more expressive. The steps on this journey are not always intuitive. It requires trial-and-error and whole lot of "I wonder if this will break?" experiments. Such is the way that I discovered that the compare() and compareNoCase() built-in functions can accept Null values as arguments in both Adobe ColdFusion and Lucee CFML.

In my earlier post on serving a bypassable Maintenance page in ColdFusion, I was checking an optional cookie value to see if the maintenance page should be skipped (thereby allowing access to the underlying application). In my first iteration, I was using the null-coalescing operator (aka, Elvis operator) to default the cookie value.

But then, I remembered that the safe navigation operator works with equality comparisons (==) in ColdFusion. Which made me think it might work with the compare() function as well. And, kabalmo! It did! In fact, both ColdFusion runtimes seem to show the same behavior.

Let's look at a quick demo in which I use the safe-navigation operator to pass null values into the compare() function:

<cfscript>

	data = {};

	echoVersion();
	// Normally we think of the compare() and compareNoCase() functions as testing String
	// values. But, it seems that these functions appear to play nicely with NULL values
	// as well. Which means, we can use them with the SAFE-NAVIGATION operator and not
	// have to worry about performing NULL coalescing to avoid errors!
	echoValue( compare( data?.foo, "bar" ) );
	echoValue( compare( "foo", data?.bar ) );
	echoValue( compare( data?.foo, data?.bar ) );
	echoValue( compare( javaCast( "null", "" ), javaCast( "null", "" ) ) );

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	// I output the current ColdFusion product version.
	public void function echoVersion() {

		var version = ( server.keyExists( "lucee" ) )
			? "Lucee CFML #server.lucee.version#"
			: "Adobe ColdFusion #server.coldfusion.productVersion#"
		;

		writeOutput( version & "<br /><br />" );

	}


	// I output the given value on its own line.
	public void function echoValue( required any value ) {

		writeOutput( value & "<br />" );

	}

</cfscript>

As you can see, I'm passing-in various combinations of NULL and not-NULL values into the compare() function and I'm writing the results to the screen. And, when we run this in both Adobe ColdFusion 2021 and Lucee CFML 5.3.8, we get the following output:

Both Adobe ColdFusion and Lucee CFML showing the same results for using compare() function with null values.

Hot dog! Both Adobe ColdFusion and Lucee CFML allow NULL values to be passed into the compare() function. And, both runtimes show the same results. This should help reduce the noise we might otherwise have in our code when working with optional values.

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

Reader Comments

Post A Comment — I'd Love To Hear From You!

Post a Comment

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