If I use ColdFusion, I don't have to worry about data types, right?

This post is more than 2 years old.

One of the nicer things about ColdFusion is that it takes a simple approach to variable types. When it comes to the simple types (non-array, structs, etc), you don't have to worry about specifying the type of data ahead of time. So for example, this code runs just fine:

<!--- Look, a number! ---> <cfset x = 9> <!--- oh oh, now its a string ---> <cfset x = "foo"> <!--- holy crap, now its a date even! ---> <cfset x = now()>

While I wouldn't recommend writing code like that, it certainly will run just fine in ColdFusion. It's actually kind of handy when it comes to 'temp' variable, like loop iterators and the such.

So with ColdFusion being loose in terms of variable types, it is easy to forget that most of the rest of the programming world isn't so easy. Every now and then that come bite you in butt. Here is a great example. I recently fixed a bug in Seeker involving a hand-made query. The query was created using queryNew and had supplied the optional list of column types to tell ColdFusion what each column represented data wise. Unfortunately, after this change, the Score column began returning 0 for every match? Why? The values were all between one and zero while the column type was defined as integer. Consider this code block:

<cfset numbers = [0.314159,0.921,0.000001,9.9]>

<cfset q = queryNew("col1,col2","integer,decimal")>

<cfloop index="n" array="#numbers#"> <cfset queryAddRow(q)> <cfset querySetCell(q, "col1", n)> <cfset querySetCell(q, "col2", n)> </cfloop>

<cfdump var="#q#">

I've got 3 numbers, all with decimals, and I copy them into a query where the first column is defined as integer and the second as decimal. When dumped, check out the first column totally changes the values:

In the same week this happened, another user wrote in with a similar issue. She was inserting phone numbers into a query and had not specified column types. When you do that, ColdFusion makes a guess to the right column type. In her case, ColdFusion guessed wrong and treated phone numbers like numbers. There were some numbers with a 0 in front and they ended up having the 0 removed. Supplying the varchar type definition for the column took care of it.

Oh, and how about a third example? Not exactly ColdFusion related, but while working on GameOne, I accidentally set the column type for Funds to a type that users easily reached. Ugh. Sometimes it takes a few hits to get it through the noggin, eh?

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by LuKeNuKuM posted on 4/28/2009 at 9:36 PM

thanks for the article... side note: think there is a typo on ya gameone link Ray!

Comment 2 by Phillip Senn posted on 4/28/2009 at 9:39 PM

<cfset I = 1>
<cfset I.Name = "Phillip Senn">
You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

Comment 3 by andy matthews posted on 4/28/2009 at 9:41 PM

One thing to consider is that even ColdFusion doesn't allow conversion between some datatypes. For example, when var-ing inside a CFC you can't var a struct to a string, then later change it to a Struct or CF will throw an error. Same with a query var.

Comment 4 by Raymond Camden posted on 4/28/2009 at 9:44 PM

@PS, AM: Yep, I specifically said the simple types. If that isn't clear, let me know.

@Luke: Fixed, thanks.

Comment 5 by shag posted on 4/28/2009 at 9:45 PM

You always say math isn't one of your strong points. Those decimals are all greater than 0, but they are less than 1. They would have to be negative to be less than 0.

Sorry... I couldn't resist. ;-)

Great point.

Comment 6 by todd sharp posted on 4/28/2009 at 9:47 PM

It should be noted that the Seeker bug was my fault, not yours! :)

Comment 7 by Raymond Camden posted on 4/28/2009 at 9:50 PM

@shag: I need to stop blogging during lunch. ;) Fixed, thanks.

@Todd: Heh, well, I've made the mistake too, so didn't want to blame ya, especially when you were helping improve the project. :)