What is rubber duck debugging?

Have you ever spent hours trying to figure out what the hell is wrong with your code, only to find out later on that you forgot to put a comma somewhere, or you spelled the function name wrong? I sure have. In fact, debugging is one of the most critical parts of my job and I seem to spend more time trying to FIND the problem than I do coming up with a solution for it.

When I first started out as a developer, I could spend hours or even days trying to figure out what was wrong. You can multiply that if I was debugging someone else’s code. Then, one day, I read an article (I wish I could remember where) about this trick – rubber duck debugging.

It literally was a life altering moment for me. You know how you start talking about your code with someone, and then as you’re talking, you realize you made a mistake or you found something that doesn’t work right? Well, before you do that, do the same thing – only this time, talk to a rubber duck.

You heard me right! It’s amazing, but it’s guaranteed to work. Pull a rubber duck out of your desk drawer, or just keep him sitting out, and do this – put him facing your screen and pull up the code that is troubling you.

Now, explain the code to the duck. Line by line. Explain how it works, explore the code, tell the duck everything it needs to know about how your code works and what it’s supposed to do. It may seem silly, but it’s not about talking to the duck or going through your code – it’s about helping you focus.

You’re basically now looking at your code from a different perspective, and you’re not bothering a coworker in the process. By observing what your code actually does and what it’s supposed to do, the changes become obvious. 90% of the time when I’m working on a project, it’s simply because I’ve overlooked something in the code.

Try this out on your next project, and let me know how it works for you!