Yellow Makes Your Code Green

Yellow Makes Your Code Green
Photo by S. Tsuchiya / Unsplash

When you have a problem in your code, what's the first tool or technique you reach for to get it fixed?

A debugger? The unit tests? The documentation?

Myself, I start with this little guy: the rubber duck.

This is a real thing in software development. Rubber duck debugging was popularized in The Pragmatic Programmer in 1999. It's laughably simple, and with a little self-awareness, it points out problems in your code so fast you'll never be able to keep up with all the changes you'll want to make. (Whether you should make those changes or not is another question, but that's a whole other topic.)

Here's how to do rubber duck debugging:

1. Beg, borrow, steal, buy, fabricate or otherwise obtain a rubber duck (bathtub variety).
2. Place rubber duck on desk and inform it you are just going to go over some code with it, if that’s all right.
3. Explain to the duck what your code is supposed to do, and then go into detail and explain your code line by line.
4. At some point you will tell the duck what you are doing next and then realise that that is not in fact what you are actually doing. The duck will sit there serenely, happy in the knowledge that it has helped you on your way.

(I sourced the above from rubberduckdebugging.com, however the original attribution goes to ~Andy from lists.ethernal.org)

Obviously, fixing the bug is a big win. It doesn't matter how cool or elegant your code is if it doesn't work! It's also useful for determining if the reported issue is actually an issue. But fixing the logical error is stopping short of the rubber duck's true potential as a code quality tool.

Step 3 – explaining your code – should be simple. If you have to stop (even for a moment) to figure out what a section of code does, you probably have a code quality issue. Are you sure you abstracted that logic enough? Are there enough comments in the code?

And all during this time, your little rubber copilot is just content that it was able to help.

Your rubber ducky won't judge your code. It won't give you grief for a defect you introduced. And with it's lack of rubber legs, it's not about to run around and desert you either.