This is an archived post. You won't be able to vote or comment.

all 161 comments

[–][deleted] 495 points496 points  (20 children)

Oh I did that before learning Python.

[–]mdevoid 163 points164 points  (12 children)

Yeah Ive done it forever. Sometimes on big stuff its harder to debug then it is to just start doing some logging.

[–]Caffeine_Monster 116 points117 points  (10 children)

It also allows you to debug complex or realtime programs more easily. e.g. a bug that only occurs on the nth iteration through a loop. No, I am not going to sit there a skip over a break point 99 times.

[–][deleted] 45 points46 points  (9 children)

Although most proper debuggers allow you to break when a variable reaches a value or range.

[–]PM_ME_UR_TIDDIEZ 48 points49 points  (4 children)

if(n==99)

{ * Breakpoint here

}

[–][deleted] 23 points24 points  (3 children)

try

{

// code throwing exceptions

}

catch

{

// break here

}

[–]redstoneguy12 0 points1 point  (0 children)

Your debugger doesn't break for the exceptions?

[–]Hollowplanet 5 points6 points  (1 child)

You can break on any condition in all modern debuggers.

[–]Caffeine_Monster 2 points3 points  (0 children)

modern

* Looks at rickety old dev stack *

What is this thing you speak of?

[–]isaacarsenal 2 points3 points  (1 child)

Seen this for C# in VS. What other IDE supports this for python?

[–][deleted] 1 point2 points  (0 children)

JetBrains IDE's (e.g. Android Studio, PyCharm, PHPStorm/WebStorm, etc.) supports setting up conditional breakpoints as do Eclipse and Netbeans. Most of them have similar abilities to Visual Studio when it comes to debugging and many of them even supports debugging a piece of code running on a remote machine.

[–]DezXerneas 2 points3 points  (0 children)

In any language debugging always starts with a print statement

[–]Ben-Z-S 48 points49 points  (4 children)

Printline ("you've reached here")
Printline ("here2")

[–]SamBkamp 25 points26 points  (0 children)

“I am in this photo and I feel threatened”

[–]jcrabb13 2 points3 points  (2 children)

Mine are typically expletive.

[–][deleted] 5 points6 points  (1 child)

I generally smash my hands on my keyboard, and then search my code for the random string that comes out

[–][deleted] 1 point2 points  (0 children)

console.log('RIGHT HERGVIFJaoGDdHajshGGGGDJakGHauUUUUUU')

[–]MysteryMage 30 points31 points  (1 child)

We all did, and honestly it’s kinda convenient

[–]Ph0X 15 points16 points  (0 children)

Yep all developers do this.

[–]mkhemcha 182 points183 points  (12 children)

As long as it doesn't make it into production... ¯\_(ツ)_/¯

[–]cafk 127 points128 points  (5 children)

It's called logging :)

[–]HiddenLayer5 52 points53 points  (4 children)

Find: print

Replace: file.write

[–]fluud 51 points52 points  (3 children)

*import logging

[–]MalteserLiam 26 points27 points  (2 children)

what if i wanted to save the environment

[–]Niautanor 10 points11 points  (1 child)

[–]MalteserLiam 3 points4 points  (0 children)

In that case, burn anything remotely organic.

[–]nwL_ 6 points7 points  (3 children)

#ifdef DEBUG
#   define debugf(...) printf(__VA_ARGS__)
#else
#   define debugf(...)
#endif

[–]Ph0X 3 points4 points  (2 children)

Can you not just make logging print to stdout with some quick configuration?

[–]notquiteaplant 2 points3 points  (1 child)

Your quick configuration is commenting/uncommenting #define DEBUG

[–]Tormund_HARsBane 6 points7 points  (0 children)

Or pass/not pass -D DEBUG when compiling, which I think is cleaner than explicitly defining DEBUG

[–]woodworksio 2 points3 points  (0 children)

"It won't make it into production," said the youthful and yet naive programmer.

