What Is Rubber Duck Debugging?

December 8 20213 mins read

What Is Rubber Duck Debugging?

What Is Rubber Duck Debugging?

The rubber ducking method is one of the debugging methods used by software engineers.

Let's say you have a bug in your code. It could be anything really, but let's just say it's an off-by-one error. An off-by-one error is when the index or counter you are using for some purpose is not incremented correctly -- either incrementing/decrementing one too many/too few times.

Rubber duck debugging or rubber ducking is a method of finding computer bugs in which the examiner explains their thoughts while examining the code. The process was named by Jon Bentley after the childhood game of "duck duck goose" because the programmer is "chasing down a bug like a duck after a greased goose".

This can be a very common bug, and it can be quite hard to track down because the error only manifests itself in certain cases, after a lot of code has been executed. No worries though, you have a faithful companion with you; rubber duck debugging. 

Rubber duck debugging is simple. You explain your problem to the duck and the duck listens. If you explain what your code does wrong to a rubber duck, it doesn't know about programming languages or compilers, so it can only give you very naive responses such as "That's not right" or "What?". This is perfect because then you can just say that line by line until the duck is totally confused. At that point, you've probably figured out the bug yourself.

This technique is often used by programmers, but it can be helpful for anyone trying to debug a problem. The problem with rubber duck debugging is that it can be difficult to explain technical problems to non-technical people, and even harder to explain code. It helps if you know your audience, or at least what language they speak. Just talk through the problem out loud, and see if that helps you to find a solution. And if all else fails, there's always the trusty rubber duck.

The method is also popular in many hackathons where hackathon participants are required to use programming puzzle problems as an outlet for creativity, problem solving and teamwork. In such contexts, the process is given names such as "coding kata". Coding kata is a programming puzzle problem that helps developers improve their coding skills. The problems are designed to be solved in a limited amount of time, and they usually require you to improve your skills in existing languages. Coding kata or rubber duck debugging can also help you learn new languages or improve your skills in existing languages.

Is the plastic yellow duck necessary?

There is no requirement for what the object should be, but having a yellow duck makes it even more adorable :)

Here's what to do for the rubber duck debugging method:

  1. Start by providing a plastic duck or any other object that you feel more comfortable describing the problem with. 
  2. Place this object in a visible place in your work environment.
  3. Explain your code line by line, including the output your code should give at the first place you got stuck and the output it is giving now.
  4. If you weren't able to detect the bug in a short time, be sure to describe in detail all intermediate states and transitions.

By explaining your program to a duck or an object, you have to try to understand and fully explain the problem in your code.

This is exactly what makes the rubber duck debugging method effective. And if all else fails, there's always the trusty rubber duck.

I hope it works for you too :)

Stop losing hours on brain-mushing grunt work.
Robotalp gives you way more time and way less busywork.