Written by: Mark Bouwman
Yes, you read it right: Debugging with ducks. Today I wanted to talk about debugging a little. Of course I don’t have to; programmers never create bugs, right? All works as intended! It’s not a bug, it’s a feature! It worked fine just yesterday!
But for that one special case where you just can’t find the bug, there’s the “Rubber duck debugging”. It’s quite simple, really. All you do is explain your code. Line by line. Out loud. To a duck. This might sound silly, but it’s actually a pretty good way to debug your code. There is reason and logic to this method!
Well then, how does it work?
You start off by explaining to the duck what your code is supposed to do. With a clear goal of what you want to explain set out for you, you then explain the code to the duck, line by line. Each line you say out loud, telling the duck exactly what the code does. More often than not you’ll find out what was wrong somewhere halfway along the code.
You might be wondering: “Why does this work?”. The magic lies in explaining. There are so many advantages in doing so.
When you take the time to sit down and really look at your code, you’ll find bugs and errors in your logic quicker. You can stare at code all you want in order to find your bug, but you won’t actually see anything. When you have to explain the code however, you re-read all of it carefully. You actually read it in order to get terms and sentences to use for your explanation, in order to get mental ‘checkpoints’ for the story that you’re about to explain. When doing this, you’ll find flaws in the flow of your code quicker.
Another reason the rubber duck debugging works is because you speak out loud. Saying things out loud helps as you take more time for each separate line of code. Also, when you want to say something out loud you first have to clarify your thoughts. You have to turn all the information you know (and assume to know) into something you can put into words.
Thinking out loud also triggers different parts of your brain. Not only do you think about it, but you also hear your story and talk about it. These are different inputs for your brain, allowing it to process all the data in different ways.
Another thing that’s so great about the rubber ducking method is that you can do it by yourself. You don’t need to bother anyone, and no one will really bother you (I mean, c’mon.. You’re talking to a rubber duck. People should be afraid to bother you). It often helps in finding solutions to the problems you’re facing without having to feel bad about resorting to someone else. And when it doesn’t work out, you’ll have a clear view of the problems and the goals. This helps when you have to explain it to a fellow programmer.
All in all, debugging with a rubber duck is a great way to think about the code you wrote. It helps to clarify your thoughts, it helps to think about the flow of your code, it helps to see things you’ve missed. It’s a great way to debug!
And let’s be honest here: who doesn’t like to have a rubber duck on his desk?
2,484 total views, 1 views today