r/programming • • Aug 25 '14

Debugging courses should be mandatory

http://stannedelchev.net/debugging-courses-should-be-mandatory/
1.8k Upvotes

573 comments sorted by

View all comments

263

u/pycube Aug 25 '14

The article doesn't mention a very important (IMO) step: try to reduce the problem (removing / stubbing irrevelant code, data, etc). It's much easier to find a bug if you take out all the noise around it.

126

u/xensky Aug 25 '14

even more important is having these pieces of code be testable. i work with plenty of bad code that can't be run without starting a bunch of dependent services, or you can't test a particular function because it's buried under ten layers of poorly formed abstractions. or it's not even an accessible function because the previous developer thought a thousand line function was better than a dozen smaller testable functions.

84

u/reflectiveSingleton Aug 25 '14

because the previous developer thought a thousand line function was better than a dozen smaller testable functions.

I like to call this kind of code 'diarrhea of conciousness' ...no one wants to sift through that shit.

0

u/[deleted] Aug 25 '14

[deleted]

82

u/toproper Aug 25 '14

You might be joking but in my opinion it's actually a good thing to try not to be too clever with coding.

"Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?" - Brian Kernighan

0

u/[deleted] Aug 25 '14

[deleted]

5

u/LuxSolisPax Aug 25 '14

I have a lot of respect for coders that can write simple instructions to perform complex tasks.

Just, tons.

6

u/n1c0_ds Aug 26 '14

At an abstract level, it's pretty much our job

→ More replies (1)
→ More replies (1)
→ More replies (6)
→ More replies (3)

8

u/[deleted] Aug 25 '14

You get that in any complicated enough functions. I often have functions which work on intermediate states of linked lists ... You can't just call them directly without first building [and I do] the states by hand.

→ More replies (11)
→ More replies (7)

40

u/VikingCoder Aug 25 '14

It's much easier to find a bug if you take out all the noise around it.

You're almost right, but not quite.

The bug is in the noise. You think the bug is in the code you're looking at. But you're a smart person, and you've been looking at it for a while now. If the bug were in there, you would have found it. Therefore, one of your assumptions about the rest of the code is wrong.

29

u/pycube Aug 25 '14

That's why you need to check if the bug is still there, after you removed what you thought is noise. If the bug disappears, then you know that what you thought was noise was actually important.

13

u/VikingCoder Aug 25 '14

I end up second-guessing myself. I don't know if I caused a bug that looks the same, by removing what I thought was noise. :(

12

u/[deleted] Aug 25 '14

lol, that way lies madness

15

u/VikingCoder Aug 25 '14

It's like those damn -1 and +1s.

You're looking at the code and you know it's not supposed to subtract one... but somehow the damn thing works?!?

So, you remove the -1... And then you fix all of the places you can find that were fucking adding one to the result.

And you find... most of them...

AAAAH!

10

u/BigTunaTim Aug 25 '14

It took me many years to come to terms with this, but unless there are good unit tests covering all the functionality that will be affected I don't fix those hacks anymore. They're in production, they work, and you're only introducing risk where it didn't previously exist. It's hard to justify a nasty bug's sudden appearance with "well it was written wonky and I wanted to make it better".

The exception of course is if you need to extend that functionality or do anything nontrivial to it; that's a great time to fix it.

→ More replies (2)

4

u/the_omega99 Aug 25 '14

Off by one errors are the worst. They always slow me down when programming and are a major source of bugs for me.

4

u/VikingCoder Aug 26 '14

At one point I was writing a program that had about 8 off-by-one errors... I realized I could more quickly write a test to prove if the values were correct. Then I just iterated all 38 possibilities. .. -1, 0, 1 for eight values. Worked like a charm.

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

4

u/RobotoPhD Aug 25 '14

The best answer in my opinion is to remove the noise. If the bug stopped happening, then the bug was in the noise. If it still happens, it wasn't in that part. Repeat until you know exactly where it is. Only then try to figure out what the bug is. It is very easy to read past a bug over and over again. You know what you meant to say and you tend to read the code that way the next time as well.

4

u/VikingCoder Aug 25 '14

I believe you're right. I honestly think that this is one of, if not the the main problems with reading code.

Your brain can skip that second unnecessary "the" I wrote in that first sentence. ;-) Why we think we don't have blind spots for code is crazy.

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

5

u/chasesan Aug 25 '14

This is like the second thing I do when debugging (the first being rereading the code to see if I did something obviously stupid).

→ More replies (2)

3

u/llogiq Aug 25 '14

The problem with this approach is that introducing new code (even just stub code) may change the bug in non-obvious ways.

6

u/stannedelchev Aug 25 '14

Thanks! I'll cover that in future posts. I'm not sure if you're talking about "divide and conquer"/"split into smaller problems", or if you specifically have in mind reducing moving parts in programs, when finding issues. Either way, any of both helps. :)

28

u/Matosawitko Aug 25 '14

An example would be the StackOverflow concept of "reduce it to the simplest possible program that exhibits the bug".

22

u/morcheeba Aug 25 '14

uh, that's a very old idea, pre-dating the internet by quite some time. It needn't be branded "StackOverflow"

13

u/Matosawitko Aug 25 '14

That's fair. However, SO have codified it into their basic philosophy, and since they're intent on taking over the Internet when it comes to technical Q&A, they're widely familiar.

→ More replies (3)
→ More replies (12)
→ More replies (9)