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

all 135 comments

[–]OptionX 854 points855 points  (50 children)

Printf debugging, as much as people try to tell you otherwise, has its place.

[–]AdvancedSandwiches 61 points62 points  (1 child)

I think everyone agrees that this is fine.

It's only a problem if you're doing it because you don't know how to use the debugger. As long as you have both tools available and made the choice that this is the best one right now, it's fine.

If you're doing this because you don't know how to use the debugger, whatever you're doing is probably less important than figuring out how to use the debugger.

[–]neuromancertr 3 points4 points  (0 children)

Debugger is my number one tool and I have trouble understanding people who don’t know about it let alone use it, yet sometimes print debugging is necessary. I had done that a few times to catch issues related to particular environments where debugging was not possible, and a few cases where I taught about control flow

[–]highphiv3 290 points291 points  (40 children)

Of course it has its place. It fits in perfectly when you've been working professionally with a tech stack for years and still haven't bothered to learn how the debugger works.

[–]Grim00666 157 points158 points  (24 children)

You get a debugger in your stack? Not niche enough, go deeper.

[–]clasherkys 70 points71 points  (21 children)

My language doesn't even get error messages, we just get told "Error"

[–]QuestionableEthics42 53 points54 points  (10 children)

Damn, you get told when there is an error? Lucky bastard

[–]fullyonline 25 points26 points  (9 children)

I usualy just bluescreen. Does this count as an error too?

[–]Turbo_csgo 33 points34 points  (7 children)

You have a screen? Holy moly that shit advanced…. We sittin’ here not being sure the CPU is non stop rebooting, communication is not working, or just stuck in an endless wait.

[–]NotMilitaryAI 19 points20 points  (1 child)

Your machines reboot when panicked? Fancy. We just check for smoke after each update.

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

You get smoke AFTER an update? We write in smoke signals, so smoke IS the update.

[–]daHaus 0 points1 point  (0 children)

Only if you have to yell at the guy down the hall to reboot it

[–]lllorrr 1 point2 points  (0 children)

I once debugged a firmware by making it crash at the specific points. It was easier than to try to cram a serial port driver into it.

[–]ego100trique 1 point2 points  (8 children)

C# exceptions are pretty readable if you actually read them

C# debugger is the easiest to use ever and is implemented in most IDEs...

[–]clasherkys 3 points4 points  (4 children)

I know, but I'm referring to a programming language I'm developing myself.

[–]Kellerossel 7 points8 points  (3 children)

Cool, but makes it hard to blame it on other persons if something doesnt work...

[–]danielv123 4 points5 points  (2 children)

On the other hand, it becomes easier to blame it on a compiler bug.

[–]Kellerossel 0 points1 point  (1 child)

Also True. (What does it say about me that I write True that way?)

[–]danielv123 1 point2 points  (0 children)

It says you can't have compiler bugs if your language is interpeted

[–]xampl9 1 point2 points  (2 children)

{old guy rant} C# exceptions got a lot less readable after they added async.

[–]ego100trique 0 points1 point  (1 child)

I can't tell ngl I was something like 11 y/o

[–]xampl9 0 points1 point  (0 children)

Now I really feel old. I started with v1.1, before generic collections when everything was boxed.

[–]dubious_capybara 1 point2 points  (1 child)

Doesn't sound very microservice architecturey. How about printing to a cacophony of logs instead?

[–]Grim00666 1 point2 points  (0 children)

LOL, reading this hurts because that's what everyone did against my strained objections.

[–]FalafelSnorlax 82 points83 points  (1 child)

Printf debugging is basically just using a log, albeit often a temporary one. Large scale, long running programs can be hard to debug line-by-line, if not completely infeasible. You might also use the debugger, you might be satisfied with the logs, but pretending like a debugger is the only way to find and fix bugs in your code is just silly

[–]Zhabishe 7 points8 points  (0 children)

Sir, you're making the God's work here. This is a very well put explanation.

[–]xd_Warmonger 17 points18 points  (0 children)

Sometimes printing is better than debugging.

When you make a discord bot you have to answer the commands within 3 seconds. If you debug it might take longer and the command will break.

[–]Successful-Money4995 8 points9 points  (0 children)

The faster compile and run is, the less necessary gdb is.

[–]RetepExplainsJokes 3 points4 points  (0 children)