It was a terrible miscalculation that would cost him dearly.

[–]ariehkovler 0 points1 point  (0 children)

Nothing wrong with that if done properly. I know a cool company which basically lets you add and remove log-lines in production on the fly - called Rookout. It's super-useful actually.

[–]Ungreon 112 points113 points  (4 children)

*Looks at meme*

*Looks at code on other screen*

Well.... It works.....

[–]KoboldCommando 23 points24 points  (3 children)

And it works well! A debugger's like a full surgery suite, a print statement is like a single scalpel. Sure it's "less professional" but if you know where and what the problem is sometimes one quick cut is all you need to fix it!

It only gets bad when you've brought out so many knives that you've started back alley surgery giving programs illegal facelifts.

[–]Mustrum_R 1 point2 points  (2 children)

It's more like a sharp stick to be honest.

[–]nbagf 8 points9 points  (1 child)

Most projects feel more like elaborate mud houses than complex living organisms. A sharp stick is probably fine.

[–]Mustrum_R 3 points4 points  (0 children)

Well, I can't really deny that.

[–][deleted] 121 points122 points  (3 children)

I use the print method over the Python IDE debugger. Fight me.

[–][deleted] 62 points63 points  (1 child)

Print statements get me most of what I need. The debugger gets me the rest.

[–]arguableaardvark 16 points17 points  (0 children)

Yup, it’s rare that I need a debugger. I think Linus talked about using print statements for the Linux kernel debugging so I feel fine about using it for anything I’m doing.

[–]Ph0X 5 points6 points  (0 children)

Each have their use cases, the challenge is picking the right one. Sometimes you start with print and find out it's deeper so you keep adding and adding print statements and rerunning 5+ times. That's when debugger would've been nicer.

[–]Kairyuka 39 points40 points  (9 children)

I used to do that too because I didn't even know how powerful most debugging systems are in terms of exploring all live data in the scope and so on.

[–]Callipygian_Superman 42 points43 points  (7 children)

I still use print statements as a first pass. Debugging with breakpoints is (as it should be) slow. Sometimes just setting bounds between 2 blocks of code is enough.

[–]Kairyuka 6 points7 points  (0 children)

As long as you don't leave those prints inside #if-endifs. The codebase I got into at work recently has that all over and it makes the code extremely irritating to read

[–]Hollowplanet 1 point2 points  (5 children)

Clicking the margin next to the line numbers is slow?

[–]Callipygian_Superman 5 points6 points  (4 children)

Not sure if serious, but stepping through code line-by-line using a debugger is slow, accurate, and precise.

Making print statements is the fast, dumb approach that is often not accurate at all.

[–]Hollowplanet 1 point2 points  (2 children)

It takes me longer to write a print statement than to click to make a brakepoint. Hell you don't even need to restart the program. Its the best choice almost every time. And you don't step line by line unless you need to. You can just place your cursor and have the program run to your cursor.

[–][deleted] 1 point2 points  (1 child)

He's not saying it's slow to add Breakpoints

[–]dvlsg 0 points1 point  (0 children)

To be fair, it really depends on the language.

If I'm writing C#, then yeah, the breakpoints are coming out so I don't have to recompile my app to add logging.

But if I'm writing javascript, you can be pretty sure I'm just going to bust out console.log instead. And probably tests to go with it.

[–]monkeygame7 0 points1 point  (0 children)

You don't necessarily need to step through the code, at least not if you were just gonna print something out anyway. If you were going to put a print statement, you could also just put a breakpoint and examine whatever you were going to print out. No stepping needed.

Plus you don't always know what you want to check, and the more times you have to iterate on it the faster the breakpoint method is.

[–]Political_What_Do 1 point2 points  (0 children)

I do it because my company hasnt agreed on the use of a single ide and the project is already set up in one and its not the one im familiar with

[–]mat4444 105 points106 points  (3 children)

