Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: RichardCooper
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: RichardCooper ( @seopo )

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

By Ben Nadel on
Tags: ColdFusion

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 5.3.3.62.

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

<cfscript>

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

	dump( nully == 3 );
	
</cfscript>

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 5.3.3.62 (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:

<cfscript>

	dump( javaCast( "null", "" ) == 3 );
	
</cfscript>

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:

false

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:

<cfscript>

	dump( javaCast( "null", "" ) == javaCast( "null", "" ) );
	
</cfscript>

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

true

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:

<cfscript>

	dump( variables?.nully == 3 );
	
</cfscript>

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

false

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.



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" ;)

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
NEW: Some basic markdown formatting is now supported: bold, italic, blockquotes, lists, fenced code-blocks. Read more about markdown syntax »
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.