July 12, 2018
Null support in ColdFusion 2018
Comments
(2)
July 12, 2018
Null support in ColdFusion 2018
Newbie 4 posts
Followers: 7 people
(2)

Null is an important construct in modern programming languages, which improves language interoperability with other tech stack and programming languages.

Before the 2018 release of ColdFusion, a database null or a null returned from an external service was getting converted to empty string. Therefore, if you were to code for null or take decisions based on null value in your ColdFusion applications, there was no inherent way to achieve this.

Imagine a scenario where you want to have an empty string as a column value in a database table. Since null is also converted as empty string, there was no way to distinguish a null from an empty string. This behavior also affected json serialization and deserialization.

Given an actual need for null-based implementation, we decided to introduce null as part of language changes in ColdFusion (2018 release).

Now in your ColdFusion applications, you can assign null to a variable, as shown below:

<cfset price = null>

You can also use the function isNull to check if a value is null.

<cfdump var=”# isNull(price)#”> //prints yes

<cfif price == null> // resolves to true

<cfif price eq null> // resolves to true

Null settings at server level

In the ColdFusion Administrator, click Server Settings > Settings. In the page, you there is the option Enable Null Support. If you enable this option, all your applications running in the server becomes null-enabled.

You can also update  this setting through an Admin API.

<cfscript>

// Login is always required.

adminObj = createObject(“component”,”cfide.adminapi.administrator”);

adminObj.login(“admin”);

runtimeObj=createObject(“component”,”cfide.adminapi.runtime”);

//get enableNullSupport flag value using admin api

isNullEnabled = runtimeObj.getRuntimeProperty(“enableNullSupport”);

writeOutput(“Is Null Enabled  : ” & isNullEnabled & “<br/>”);

//set enableNullSupport flag value through admin api

isNullEnabled =runtimeObj.setRuntimeProperty(“enableNullSupport“,true);

writeOutput(“Is Null Enabled  : ” & isNullEnabled & “<br/>”);

</cfscript>

Null settings at application level

To be able to support legacy applications, we have also null as an application-level setting. This means that your server level setting could be OFF and you can have null turned ON only for selected applications. You can do this by specifying below in your Application.cfc –

this.enableNullSupport = true

The behavior of isNull, isDefined functions changes when null Support is enabled.

The isNull and isDefined functions return true for a variable with null value. Previously, the output of isNull function was a reverse of the output of isDefined function, but now it checks for a variable with value null.

Note: Null is a reserved keyword in ColdFusion. You will not be allowed to create a variable named “null” in your application.

2 Comments
2021-06-05 13:42:14
2021-06-05 13:42:14

This behavior also affected json serialization and deserialization.

I don’t see any explanation for how this new functionality affects JSON serialization and deserialization. The explanation regarding empty string hasn’t affected JSON since ColdFusion 10.

Like
2018-12-03 02:03:35
2018-12-03 02:03:35

Hi Vijay,

I believe the Admin API example needs changed? The topic is Null support in CF2018. But, the Admin API example is for retrieving corePoolSize, maxPoolSize and keepAliveTime.

Thanks!,
-Aaron

Like
Add Comment