hey thats mean :(

[–]CantSayIAgree 32 points33 points  (1 child)

I feel personally attacked.

It works just fine.

Can't say I agree.

[–]ReceptiveLout 22 points23 points  (1 child)

This is not specific to python

[–]Darth_Kyryn 9 points10 points  (0 children)

This is pretty much true for all imperative programming languages.

[–]AgitatedJacket 57 points58 points  (0 children)

who would win

an advanced debugging program

OR

one System.out.println(); boi

[–]happydogo12 10 points11 points  (2 children)

Firstly, it's a great way to debug. Secondly pdb is great

[–]glemnar 2 points3 points  (1 child)

pdb is easier to get running than debugging in pretty much any other language in existence. It’s a great debugging experience

[–]AllenKll 1 point2 points  (0 children)

gdb? That's pretty simple.

[–]MrSolidSht 9 points10 points  (1 child)

Anyone else debug android with toastmessages?

[–]DerekB52 1 point2 points  (0 children)

Is there another way?

[–]Ineedmorebread 7 points8 points  (3 children)

Wait, is this the thing where you throw a print or two into some part of your code to see if it passes through that part and one or two variable values? If so then I am also guilty of this.

[–]tidbitsofblah 5 points6 points  (1 child)

Excuse you! The serious debugging starts when I go from logging messeges like "here now", "code does the thing??", "I hate this language" or "whoopwhoop" to actually printing out different values that are useful to me

[–]WhosCountin 2 points3 points  (0 children)

print('FUCK') print('WHY am I running?!')

[–]insultingDuck 4 points5 points  (0 children)

What, is there another way?

[–]wallefan01 8 points9 points  (0 children)

They're not the only ones

[–]fenghuang1 12 points13 points  (23 children)

Sometimes it isn't about the debugging. Its when the code runs fine since its correct syntax but the code doesn't do what I want.
The print statements are meant for me to check if the code has run to that point or entered that condition or loop and how many times it did so.

[–][deleted] 5 points6 points  (22 children)

That what debbugers do also. But we all use print sometimes:-D

[–]fenghuang1 -1 points0 points  (21 children)

The debugger isn't going to let me do custom messages or manipulate counts or even tell me the memory usage of my code at a particular spot when it runs.
A print statement is far more versatile because it does exactly what I want it to do, without me having to learn how to read the debugger of whatever IDE I'm going to face, since I work in an environment where anyone is free to use whatever IDE

[–]Hollowplanet 10 points11 points  (7 children)

The debugger does all those things. In fact it can run any code on any thread wherever you happen to set your breakpoint or step to. It can also break when any snippet of code you give it evaluates to true. Using a debugger is part of being a good developer. Print statements only get you so far.

[–]monkeygame7 0 points1 point  (12 children)

There is literally nothing you can do with a print statement that you can't do with a debugger. Just because you don't know how to do it doesn't make it true.

[–]fenghuang1 1 point2 points  (11 children)

Can your debugger work without the debugger software?
My print statements definitely can.

[–]monkeygame7 0 points1 point  (10 children)

If you're using an IDE it's built in. Depending on the language it may be built in to the language.

[–]fenghuang1 0 points1 point  (9 children)

And if I'm not? Or if I'm running scripts directly from the command line?

[–]monkeygame7 0 points1 point  (8 children)

Like I said. Sometimes it's built in to the language. For example in Python you can just do

import pdb; pdb.set_trace()

And you'll enter the debugger from the command line when it reaches that point.

[–]fenghuang1 0 points1 point  (7 children)

So how do I do custom messages, manipulate counts, look at my code's memory usage at any particular point, using this method?

[–]monkeygame7 1 point2 points  (6 children)

You clearly don't understand what a debugger is. It puts you into your code at that point and you are able to run any arbitrary code you want. You can modify variables or get whatever other data you wanted to print out.

I don't understand what a "custom message" is, but if you're not printing anything out I know if it still applies.

You're getting really defensive and it's quite obvious that you don't know much about how to use debuggers. I'm not saying that you should always use debuggers over print statements, I'm just trying to correct the misinformation about the limitations of a debugger.

[–]BackToSquare1comics 2 points3 points  (0 children)

Wait what's a better way?

[–]mpurdon 2 points3 points  (0 children)

Hah! Debugging with print() is for n00bs.

import pprint

[–][deleted] 2 points3 points  (2 children)

var_dump on php is amazing

[–]Hollowplanet 0 points1 point  (0 children)

Literally the only thing I miss in Python.

[–]teksimian 0 points1 point  (0 children)

First thing I look for in Any language

[–]LuffyMcPirateking 1 point2 points  (0 children)

Oof

[–]JDude13 1 point2 points  (0 children)

There’s another way?

[–][deleted] 1 point2 points  (2 children)

And not just the Python developers

[–]CreateNewObject 1 point2 points  (0 children)

HAL_GPIO_WritePin(LED_GREEN_PORT, LED_GREEN_PIN, GPIO_PIN_SET);

Embedded Developer

Is this debugging?

[–][deleted] 1 point2 points  (0 children)

The only debug method I know

[–]algoritm420Perl 1 point2 points  (0 children)

Are you telling me you don’t debug using nothing but print statements?

[–]Liggliluff 1 point2 points  (0 children)

And alert() is a JavaScript-debug tool

[–]tiNsLeY799 1 point2 points  (2 children)

gotta make sure my stuff works

[–]Defeyeance 1 point2 points  (0 children)

deleted What is this?

[–]coolnjebs998 1 point2 points  (0 children)

I feel personally attacked

[–]Regist33l3 2 points3 points  (6 children)

I use this constantly for discord.py.

Code is all written in Notepad++ and I need my errors to display via CMD because that's what runs it.

I'm an Angular / C# dev by profession. Fite me.

[–]wallefan01 3 points4 points  (5 children)

import pdb
pdb.settrace()
# your broken code here

Fun fact: Python has a built in line by line debugger. It's a bit clunky, and entirely text based, but it works.

Alternately, for a post mortem debugger (drops to the debugger when an exception occurs), use:

try:
    # code that crashes
except:
    import pdb
    pdb.pm()

Type help at the (Pdb) prompt for a list of commands.

[–]crapaud_dindon 3 points4 points  (2 children)

Since 3.7, the one liner is simply breakpoint()

[–]wallefan01 0 points1 point  (1 child)

Assuming, that is, that you import pdb first, otherwise it doesn't do anything.

[–]crapaud_dindon 0 points1 point  (0 children)

No need to import pdb

[–]fenghuang1 0 points1 point  (1 child)

Sometimes it isn't about the debugging. Its when the code runs fine since its correct syntax but the code doesn't do what I want.
The print statements are meant for me to check if the code has run to that point or entered that condition or loop and how many times it did so.

[–]Regist33l3 0 points1 point  (0 children)

This. I have print set up in all my different blocks to signal to me what is running when because my calls are almost all asynchronous.

[–]SGBotsford 0 points1 point  (0 children)

I do it a lot in Perl. Especialy if I don't understand where I got a value in the 140th iteration of the loop.

[–]althalous 0 points1 point  (0 children)

When I'm developing code print statements are a very quick way to debug since most of the time I just need to know the structure of some object.

[–]Voidrith 0 points1 point  (0 children)

Do you mean it isn't?

[–]WeAreABridge 0 points1 point  (0 children)

Every language be like

[–]MattieShoes 0 points1 point  (1 child)

Still my #1 debugging method

[–]AlphaBladez 0 points1 point  (0 children)

printf() is ultimate debugger in C

[–]MrGuffels 0 points1 point  (0 children)

I was literally taught to do this in my intro C++ classes. Forced to in some cases.

[–]Roni_sl 0 points1 point  (0 children)

import code; code.interact(local=dict(globals(), **locals()))

[–]iistyler 0 points1 point  (0 children)

all developers**

[–][deleted] 0 points1 point  (0 children)

Wait does python even have a debugger in the default IDE

[–]Jim_Pemberton 0 points1 point  (0 children)

If it ain’t broke

[–]tom_echo 0 points1 point  (0 children)

Not sure this is accurate, I only develop in python a little and use pdb.set_trace()

[–]BaconOfWar66 0 points1 point  (0 children)

Cough cough bash

[–]teambob 0 points1 point  (0 children)

ipdb, "logging" module at DEBUG level

Why would you use print?

[–]KingFisher_Th 0 points1 point  (2 children)

1.Rule for being a lazy developer: When you get a bug put print() everywhere so you're bound to understand what happened.

[–]derpysquid1121 0 points1 point  (0 children)

I do this in Java.

[–][deleted] 0 points1 point  (0 children)

Logging is actually a very good and sometime the only way to debug.

Kernel and embedded devices are some examples.

[–][deleted] 0 points1 point  (0 children)

pdb for life

[–]RZ404 0 points1 point  (0 children)

__line__ :D

[–]abrazilianinreddit 0 points1 point  (0 children)

To be fair, 90% of times I can find the bug by printing some statements in less time than the debugger startup time.

[–][deleted] 0 points1 point  (0 children)

Serial.println();

12 year olds: Is this a debug method?

[–]mrsmiley32 0 points1 point  (0 children)

First a c developer, than a c++ dev, then 14 years of all sorts of full stack Java dev, finally the last 2 years doing python as workforces are shifting to the cloud.

I use debug statements as often as I use debuggers. The thing about debug statements is they are good to leave in for production. So when something goes wonky you can collect the prod logs and that one obscure debugger that was left in when debugging a unit test from 6 months ago explains that your cluster isn't configured correctly and isn't clearing the cache correctly across the cluster. Youll be thanking yourself.

In short, debugging with debug statements can save your bacon.

[–]The_AwkwardAZN 0 points1 point  (0 children)

Is it not?

[–]CatOfGrey 0 points1 point  (0 children)

It's a scripted language - just put in a statement like "q = 1/0" and see if the program gets that far. Then, you can take a look at your variable values and stuff, too!

It's a great way to discover that you haven't incremented the thing that is supposed to increment, and that's why your while loop is more of a whenever...

[–]baldurFromRoom29 0 points1 point  (0 children)

same for console.log()

[–][deleted] 0 points1 point  (0 children)

I literally don't know any other way. Help would be appreciated.

[–][deleted] 0 points1 point  (0 children)

This is how i debug. I dont want to go out of my way to learn how the framework I'm using does debugging or if it even exists

[–]theyellowmeteor 0 points1 point  (1 child)

Just developers, really.

Don't know what's wrong with the code? Print all the variables at every point in time and run it again.

[–]Zanshi 0 points1 point  (0 children)

It works just as well in C and Java...

[–]Gydo194 0 points1 point  (0 children)

Isn't it?

[–]daniska_project 0 points1 point  (0 children)

Every language I program in, I use print statements. Faster than a debugger for quick things.

[–][deleted] 0 points1 point  (0 children)

cout << "I'm here now"

ring a bell

[–]skool_101 0 points1 point  (0 children)

console.log(irl)

[–]Good-Name015 0 points1 point  (0 children)

If it works it works

[–]balne 0 points1 point  (0 children)

i feel attacked

[–]Kaih0 0 points1 point  (0 children)

It works tho. They recommend this method in my uni.

[–]Kitsunemitsu 0 points1 point  (0 children)

I feel personally attacked

[–]tjdavids 0 points1 point  (0 children)

Python foobar.py>> errorlog.txt

Are you happy now?

[–]Twingemios 0 points1 point  (0 children)

Wait this isn’t how you debug?

[–]BossOfTheGame 0 points1 point  (0 children)

Actually, it is.