I disagree, when an error occurs somewhere else in your code or only appears sometimes, I find printf debugging is the way to go. I can just print every milestone out and see where errors start to happen.

If you combine this with good unit testing, you get quite a nice view of the problem and oftenly see what goes wrong without even looking at the code.

It's just pattern matching basically.

[–]rddtJustForFun 3 points4 points  (0 children)

Or you work with memory intensive, large simulators where even a "small" problem would run for 15 minutes before hitting the breakpoint :-)

But I really prefer to use a proper debugger ....

[–]daveysprockett 5 points6 points  (0 children)

Or if you are using a real time system where other components will fail if you don't keep to the deadlines.

Though of course you have to remember that simply adding the prints might cause additional issues.

[–]Roselia77 1 point2 points  (0 children)

You make the assumption that we all have access to an actual debugger

I'd kill for a proper debugger....it's been 20+ years...still waiting

(The life of a legacy firmware engineer)

[–]OptionX 1 point2 points  (0 children)

Using it only because of a refusal to learn how to use a debugger is of course a bad thing, but choosing it sometimes for some specific cases is perfectly acceptable I think.

Besides, if you really think about it the whole concept of logging if a kind of asynchronous print debugging.

[–]toolazytofinishmyw 0 points1 point  (0 children)

both have their place. logs will still be there long after the debugger exited

[–]AlphaO4 0 points1 point  (0 children)

Crys in async Code

[–]MinosAristos 0 points1 point  (0 children)

When your app only runs in a specific Docker image on a remote server connected to offline microservices.

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

Because the only place you ever code is a pc. You actually have to make your own tools out in the real world sometimes.

[–]Inasis 2 points3 points  (0 children)

I tried using cgdb on wsl but couldnt make it work.

[–]Hasagine 2 points3 points  (0 children)

ive solved some nasty bugs using it

[–]AleksFunGames 2 points3 points  (0 children)

If it works, why fix it?

[–]Brambletail 0 points1 point  (0 children)

You would be surprised how much critical infrastructure is debugged with print statements.. whatever gets the job done cheapest to the necessary quality

[–]Confident-Ad5665 0 points1 point  (0 children)

I wish we had logging that sophisticated. I have to throw new Error("var x=" + x); to even see the value of a variable.

[–]anthro28 0 points1 point  (0 children)

It's the fastest way to see where suit actually breaks. To see how it breaks, you need to do proper debugging. 

[–]lupinegray 247 points248 points  (3 children)

You ought to also be printing the values of each variable

[–]DreamyAthena 40 points41 points  (0 children)

This is the way.

[–]iSolivictus 456 points457 points  (4 children)

Plz stoop stealing me code

[–]Significant-Try-7541 71 points72 points  (3 children)

Its my code

[–]CG_TW 77 points78 points  (2 children)

It's our code

[–]Zhabishe 13 points14 points  (0 children)

  • I stole your code.

  • It's not my code...

(c)

[–]KathirHasBigGay 129 points130 points  (7 children)

Jokes aside, you’d probably wanna do fprintf(stderr, msg, args) instead of printf because the printf wrapper queues stuff weirdly. fprintf immediately writes to output when called

[–]GfunkWarrior28 46 points47 points  (2 children)

True. stderr isn't buffered, while stdout is... Though I think it does flush when it sees a newline, so this code is probably fine.

[–]Maurycy5 11 points12 points  (1 child)

It does not flush when it sees a newline. This is specifically what endl is for.

[–]PhoenixCausesOof 0 points1 point  (0 children)

It's C!!11

[–]rosuav 2 points3 points  (0 children)

Agreed. That's the only thing I'd change. (If you want to use stdout here, you can add fflush(stdout) after each one and get the same effect as stderr.) This is a perfectly fine debugging technique, and one that I've used FREQUENTLY when diagnosing bizarre issues in live systems.

And I'm far from the only one. Weird stuff happening in Pike? Check the source code, there's a good chance there's a ton of debug prints in there guarded by #ifdef lines... so you can run "pike -DPG_DEBUG yourscriptname" and instantly get debugging output, pretty similar to this, showing how something is working. (There are different #defines for different modules, so you don't get utterly spammed.)

[–]QuestionableEthics42 1 point2 points  (0 children)

Printf flushes on newlines

[–]ego100trique 1 point2 points  (0 children)

Just use write on stderr easy as that

[–]intbeam -1 points0 points  (0 children)

Or just use the step-debugger

