Archives For debugging

Whenever you begin working with a database, you introduce more possible causes of errors. Thus, you must learn additional debugging strategies. When using PHP to run queries on the database, the problems you might encounter include:

An inability to connect to the database
A database error thrown because of a query
The query not returning the results or having the effect that you expect
None of the above, and yet, the output is still incorrect
On a non-framework site, you just need to watch for database errors to catch the first two types of problems. There’s a simple and standard approach for debugging the last two types:

Use PHP to print out the query being run.
Run the same query using another interface to confirm the results.
Debug the query until you get the results you want.
When using a framework, these same debugging techniques are a little less obvious, in part because you may not be directly touching the underlying SQL commands. Thankfully, Yii will still be quite helpful, if you know what switches to flip.

Continue Reading...

Rubber Duck Debugging

December 4, 2010

I recently came across the concept of “Rubber Duck Debugging”, with which I was previously unfamiliar. I first saw the idea on an old Linux mailing list, although there’s a Wikipedia article on the subject as well (of course). You should read the first link (it’s short), but the basic premise is that you can debug code by walking through it, trying to explain the code to an inanimate object (or an animate one, if you want). The theory is that when you get to the point where your explanation doesn’t make sense, you’ve found the source of the problem. I’m personally a big advocate of the “walk away from your computer” debugging approach: when you’re having a really hard time debugging a program, step away from your computer and don’t think about it for a while. In my experience, you’ll either solve the problem fairly quickly after you stop thinking about it, or fix it shortly after returning to your computer with fresh eyes. In any case, you need as many debugging tools as possible in your programmer’s toolbox.

In the past decade I’ve also discovered an effective, yet impractical, way to improve one’s programming skills: write a book! We’ve all had times where an application or some bit of code did exactly what you wanted it to, even though you didn’t understand exactly why. And, really, that’s fine. Sometimes functioning is all that’s required, even if that’s not an ideal goal. The next level of knowledge is knowing why something works. And a higher level is knowing something so well that you can explain why it works to someone else (duck or not). More importantly, you understand why the code should do A instead of B, or you are able to clearly see that X isn’t necessary. I’ve found that writing a book on a subject hones those skills. In short, I know personally that I’m a better programming because of the writing, and teaching, I’ve done. Clearly, it’s not practical for everyone to write a book on everything they do, but I would recommend you occasionally take a crack at trying to explain some code—even code that’s working perfectly fine—to anyone or anything as a way of solidifying your own knowledge.