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
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
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,
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:
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.
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
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
When any of those issues comes marching towards you, reach for the more specialized and powerful tools. But until it, don't belittle
Sign up for Syntax Error, a monthly newsletter that helps developers turn a stressful debugging situation into a joyful exploration.
If you found this post interesting, consider sharing it with your community or following me on Mastodon.