Skip to main content
Ben Nadel at RIA Unleashed (Nov. 2009) with: Chris Lee and Geoff Bergey
Ben Nadel at RIA Unleashed (Nov. 2009) with: Chris Lee ( @iamchrislee ) Geoff Bergey ( @bigtallgeoff )

Safe Navigation Operator Works In Comparison Expression Even When NULL In Lucee CFML

By on

While there is a version of the Lucee CFML syntax that treats null as a first-class citizen, null has, historically, been a tricky value to work with in the ColdFusion world. As such, my instinct is always to treat a potentially null value with kid-gloves. That said, I discovered this morning that the Safe Navigation operator will work properly in a comparison expression, even if the evaluation of the operand results in a null value in Lucee CFML

Having a fuzzy understanding of null values in ColdFusion is, in my mind, completely justifiable. Take, for example, the following code:


	nully = javaCast( "null", "" );

	dump( nully == 3 );

In this case, we're creating a null value and then attempting to use it in a comparison expression. And, when we run the above CFML code in Lucee (as well as other ColdFusion engines and version), we get the following output:

Lucee CFML throwing an error due a null value reference.

As you can see, Lucee CFML is not happy when we explicitly try to reference a null value. However, what if we bypass the nully variable reference and try to use the null value directly in the comparison:


	dump( javaCast( "null", "" ) == 3 );

In theory, the above code should function exactly the same since we are doing nothing but removing an intermediary reference. However, if we run the above ColdFusion code in Lucee CFML, we get the following output:


In this case, instead of receiving an null-reference error, we were able to use the null value in the comparison expression without error. And, in fact, we can even compare two null values:


	dump( javaCast( "null", "" ) == javaCast( "null", "" ) );

... which gives us the output (in Lucee CFML - I didn't test other engines):


As you can see, handling null in ColdFusion isn't always straightforward. Which is why my instincts for it aren't always on-point. That said, getting to the point of this post, I realize now that this is why we can use the safe-navigation operator in a comparison expression:


	dump( variables?.nully == 3 );

Running this code in Lucee CFML gives us the following output:


As you can see, even if the evaluation of the safe-navigation expression results in a null value, we can still successfully use it in a comparison expression.

What I am finally beginning to understand is that null values aren't a problem in ColdFusion - it's null references that are problematic (or at least require more involved handling). Which aligns with my earlier discovery that ColdFusion can safely pull-through null values in a Function return statement. The return statement works, again, because it is consuming the null value directly rather than a reference to it.

It is always a little bit embarrassing when you've worked with a language for years and discover that you still don't have a rock-solid mental model for basic type-handling. But, I think I finally see how null values work in ColdFusion. And, it's better late than never.

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

Reader Comments


It is always a little bit embarrassing when you've worked with a language for years and discover that you still don't have a rock-solid mental model for basic type-handling.

It's even more embarrassing when you read an article like this and you've got Arsenio Hall in your head saying, "Things that make you go hmmmm" ;)



So, in the above post, I used the safe navigation operator in a way that I didn't even realize was surprising at the time. In the example where I did this:

dump( variables?.nully == 3 );

... that actually didn't jive with my mental model, but I didn't see it when I wrote this code. I've always thought that the safe navigation operator applied to the left operand, which would be variables in this case. However, the "null" value here is actually the right operand. In retrospect, I should have been surprised that the above code didn't throw an error.

I wrote up my realization in a new post:

The safe navigation operator appears to work the same in both Adobe ColdFusion and Lucee CFML.


On a related "value comparison" note, I just discovered that the compare() and compareNoCase() built-in functions work well with NULL values as well:

This means we can use the safe-navigation operator to pass values into the compare() function without having to worry about adding syntactic noise in order to make sure that values are defaults:

compare( cookie?.optionalKey, "someKnownValue" );

I love being able to remove unnecessary syntax 💪

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