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 18.104.22.168.
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 22.214.171.124 (as well as other ColdFusion engines and version), we get the following output:
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:
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
<cfscript> dump( javaCast( "null", "" ) == javaCast( "null", "" ) ); </cfscript>
... which gives us the output (in Lucee CFML 126.96.36.199 - 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:
<cfscript> dump( variables?.nully == 3 ); </cfscript>
Running this code in Lucee CFML 188.8.131.52 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.
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" ;)
A video reference to my last comment. I should have included it in
the original post. Sorry! I would have edited if I could, but here it is...
To be fair, you could have just as easily been referencing C & C Music Factory :D
Forgot about that one, but oh yeah! Even better