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#”> </code>
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?