DEV Community

Discussion on: Using Print Statements Are A Handy Way to Debug and Explore Code

Collapse
 
yaser profile image
Yaser Al-Najjar • Edited

I still remember once I said in reddit:

I don't use the built-in debugger in Visual Studio 2017

I got lots of negative attitude like:

Are you really doing software developing?!

Heck yeah... I generally use Console.WriteLine or print for debugging my apps!


To make finding the printed line easier for me (say I wanna see the value of x), I do something like:

print('koko')
print(x)

So I'm gonna search for the word "koko" in the output, and the next line should be x :D

Collapse
 
samuraiseoul profile image
Sophie The Lionhart

Yeah I'm going to have to agree with them for the most part.

This is fine for a quick "Does it hit this line?" or "What is the value of this one thing?" But from there you need more diagnostics. What caused that value? Now that I know this value, what are the values of these other things, how many times is this gonna loop, why didn't this thing evaluate to true? You might have all of those questions sequentially, and will have to run your program again and again and AGAIN. But if you had a break point, and used your debugger, you could have figured it out in one run. You could have used the console to evaluate some operations on the variable x + 2 is "12" instead of 3 because x is a string, oops. Its easier to see types and things using the debugger than using print.

Collapse
 
yaser profile image
Yaser Al-Najjar

I agree with you... in some cases like doing queries and digging through each the vars in the hierarchy, it's much faster to debug.

But, for checking the type, one simply can just print(type(x)) aside from the fact that exceptions show clearly what happened when you add two different types in python :D

But using the debugger as a frequent used tool is just time consuming.

Thread Thread
 
samuraiseoul profile image
Sophie The Lionhart

That works in python, but for other langs it won't. In addition the debugger should not be any more time consuming than the print statement. Add the break point and run the program, you have to type the print and run the program anyways. If its too hard then you have some other problem in your workflow to work out. You also run the risk of forgotten print statements and logging. enough of those especially in a loop can absolute MURDER performance. Print statements for debugging are no faster than debugging, but they are more dangerous.

Thread Thread
 
yaser profile image
Yaser Al-Najjar

In addition the debugger should not be any more time consuming than the print statement

I developed recently in django and spinning the debugger takes more time than printing and re-running the app.

It always depends, sometime running the shell and trying things out there is really much faster.

Print statements for debugging are no faster than debugging, but they are more dangerous

When you try this stuff in Android development (esp. Xamarin Android), you will change this line :D

enough of those especially in a loop can absolute MURDER performance

Generally you won't test a loop or an algorithm by hand / debugger, you would write logical tests against it... and run those tests till you get green ;)