Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at BFusion / BFLEX 2009 (Bloomington, Indiana) with: Simon Free
Ben Nadel at BFusion / BFLEX 2009 (Bloomington, Indiana) with: Simon Free@simonfree )

ColdFusion Can Safely Pull-Through NULL Values In Function Return Statements

By Ben Nadel on
Tags: ColdFusion

One of the most frustrating parts of ColdFusion is that it doesn't really have a solid construct for NULL or undefined values. When you store a NULL value into a variable or an object property, ColdFusion destroys that variables or object property. ColdFusion 9 introduced the isNull() operator; but, working with NULL values is still less that enjoyable. That said, one saving grace is that you can pull-through NULL returns values. Meaning, you can safely return a NULL value if it's pulled-through directly from another method call.

NOTE: I did not test this on ColdFusion 9 as I no longer have that installed anywhere. This has been tested in ColdFusion 10.

This is easier to see in code. In the following demo, I have a function that has no explicit return value which means that calling that function will result in what ColdFusion considers a NULL or undefined value. I then have another function that invokes that method as part of its own return() statement, pulling the NULL value through without an intermediary variable:

  • <cfscript>
  •  
  • public any function this_returns_null() {
  •  
  • // No actual return value - will be null / undefined.
  •  
  • }
  •  
  •  
  • public any function this_pulls_through_return_value() {
  •  
  • // Here, we are directly pulling-through the return value of the given method
  • // call as our own method's return value.
  • return( this_returns_null() );
  •  
  • }
  •  
  •  
  • public any function test_null_return() {
  •  
  • // Here, we are directly pulling-through the return value of the given method
  • // call as our own method's return value.
  • return( this_pulls_through_return_value() );
  •  
  • }
  •  
  •  
  • // ------------------------------------------------------------------------------- //
  • // ------------------------------------------------------------------------------- //
  •  
  •  
  • try {
  •  
  • // Try to invoke the method that returns a null value being pulled-through
  • // from another method that returns a null value.
  • value = test_null_return();
  •  
  • // Because we are returning null, ColdFusion will destroy the "value" variable.
  • // As such, if everything WORKS, the value will no longer exist.
  • if ( isNull( value ) ) {
  •  
  • writeOutput( "Successfully pulled-through NULL return values!" );
  •  
  • }
  •  
  • } catch ( any error ) {
  •  
  • writeOutput( "Something broke: #error.message#" );
  •  
  • }
  •  
  • </cfscript>

As you can see, I'm actually pulling the NULL value through two different functions, each of which does nothing but invoke the other function as part of its own return() statement. And, when we run the above code, we get the following page output:

Successfully pulled-through NULL return values!

This is actually really great as it means we don't have to check the existence of the return value if all we're doing is pulling it though to the calling context.

Now, let's quickly contrast that with a version of the code that stores the NULL pull-through value in an intermediary variable before returning it:

  • <cfscript>
  •  
  • public any function this_returns_null() {
  •  
  • // No actual return value - will be null / undefined.
  •  
  • }
  •  
  •  
  • public any function this_pulls_through_return_value() {
  •  
  • // Here, we are directly pulling-through the return value of the given method
  • // call as our own method's return value.
  • return( this_returns_null() );
  •  
  • }
  •  
  •  
  • public any function test_null_return() {
  •  
  • // In this case, rather than pulling the return value though as our own return
  • // value, we're going to store the return value in an intermediary variable
  • // before we return it.
  • var intermediary_return_value = this_pulls_through_return_value();
  •  
  • // Return the INTEMEDIARY value.
  • return( intermediary_return_value );
  •  
  • }
  •  
  •  
  • // ------------------------------------------------------------------------------- //
  • // ------------------------------------------------------------------------------- //
  •  
  •  
  • try {
  •  
  • // Try to invoke the method that returns a null value being pulled-through
  • // from another method that returns a null value.
  • value = test_null_return();
  •  
  • // Because we are returning null, ColdFusion will destroy the "value" variable.
  • // As such, if everything WORKS, the value will no longer exist.
  • if ( isNull( value ) ) {
  •  
  • writeOutput( "Successfully pulled-through NULL return values!" );
  •  
  • }
  •  
  • } catch ( any error ) {
  •  
  • writeOutput( "Something broke: #error.message#" );
  •  
  • }
  •  
  • </cfscript>

As you can see, this code is almost identical except for the final pull-through that is actually storing the function result in a variable that is, in turn, being returned. And, when we run this code, we get the following page output:

Something broke: Variable INTERMEDIARY_RETURN_VALUE is undefined.

As you can see, the NULL value stored into the intermediary variable destroyed the intermediary variable. And, as such, when we went to reference it in the subsequent return statement, we get an error because the variable no longer exists.

For me personally, the two biggest points of friction in ColdFusion are NULL value handling and JSON (JavaScript Object Notation) functionality. But, I am very pleased to see that I can - at the very least - pull NULL values directly through from one method's invocation to another method's return value. It gives me one less edge-case to have to think about.




Reader Comments

The behavior of "intermediary_return_value" drives me nuts. It makes working with the language, at times, so much more difficult than it should be.

Reply to this Comment

@Dan,

Much agreed! I wish NULL was just a "real value" in ColdFusion - would make lots of things easier.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
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.