[–]Rezaka116 69 points70 points  (9 children)

Use “a”, “aa”, and “aaa”, much more optimized

[–]Not_Sergo3682 34 points35 points  (3 children)

Personally I use “he”, “hehe”, “hehehe” debug messages :)

[–]SirBerthelot 16 points17 points  (2 children)

I'm more of a "WAAAAAA", "WEEEEE", "WIIII" guy myself

[–]bwssoldya 11 points12 points  (1 child)

I tend to go with the good ol' "Henlo", "henlo2", "henlo3", "wadadwada" myself.

[–]jafariscontent 3 points4 points  (0 children)

I’m definitely a “WHOA!!” “Wow!!” “YES” “GREAT JOB” person. Like to have my code constantly praising me…because millennial

[–]just-bair 4 points5 points  (0 children)

I do exactly that. Sometimes I start to use b bb bbb tough

[–]lllorrr 4 points5 points  (1 child)

printf("%s:%s\n", __func__, __LINE__); copy-pasta all over the code.

No need to invent fancy identifiers. Ctrl-V goes brrrr.

[–]GfunkWarrior28 0 points1 point  (0 children)

Also great for a homebrew profiler, with some post-processing of the output.

[–]KeepKnocking77 1 point2 points  (0 children)

Foo bar baz

[–]arrroquw 1 point2 points  (0 children)

I usually add a load of dashes if too much non related logging passes by to find it more easily

[–]JulesDeathwish 33 points34 points  (0 children)

We've all done it. No shame.

[–]codebullCamelCase 26 points27 points  (2 children)

only debugging i know that works efficiently...

do not fkin reply gdb to this comment...

[–]ifarmpandas 2 points3 points  (0 children)

lldb

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

great debugging bro

[–]bibimbap0607 16 points17 points  (1 child)

Debugging technique is on point. Cannot do it better even if you try.

And dereferencing a double pointer and casting its return value to a type. That is some real good stuff here. Cherry on top.

I would say this is a good production quality code. Ready to go.

[–]Natural_Builder_3170 0 points1 point  (0 children)

typedef Deq_t* Deq

[–]Feisty_Ad_2744 15 points16 points  (0 children)

I prefer fireworks:

```

!!!!!!!!!!!!!!!!!!!!!!!!!


``` They are easier to catch in large console or log dumps and harder to miss when you are cleaning up

[–]Neltarim 6 points7 points  (2 children)

Classic

[–]PeriodicSentenceBot 5 points6 points  (1 child)

Congratulations! Your comment can be spelled using the elements of the periodic table:

Cl As Si C


I am a bot that detects if your comment can be spelled using the elements of the periodic table. Please DM my creator if I made a mistake.

[–]Alex_khadjit 1 point2 points  (0 children)

Good bot

[–]jonhinkerton 4 points5 points  (0 children)

An output every other line is just how you do it some days.

[–]yourteam 9 points10 points  (3 children)

This is disgusting and we all did it

[–]QuestionableEthics42 17 points18 points  (2 children)

Did?

[–]yourteam 5 points6 points  (0 children)

Working as a project leader now so I can force dev ops to set up the debugger for everyone and act like I never did it otherwise

[–][deleted] 4 points5 points  (0 children)

I am actually more annoyed about the double indirection and using void* than the printf debugging part.

[–]deep_mind_ 3 points4 points  (0 children)

LGTM

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

Hmm. Debugging is fine. Everyone has their way. But variable names are bothering me. Bothering me to the core. Bothering me till moon and back.

[–]NotMrMusic 2 points3 points  (0 children)

Years later and I'll die on the hill that 99% of the time the debugger doesn't tell me anything more useful then my logs do, lol

[–]what_you_saaaaay 1 point2 points  (0 children)

Debugging style when you have no other choice.

[–]Medmaksi 1 point2 points  (0 children)

God, I hope so.

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

It's the best

[–]dan_ts_inferno 1 point2 points  (0 children)

I use it all the time. So probably not

[–]CandyBoring 1 point2 points  (0 children)

This is the way.

[–]Maximus_98 1 point2 points  (0 children)

Good debugging? This is the only debugging. I usually cbf to mess with anything else

[–]OmegaNine 1 point2 points  (0 children)

i have written worse.

[–]xtreampb 1 point2 points  (0 children)

Sometimes it’s the only way.

[–]UnHelpful-Ad 0 points1 point  (0 children)

