Why I Just Use Print Statements To Debug Code

I’ll be the first to admit that my testing and debugging game isn’t as good as it should be. When I code I like to just do the thing, break it, make it more secure, add more awesome functionality till it breaks again, and then rinse and repeat. I haven’t gotten any complaints yet. All of my clients are happy with my work and I’d like to say I’m not doing too bad for myself.

A question I get asked a lot is how I manage to find my bugs quickly and fix it so fast. My secret? Print statements. Don’t make fun of me, please. I know breakpoints exist, but I use simple editors like Vim and Nano to code. Sometimes I’ll use Notepad++ if I’m feeling a little feisty. And honestly I don’t even know if there’s a standard way I should be debugging on there. If there is, I just haven’t had the need to go Google it (lol).

What I do when I debug is to find where the error is happening, and then put some print statements around the area where the exception is occurring to find out what is going on with the variables, logic, etc. It works for me, because it allows me to see how a variable is changing at each iteration of logic that I put it through. For real.

I see it pop up on the console and I’m able to follow what happens at each line in one nice looking terminal before it ends in an error. Then I fix it where I see a variable change in a way it is not supposed to. Simple as that.

Putting print statements in to debug also reinforces my understanding of what I’m doing at each line in the code. Go back into a year old code base and then try and remember what you are doing. It’s easier if you add in some print statements like “at this part I’m doing some complex math on this variable and it should produce this output” and then delete when you go into production. Comments are a developers best friends, and print statements are a debuggers.

This is a short blog post because there really is no secret to it. I just use print statements to debug because it’s easy, it makes sense, and it saves me time. That’s the most important thing.

Is using print statements to debug weird or a common thing? Let me know in the comments below.

Happy coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s