Select Page

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

Published: March 16, 2022

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:

	data = {};
	// 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 />" );

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.