Programming can be a fun and rewarding experience… until the program does not run. You know you have typed everything correctly and have read your code over at least four times now. However, no matter what you do, you encounter some sort of error. Perhaps the code compiles but does not perform the exact job it is supposed to do. Perhaps your code refuses to compile at all for no clear reason. This can be extremely frustrating (especially if your integrated development environment, a software that can be used to make programming much easier does not come with automatic error flagging) and often requires you to debug your code to determine what the exact issue is.
While some may enjoy the process of debugging, others may not. Fortunately, though, there is an often-mentioned method of debugging that not only can make it a little more bearable but can also help you to catch errors that otherwise could have remained invisible. This method is referred to as “rubber duck debugging”.
How Does it Work?
Rubber duck debugging is simple. Take a rubber duck and place it next to your screen. Then, go through your code, line by line, and explain, out loud, what the line of code is supposed to do and what the line of code is actually doing. By verbally explaining your code, you become much more aware of what the code actually says.
Of course, if you do not have a rubber duck, you can substitute in any object. You can print out a piece of paper with your favorite TV show or movie character, you can talk to a mug or a cup, or even put a set of googly eyes on a potted plant or a textbook. As long as you are verbally explaining your code, step by step, you are achieving the benefits of rubber duck debugging. You could also opt to explain your code to a person as well, if that is more your style, but the benefit of a rubber duck over a person is straightforward: the duck won’t talk back. However, if you do want direct feedback, then by all means, ask a person!
Example
For example, take the following Java code snippet:
for(int i = 0; i < 10; i++){
System.out.println(“Current number: + i”);
}
This code snippet is meant to count from 0 to 9 and print each number out to the console. You may have already spotted the error here, since it is a rather simple one. Imagine, though, that this is one chunk of code buried near the bottom of a thousand or so lines of code. After scrolling through that much code, it becomes increasingly easier to miss even a mistake like this.
While the program is meant to count, it instead prints “Current number: + i” to the console 10 times, which is not what we want. Let’s try utilizing the rubber duck debugging method.

for(int i = 0; i < 10; i++){
System.out.println(“Current number: + i”);
}
Ok, Mr. Rubber Duck. This first line here is a for loop. Our loop variable is “i”, and it starts at 0. So long as i is less than 10, the loop will execute. After the loop executes, i will be incremented by 1. So far, so good; this line of code does what we want it to do.

for(int i = 0; i < 10; i++){
System.out.println(“Current number: + i”);
}
Next line is our print message. It tells the system to send our message to the default output location, which is the console, and it tells the message to be printed on its own line each time. So far, so good. The message it is supposed to print is “Current number: ” and whatever value “i” holds at that time. However, it looks like we made an error here! The closing quote is in the wrong place, and that is why it is not printing our expected message! Instead, it is printing the entire output as a String! Let’s fix that now. Other than that, there are no other errors with this line of code.

for(int i = 0; i < 10; i++){
System.out.println(“Current number: " + i);
}
Final line of code, Mr. Duck. Looks like it is the end brace for the for loop. Everything seems to be in order now, and if we run the code, it will work properly!





Leave a comment