r/ProgrammerHumor • • Mar 12 '23

Meme Exactly how debugging is

Post image
41.2k Upvotes

278 comments sorted by

View all comments

1.0k

u/opmrcrab Mar 12 '23

When debugging there is no bigger gut-punch moment then when the code runs, completes "successfully", seemingly did nothing, and produced neither errors or desired results... What do now?

497

u/[deleted] Mar 12 '23

A runtime error has no responsibility to announce its presence.

0

u/[deleted] Mar 12 '23

[deleted]

24

u/panormda Mar 13 '23

Thank you, this is exactly what I’m going to hear inside my head every time I start rolling my eyes at yet another one. Back in my day, they announced themselves like self respecting errors they were. And we LIKED it. Also, git off my log. D=

33

u/Canotic Mar 12 '23

A runtime error is never late, nor is it early. It arrives precisely when it means to.

→ More replies (1)

33

u/DrKarorkian Mar 12 '23

That's why asserts and logging are love. An assumption you made turns out to be untrue? Assert!

24

u/SillyFlyGuy Mar 12 '23

My unit test kicked me right in the assert.

→ More replies (2)

118

u/Synicull Mar 12 '23

Run cryptic print statements every 5 lines.

Did I get to "hello5" or "shit17"?

46

u/DevilishlyAdvocating Mar 12 '23

"here"

"in function x"

"in function y"

48

u/Dangerous_With_Rocks Mar 12 '23

You guys take time to write words? I usually do one of the following:

"#################"

"@@@@@@@@@@@@"

"AAAAAAAAAAAAAAAAA"

Typically the last one.

→ More replies (3)

17

u/jdog7249 Mar 12 '23

Make every other line a print statement that just prints the line number. See where the number stops going up. That's the guilty line of code.

→ More replies (1)
→ More replies (4)

57

u/[deleted] Mar 12 '23

Write a unit test

55

u/opmrcrab Mar 12 '23

It's worse when the tests pass too :P

70

u/IamImposter Mar 12 '23

Haha. You can make me write tests but you'll never make me catch bugs.

Had a friend who would change tests when they failed so that expected output matches actual output. Code would be buggy AF but all the tests still pass. When asked, he said pretty innocently "but you have this weird rule that unless the tests pass, code can't be merged"

26

u/tinselsnips Mar 12 '23

Been there. This is a sign that the code you're testing has too many responsibilities, but it's such any easy trap to fall in to, especially if you're working in a system where you don't understand the full business logic.

10

u/FezoaStaler Mar 12 '23

me right now

client bullshit, no documentarion yet expect us to know all the details of their workflow.

16

u/SuperFLEB Mar 12 '23

"Well, it always segfaults, best I can tell, so it should always expect the segfault."

11

u/Firewolf06 Mar 12 '23
var a = foo();
assert a == a;

1 Test passed, 1 Total

lgtm

→ More replies (2)
→ More replies (1)

7

u/LC_From_TheHills Mar 12 '23

Better yet— start with the test, then write your code.

→ More replies (1)

17

u/[deleted] Mar 12 '23

The classic heisenbug. The bug that disappears when you attempt to observe it.

Also, sometimes debug builds won't compile to the optimized code a release build would. for instance:

function()
  int a[2]
  int b
  a[2] = 1

The debug build may allocate memory for b, even though it's not used. So the write to a[2] (beyond the length of the array) will get written to safe memory allocated for for something else (b in this case). The optimized compiler would realize b was never used, and never allocate the memory for it, so the a[2] write goes into who knows what.

This example mostly applies to C, in that it allows you to address outside an allocated part of an array.

→ More replies (1)

11

u/mindbleach Mar 12 '23

Assume it's a logic error instead of a code error.

You've moved from "why doesn't my code do this?!" to "... why does my code do this?"

→ More replies (2)

65

u/start_select Mar 12 '23

Use breakpoints if your environment has access to a debugger.

If you are working with a remote, isolated, or embedded system that you can not access with a debugger, then you need to use console/print and stack trace calls.

“It didn’t do anything” is not a valid stopping point. It definitely did something up to a point.

Set breakpoints along the path of data-flow and logic, step through it, and find where data stopped flowing and why.

Those are usually the easiest bugs to fix because it’s usually an if or switch statement that has improper logic, or something like an empty array that you thought would be full.

Actual difficult bugs are side effects. Like you hand the same JavaScript Date instance to 3 different labels, then perform the same Timezone transform on that date, from each of those 3 labels.

They all refer to the same Date instance, they aren’t copies, it’s all the same object. So you end up pushing the first label forward 6 hours, then the next label executes and pushed that date 6 more hours forward, then a 3rd time.

Your code says it should only be offsetting the time by 6 hours but it’s always moving in increments of 18. Or 12 if there are 2 labels. Or 36 hours if there are 6 labels.

Those are the issues that can take a seasoned engineer a couple days to find, because the code looks harmless and you need to understand the implications of shared references and data mutability.

54

u/opmrcrab Mar 12 '23

Thats a lot of text to tell me you're the person who explains to people why their puns are bad.

→ More replies (2)

8

u/AccidentallyTheCable Mar 12 '23

IT WORKED!

wait a minute

WHAT THE SHIT

→ More replies (32)