Juha-Matti Santala
Community Builder. Dreamer. Adventurer.

print is your best debugging tool

I'm a huge fan of debugging. To me, it's one of the most enjoyable aspects of software development. For the past few years, I've been spreading my love for debugging in various meetups, workshops and conferences, most recently at HelPy meetup and Django Day Copenhagen conference.

Each time I've given the talk, it has sparked great discussion: some people agree, some disagree and sometimes we get to have great in-depth discussions about the topic.

This week, instead of rehashing my talk into a blog post as-is, I'll focus only one small part of it: the print statement. In your language, it may come in dressed as console.log, print, println, printf, Console.WriteLine, echo or some other form – regardless of its form, we're talking about the same concept: printing something into the console.

The Superpowers of print

Many might not consider print to even be a debugging tool and that's a fair assesment. One could argue that the category starts from more advanced and specialized tools: something like debugger; in Javascript or breakpoint() in Python that halts the progress of the program and gives you access to inspect and interact with the code.

However, from my point of view, print is a tool that can help you debug and hence I'll categorize it as a debugging tool. Regardless, that semantic is beyond the point. We're here to talk about how and why is print so powerful.

print is not all-powerful. It's a very simple tool that does one thing. And the simplicity is the beauty of print.

First, it only takes a few seconds to write print('I AM HERE') which is kinda where I often start.

There's this brilliant meme that I originally found from @JenMsft's Twitter:

Four panel meme: 1st panel, an evil cartoon character points up with grim next to a whiteboard that says "need to debug an issue". 2nd panel, the board says "set a breakpoint to get started", 3rd panel, the board says "the breakpoint never hits" and the 4th channel, the board says "the breakpoint never hits" and the evil character looks towards it awkwardly
from https://twitter.com/JenMsft/status/1256007715425382400/

I've seen something similar happen often with developers of every skill level: they see a problem, they dive immediately into the code, try to reason with the code, change something and nothing changes in the software.

A single print, that takes a few seconds to write, can confirm to you that you're in the right part of the code. It's the developer equivalent of asking our parents "make sure you've plugged it in" when they complain the printer isn't working. Our brain keeps telling us, surely we know that simple thing but we quite often don't.

Secondly, the print is easy to read. All you need to do is to keep an eye on the console and see if you see your print output. No need to dig into some logs like if you'd set up logging and no need to halt the execution of the program and run commands like with debuggers. It's especially nice because you don't need any infrastructure set up.

Not having to set up anything means you can debug any legacy software in pretty much anywhere.

Printing being such an integral part of any programming language, it also means that you can effectively start debugging software even if you're not very familiar with the technologies used. Granted, that happens rarely in real life but as someone who teaches and mentors a lot of juniors, it has been handy.

I've heard from few sources that some senior developers have been teaching juniors to avoid printing. I can only guess it comes from the dislike of print statements ending up in production but it's unfortunate if the side effect is that new developers skip printing altogether, even when it's hugely beneficial.

Shortcomings of print

Even though print is the your best tool, it's not perfect.

There are often cases when print is not enough and you need more powerful tools like debuggers, or it's better to setup something that is always running like logging and monitoring to give you a head start for debugging when problems occur.

One major downside of print is that you need to run the software (or the functionality you are debugging) over and over again everytime you want to change what you're outputting.

Also, if you have too many print statements, the output can become hard to follow. And debugging more complex objects or expensive function calls can be cumbersome with print.

When any of those issues comes marching towards you, reach for the more specialized and powerful tools. But until it, don't belittle print but instead make it your best friend. Because it's fast, simple and inherently powerful tool.

Syntax Error

Sign up for Syntax Error, a monthly newsletter that helps developers turn a stressful debugging situation into a joyful exploration.