Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Katie Atkinson

Handling NULL Values In ColdFusion

By Ben Nadel on
Tags: ColdFusion

There have been some NULL value discussions taking place lately on the House of Fusion CF-Talk list. Null values in ColdFusion can be very tricky. There are null values in a database, which I have discussed before, and there are NULL values as variable data. In my experience, there are no Null values in ColdFusion variable data, and in fact, if you set a variable to NULL (via some non-cf call), the variable itself gets destroyed.

Take as an example, reading in a ZIP file. To do so, you have to keep streaming in zip entries until you hit, in Java, a null value. In Java, you would do something like:

  • while (objZipEntry != null){ ... }

In ColdFusion however, putting a null value into objZipEntry will actually destroy it and remove it from its parent variable scope. So, the equivalent in ColdFusion would be:

  • // Loop while we did not hit a "null" value.
  • while (StructKeyExists( REQUEST, "ZipEntry" )){
  • // .... more code here ....
  •  
  • // Get the next zip entry (finish if null).
  • REQUEST.ZipEntry = REQUEST.ZipInputStream.GetNextEntry();
  • }

As you can see, we have set the ZipEntry variable into the REQUEST scope (not shown in the code). Then, for each while loop iteration, we get the next zip entry. Once the Java object ZipInputStream returns null, signaling the end of the zip entry streams, ColdFusion "executes" that null value by removing the ZipEntry key from the REQUEST scope.

In order to handle this well, I would suggest scoping all values that might receive null values. You can use the VARIABLES scope, but my personal choice would be REQUEST or some other custom struct.

Tweet This Great article by @BenNadel - Handling NULL Values In ColdFusion Thanks my man — you rock the party that rocks the body!



Reader Comments

The request scope is slightly dangerous since it's globally available to all objects, methods, etc within the given request. You run some chance of colliding with another variable in that scope, especially in larger applications. A local scope is much safer, this, variables, or a struct you create for this purpose.

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.