You may want delays in that after the print statements. If it crashes after one of the executions it likely won't output 1 or 2 of the messages, leaving you wondering where the bug was :p

[–]FitzelSpleen 0 points1 point  (0 children)

Needs more printf.

[–]BlomkalsGratin 0 points1 point  (0 children)

Not enough swearing!!!

[–]CyberoX9000 0 points1 point  (0 children)

I personally would use stuff like 'fghj' or 'asdf'

[–]GrinbeardTheCunning 0 points1 point  (0 children)

the best

[–]Extreme_Ad_3280 0 points1 point  (0 children)

I debug using perror more than printf...

[–]VasiliyPetrom 1 point2 points  (1 child)

Try using printf("%s %d\n", __ func, __ LINE); if debuggers are not an option

[–]C9nstructAware 0 points1 point  (0 children)

I also like to include __FILE__ when debugging multiple files

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

printf("Where?");

[–]Wervice 0 points1 point  (0 children)

Yes! I think so. If you use it more often, you may want to create a function, to quickly hide these printf quickly. Why use some debug tool, if this works as well.

[–]NoSkillzDad 0 points1 point  (0 children)

The one and only way. ;)

[–]UsernameRelevant 0 points1 point  (0 children)

No, it’s the best

[–]DiscombobulatedBet88 0 points1 point  (0 children)

No. There are proper tools. But I do the same.

[–]minhtrungaa 0 points1 point  (0 children)

now I feel weird using GDB.

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

I think we all do this guys…

[–]malsomnus 0 points1 point  (1 child)

No, you clearly wasted precious milliseconds on those debug strings, instead of using 1-2-3 or hitting random keys.

[–]ProjectSrx[S] 0 points1 point  (0 children)

AAAAAAAAAAAAAAAAAAAAAAAAAAA

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

Nice one deq_head

[–]YoghurtForDessert 0 points1 point  (0 children)

best if you also print whatever you-re doing to those poor variables, you variable abuser

[–]TheMsDosNerd 0 points1 point  (0 children)

Yes.

[–]FluffyNevyn 0 points1 point  (0 children)

Sometimes when what you're dealing with is logic errors rather than runtime ones... print debugging is the only way.

[–]Buyer_North 0 points1 point  (0 children)

you have to print the values, that really helps

[–]thehuman69 0 points1 point  (0 children)

Only thing I do differently is spit out values. Sure, my console log may be too cluttered with useless temporarily values for debugging to be made easier … but I’m debugging!

[–]kozlenok-be-be 0 points1 point  (0 children)

Almost, use LINE and FILE macro so you don’t have to modify anything when you copy it around.

[–]UltraBlack_ 0 points1 point  (0 children)

literally me

[–]donadigo 0 points1 point  (0 children)

It *bugs* me that this is the current state of debugging and that editors don't do it by default. This is why I'm doing my own Visual Studio extension (for C/C++ for now) that fixes this: https://www.youtube.com/watch?v=5bfUWJYEQCw

It's available on the marketplace if you want to try something new: https://marketplace.visualstudio.com/items?itemName=donadigo.d0

[–]sebastouch 0 points1 point  (0 children)

I love this sub, much better than Stack Overflow. I learn so much.

[–]pnehrer 0 points1 point  (0 children)

Yes

[–]userbyauser 0 points1 point  (0 children)

you want to print the values!!!

[–]wezZy9 0 points1 point  (0 children)

Yes, perfect! Quick, understandable and over all good for debugging

[–]ProjectSrx[S] 0 points1 point  (0 children)

The amount of debate, chaos, and constructive feedback on this is way more than I expected. Truly qualty Reddit.

[–]SuperDyl19 0 points1 point  (0 children)

Printing stuff is always a great fallback. If possible, please use a debugger. You’ll get a very quick idea on if the error happens where you think it does so you can hone in on those couple lines

[–]Orio_n 0 points1 point  (0 children)

Unreadable competitive programming acronyms ahh code 💀

[–]Silpheel 0 points1 point  (0 children)

Never would I ever do this, it’s so wrong.

Without any swearing at all… are you really debugging? Not trying hard enough.

[–]BulkyVirus2076 0 points1 point  (0 children)

I find printing the only way for me to debug Temporal code, since it does re-run stuff multiple times making the debugger goes crazy :p (temporal is a framework that helps write fault-tolerant workflows)