Rubber Duck Debugging: The best way to debug your code that you’ve never tried

By Alex Coleman

It might not work as well with a real duck.

You sit down to do some programming. Today you’re going to add some code to display video games in your new Laravel web application, a video game crowdsharing app named Gamez0r. You’re stoked.

Everything’s going great. You’ve created the model and a database migration and can successfully retrieve video games from your database.

Time to create a controller. You generate the controller file and start to build out the indexaction, to power the page that will display all of the video games.

Then you realize that you want the page to only display games made after 1985. You’re going to show the classics on their own page. After all, they deserve their own page.

But then, all of the sudden, you can’t remember how to filter a collection of objects. Right now you’re still retrieving all of the video games from the database.

You’re stumped. And you can’t even think of what to Google. You try “laravel filter model results” … No luck.

We’ve all been here. And will be here again, many times, in the future.

But the question that still begs an answer is: “What do you do now?”

If you want to learn my sure-fire system for getting out of binds like these, read on.

Rubber duck debugging: the answer to your problems

Let me introduce you to one of my favorite programming (and otherwise) problem-solving techniques: rubber duck debugging.

Rubber duck debugging is a simple system that helps you solve problems — like the one described above — when it feels like nothing else seems to work.

And it’s dead simple. All you need to do is:

  1. Recruit an inanimate object — or a willing friend or colleague, if they’re available! — to listen to your issue. (Note: recruiting the inanimate objects is easier.)
  2. Start at the beginning, and explain to your recruited object/person, in as much detail as possible, what you’re trying to achieve, and what isn’t working.

That’s it.

It’s just you and your duck.

Often times when you’re working on something complex, like programming, your brain gets trapped, running nonstop of its own internal hamster wheel, doubling back to the same thing time and time again, making no progress.

If you stop and take the time to thoughtfully describe your problem, you force yourself to assess the situation from the outside. And that lets your brain work in a different way and gain an objective understanding of what’s happening.

And then the answer — and forgive the overused cliché — just comes to you. But really, that’s how it typically ends up working.

Rubber duck debugging in practice

Here’s an example of how rubber duck debugging might be used to solve our original video game display issue:

  1. You program for a while, making solid progress.
  2. You get stuck. You just can’t figure out what’s wrong.
  3. You pause, and take a deep breath. (<- This, by the way, is the hardest part of the process.)
  4. You get your duck. Or, better still, your favorite Darkwing Duck figurine.
  5. And you explain what’s going on to said duck: “I created my model. I made my controller. I’m using the model to retrieve my video games in my controller… but right now it’s retrieving them all. I only want to retrieve the ones made after…”
  6. AND THEN THE AH-HA MOMENT STRIKES. You need to use a where call in your video game retrieval line to restrict it to fetching only video games with a production_dategreater than or equal to ‘1999–01–01’!

And that’s how it feels almost every time: “Duh! I knew that!”

Because really, you’re just finding a way to access information that’s already stored in your head. But there’s a lot of stuff in there, so it takes a little extra effort sometimes.

Next time you’re stuck, try it

Sorting through bugs, problems, and general conundrums is a fundamental part of programming. So developing techniques to swat your way through the bugs and find your way out of the binds is as crucial as learning all of the syntax.

Next time you’re stuck and nothing else seems to be working, try rubber duck debugging. It’s almost always helpful to get more information, and rubber duck debugging helps you do just that.

You’ll likely come up with your answer, and your duck will be no worse for wear. Hell, he’ll get to participate in a nice conversation. Probably end up notching one in the “good day” column, even.

This article first appeared in my weekly newsletter for aspiring self-taught web developers.