Null support in ColdFusion 2018

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

corePool=runtimeObj.getRuntimeProperty(“corePoolSize”);

writeOutput(“core pool size is: ” & corePool & “<br/>”);

maxPool=runtimeObj.getRuntimeProperty(“maxPoolSize”);

writeOutput(“max pool size is: ” & maxPool & “<br/>”);

keepAlive=runtimeObj.getruntimeProperty(“keepAliveTime”);

writeOutput(“keep alive time is: ” & keepAlive & “<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.

Leave a reply

Related