May 2014

Gavin Pickin

Techie Gotchas - Debugging Code - How important is it to a Programmer?

CFML Language, Chit Chat, Conferences, Dependency Injection, Techie Gotchas, WireBox

It seems as programmers, we're always writing code, but as Dan Wilson said in one of his great sessions at cf.Objective() last week, we spend only a small fraction of our time writing new code, we spend most of it debugging, fixing and reading code. Thats not something you normally think about, but it aligns with my thinking recently, about what makes a good programmer. Being involved with teams for some time now, albeit small teams, adding new members is something which is constantly on the horizon, and the more I think about it, one of the biggest signs of a good programmer is the ability to spot bugs and problems with code. If you're fixing, debugging, or just trying to read code, it makes sense that reading and understanding the code is the most important thing. Now, whether you agree or not, why am I writing this today? 

I have been re-writing and integrating a lot of cool new things into our codebase at work, and its fun, although slower than normal, because creating conventions as you go requires a lot of conversation, and almost 2 steps back 1 step forward progress. Changing so much so quickly has got me debugging a lot more code than normal, as I go out of my comfort zones, and it reminded me of some of the quirks when debugging, and I thought I'd share them, and invite others to comment on their own debugging stories, lessons etc.

I know I have before talked about misleading error messages, but that is what throws a spanner at me more than anything else, so of course it comes up again. Of course, debugging is full of stupid mistakes, and I'm happy to share my stupid mistakes, because guess what, I make a lot of them, but I pride myself on spotting them and fixing them quickly, and keeping the work going. Yesterday, after coming off the Into the Box and cf.Objective() high, my brain escaped me for a while, and I was having some issues with my simple dao.

Example 1

I was passing in an Integer, doing a simple get query, with a integer query param, and returning the query.

<cffunction name="get" returntype="query" output="false" description="Gets an Address">
    <cfargument name="addressID" required="true" type="integer">
    <cfset var="" q="">
    <cfquery name="q">
        FROM address
        WHERE addressID = <cfqueryparam value="#arguments.addressID#" cfsqltype="CF_SQL_INTEGER">
    <cfreturn q="">

Now of course, this is a simple issue, and I blame my brain not being fully engaged, but what threw me was the error message.
I'm on CF9 for this application, hence the tags for queries, but the error message stated (paraphrasing but close enough)

The variable addressID being passed into get is not of type integer.

I checked my code that was calling it, verified, and then clicked.

The variable is of type integer, but thats not a valid type to check for in cfargument. It should be numeric. Of course, since I was in the DAO, I was thinking integer integer integer, but the error message threw me off. Sometimes, you have to trust your gut and use your code smell to sniff out the real problems, because sometimes the error messages are as sloppy as your code.

Example 2

The next issue I was having, was while using Wirebox. Wirebox is a pretty slick product, and I am loving using it, it is pretty non intrusive, and as little as we are using it so far, its making life easier daily. I wish we had everything using it, but baby steps, and legacy is brittle, so lets not rush it. Now, when WireBox's config runs, it preps them, ready for use. I was getting an interesting message (which since it is open source, I might have to dive in and update this), but when it was doing the prep, it would give me an error message stating my AddressDAO was not of type AddressDAO. I looked at it, the file is named AddressDAO, I have my component name at the top matching, I am returning the type AddressDAO from the Init function, which I thought was weird. What else could effect this? In the spirit of debugging, if it errors because its looking for a given type, change it to any, and see what happens.

Do you know what happened?


Yup, it was me, sloppy me again, I had an error, but when it tried to do the prep, it didn't tell me there was an error, it just didn't Init right, and pass back the right object, it caught the error, but then it threw a type error.

So, I'll hunt down that piece of code, and maybe append something to the message which states that the CFC itself might have compilation errors or something.

The lesson learned was, just because you get an error message, it might be more under the covers.
Of course there are lots of useless error messages in languages, its funny how you learn them over time… but sometimes, they can throw a spanner at you.

What are some of the weirdest misinformational errors you have seen with ColdFusion?

My worse, apart from the cfarguments, was problem the cffile, if the Destination is invalid, it complains that the Source File is invalid… obvious copy and paste and didn't change the error there.


Share the despair :)


by Joey Daly
05/22/2014 05:04:07 PM

Hard to remember the good ones, but the other day I had:

null null <br>The error occurred on line 353.

At least it gave me a line number.

by Jose Galdamez
05/23/2014 11:14:40 AM

My favorite is when you get an error in the middle of some huge SQL query, but the line number is only the first line of the query.

Blog Search