Shruti Turner.

What Exactly is the Function of a Rubber Duck?

Machine Learning EngineerML EngineeringMachine LearningData ScienceData Scientist

Now, those with a particular love of Harry Potter might be wondering how a cracking book quote is relevant at all to a Machine Learning Engineer's blog. Everyone else might still be wondering why I'm talking about rubber ducks. No, I'm not going mad this is a real thing and knowing about it might well change how you approach your code problems...

You've got a bug...

Take yourself back to a situation where you were stuck with your code. Maybe it's running without errors but you're getting an output you didn't expect? Or perhaps you're getting an error but you just can't figure out why so you're going back over your logic.

Sometimes all the code and logic and syntax circling around your brain can just feel too much. We just want this to be a problem we can talk through in my own natural language, but no one is around to listen. To be honest, you don't really want to talk to anyone and have a conversation but it would be handy to have someone to talk at...

Enter rubber duck!

Rubber Duck Debugging

We don't need *someone* to talk to, but something with a face can be just as helpful. They don't need to respond and you can feel like you're talking through your problems in your natural language without taking up anyone's time.

The term can be traces right back to 1999, from The Pragmatic Programmer book, by Andrew Hunt and David Thomas. In this, they describe a programmer who carries around a rubber duck (yes, the bathtub variety). This duck is a constant companion for the programmer to explain their code line by line to, which in turn helps the programmer figure out how to solve their bugs.

Do I Really Need a Rubber Duck?

You don't *need* a rubber duck specifically, you could talk to anything or anyone who'll listen. I often talk to my dog. I'm pretty sure she sometimes just looks at me like "Really, Shruti, why would you do that?" and other times I think she's just happy to be getting a fuss whilst I talk things through with her.

If you'd rather have a response or you prefer to type than talk out loud, you can check out this website where you can talk to Cyberduck.

By explaining the code step by step, to something (someone if you're attached!) with no assumed knowledge of code means that you have to break everything down. Not only does this mean that you have to really know and understand your code, which is great to reinforce your learning, but also by talking out loud you can hear when something just doesn't make sense. You can think about it similarly to when you write something down and think it makes total sense, and then you read it out loud and you realise you've got words in the wrong order or a typo of poor grammar! It's the same thing in the coding world.

It's also a great way to improve your code, our brain processes things differently when we're talking out loud. Something just might click as you're talking out loud and wondering "why on earth would I do that?" - challenging yourself with these questions is a great way to learn. Can things be done a different way? Can I write my code to be more efficient? Great ways to be come a better developer, whatever the focus of your code.

I realise, this might sound a little crazy but humour me..give it a try! There's something about talking things through with/talking at something that can make something click. Whoever your coding companion is, they're really worth having around.

Share Now



More Stories

Cover Image for Tickets, Please?
Ways of WorkingTicketsAgileScrumKanban

Tickets are the building blocks that make up a team’s work, without clearly defined blocks it’s difficult to work efficiently and effectively as a team, catching gaps and avoiding duplication of work.