There are very few things that feel worse than running your program for the umpteenth time only to learn that it still won't start. Even the best programmers know what it's like.

You're staring at a block of code that you've been chiseling at for hours. That syntax looks correct, your spacing is consistent, you've named your variables appropriately... so what gives?

Maybe you need a new method to debug. And you would be surprised to find that one of the tried-and-true ways to go about it, oddly enough, involves a toy of the bathtub variety.

What Is Rubber Duck Debugging?

This is best explained through a little story. Picture this: you're a programmer working with a team of people. You're doing your day job, coding whatever it is you need to code, and then you run into a problem. For whatever reason, the program isn't working as it should.

So you decide to walk down the hall and tell your colleague that you're dealing with a stubborn bug. They agree to help you out and ask you what exactly is wrong. You start explaining the problem in detail and find that, in the middle of your explanation, you've hit upon a solution.

This actually happens in the real world more often than you think. You might be wondering what all that has to do with rubber ducks. Well, you could have confided in a plastic yellow buddy (or any inanimate object), instead of disturbing someone else.

The best part? You can find comfort in knowing that your duck isn't going to judge you for your long-winded spiel. If you find yourself needing to debug often, check out this list of the most common programming and coding mistakes.

Where Does the Term "Rubber Duck Debugging" Come From?

"Rubber duck debugging" is a reference to a story in a programming book called The Pragmatic Programmer: From Journeyman to Master by Andrew Hunt and David Thomas. In it, a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line-by-line, to the duck.

Why Does Rubber Duck Debugging Work?

Oftentimes, in describing what the code is supposed to do, you observe what the code actually does, and subsequently, find where things don't line up. That's why it is said that teaching a subject is one of the best ways to learn it. You have to evaluate issues from different perspectives to help someone else understand, which helps you understand.

The most important part of using this method is your commitment to it. Yes, it may feel extremely silly to have such a thorough one-sided conversation. But pouring so much effort into walking another person (real, imaginary, or made of rubber) through your problem is exactly what will make the problem seemingly solve itself.

This method works so well because most bugs in code are caused by a lack of providing explicit information and instruction to the software. Computers can't understand vague instructions. By assuming that your duck knows nothing at all about your problem, you’re forced to describe all the nitty-gritty details. Turns out that you didn't understand everything as well as you thought you did.

How Do I Use the Rubber Duck Method?

There are only three simple steps to rubber ducking:

  1. Assume your duck knows absolutely nothing about your problem. State the problem, how things are sitting at the moment, and what you are trying to accomplish instead.
  2. Explain the flow of things. What happened? Go over every single step of the process without missing out on a single detail.
  3. Arrive at an epiphany.

Applying the Method to a Real-Life Problem

This handy method was coined by programmers, but it can definitely be applied to problems outside of programming. How? Here, let's build another imaginary scenario: you just bought a new refrigerator for your house.

You get the new fridge shipped to your home and put it in its spot in the kitchen. You transfer all the goods from your old fridge inside the new one and get rid of the former. The day ends and you go to bed. When you wake up the following morning, you open the fridge door to discover that all your food has gone bad overnight!

You're furious, of course, and you start venting to a family member. "How could this have happened? I paid for it, brought it back, and moved everything inside this thing that, when powered, is supposed to have incredible temperature contro—"

Oh. You forgot to plug the darn thing in.

That's a little embarrassing, but at least you know the problem now. That's because you took the time to think about the whole situation from start to finish. Sometimes, when you're so deep and familiar with a process, you don't notice the most obvious issues creep right by you.

Applying the Method to Code

For example's sake, we'll be going over some sample Python code. As we've demonstrated though, you can use your trusty rubber duck to solve all sorts of different problems. The method can be applied to whatever programming language you happen to be working in.

We want to print the numbers one to five. So, you go ahead and type all that code out:

        for x in range(5):
 print(x, end=" ")

Output: 0 1 2 3 4

Huh. That's weird. You asked the compiler to print a range of five numbers. What's the problem here? Let's go over all the possibilities. Your syntax is correct since the code ran without errors. It's not that you're missing any arguments, because the start and step arguments are optional...

Hang on. The range function returns a sequence of numbers, increments by one (by default), and stops before an upper limit that you set. But where do ranges begin? In Python, ranges start at zero unless specified.

Now, you know you're supposed to write.

        for x in range(5):
  print(x+1, end=" ")

Output: 1 2 3 4 5

Related: The 5 Best Websites to Learn Python Programming

Your Unlikely Programming Buddy

While unable to just tell you where exactly you went wrong in your code, rubber ducks have helped programmers everywhere. Next time you run into a bug when programming, try grabbing a rubber duck (or some other object or person to act as one), and see whether this problem-solving method works for you.

Who would've thought that rubber ducks could be more than just a bath toy?