Skip to main content
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Randy Brown
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Randy Brown

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

By
Published in Comments (2)

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.

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

Reader Comments

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