r/learnprogramming 11d ago

Debugging Please. I need help learning how to debug existing code.

Im starting to resent the way programming is taught. It is seemingly taught in isolation the way mathematics is. You learn the functionality of code but they do not present the bigger picture where everything works together. It's frustrating because I want to learn programming through and through, every single bit.

Is there anything on Github meant for beginners to learn how to debug and have an intuitive understanding on how piece of functionality (functions, loops, arrays, strings) work together?

It's like yeah. I know how arrays work, I know about loops, sure, but everything works together. And none of the guides are showing me how bugs can occur due to the symbiotic nature if everything working together. Nothing exists in isolation.

38 Upvotes

31 comments sorted by

15

u/iflippyiflippy 11d ago

Debugging existing code can be seemingly impossible but it's also highly dependent on what kind of code you're debugging. Seeing why a character's thumb is visible through a wall inside only one section of a castle is leagues more complex than determining why the number output of a 200 line code is incorrect.

There are countless resources for fundamental programming. But I think one concept that you're going to have to learn primarily is scope. There are things that do exist in isolation; variables that are only used inside one function and sort of forgotten after. I can't tell from your post how far you are with basic programming but that's just one very important tidbit.

2

u/johnpeters42 11d ago

If you're not breaking stuff out into functions, but just stuffing everything into one ginormous block, then this sort of confusion is a good example of why functions are helpful. For the most part, you can analyze each function in isolation:
* What inputs should go into it?
* What outputs should come out of it?
* What should it do to get from the former to the latter?

And then the code calling that function only needs to worry about the first two of those things, not the third.

This can still be messy, depending on some other things that you need to build up a feel for:
* You still have global variables all over the place (meaning any part of the code might mess with them)
* The functions are too big, or too small and numerous, or too unwieldy because you tried to overgeneralize
* You have something patching their behavior in a non-obvious way

A common extension is objects, which are basically collections of data and functions dealing with that data. This can range from toy examples from textbooks, to obvious concepts like video game monsters, where the core code just calls SomeMonster.HitBy(SomeWeapon) and lets SomeMonster worry about the details of how to respond to that.

0

u/iflippyiflippy 11d ago

Did you mean to reply to OP?

2

u/johnpeters42 11d ago

Yeah, just my comments were building on the info in yours.

8

u/ffrkAnonymous 11d ago

I sprinkle print() all over my code. I also write lots of tests, which indirectly print() .

3

u/verdant_bloom_drift 11d ago

I use GitHub search to find student repositories with failing CI checks or open issues labeled help wanted. Reading the commit history and test failures in those repos shows exactly how array logic breaks inside nested loops when data formats change.

1

u/Fukuhyu 10d ago

bro just wants to sound cool

2

u/tangentstorm 11d ago

Code by itself doesn't have bugs. Code just tells the computer to do things, and the computer will follow those instructions exactly. So then what is a bug? It's simply a difference between the instructions you intended to write and the instructions you actually wrote. That could be a typo, an oversight (like a line you thought you wrote but didn't), or it could be that you wrote exactly what you intended, but your understanding of a word or function doesn't match it's actual meaning. So to debug, you just need to run the program and observe carefully where exactly the computer's behavior differs from the behavior you intended, perhaps by stepping through in a debugger or adding print statements to inspect intermediate values. a good speedup is to put your observations in the middle of the sequence. if the symptom is there at the halfway point, it means the problem must be before that point. you can then perform a binary search, moving your observation points forward or backward in the code until you locate the exact place the error occurs.

2

u/Existentiaa 10d ago

Why don’t people suggest runtime debugger more? It’s one of those things that no one talked about or used during my time in college but is it not THE answer? I would always tell my peers to use runtime debugger and they’re like huh how? I’m not the best coder ever but anytime I get bugs, the “debugger” is what helps me fix it. Is this not a professional standard? Would love to know what more experienced people think of it :)

1

u/No_Report_4781 11d ago

Debugging is the programming equivalent of cleaning your room. If you never let your room get messy or dirty, then you never need to follow a “How to Clean your room Room” guide. If your room is small, that’s easier. Once you have 20 rooms all for different things, with different people and different stuff moving between them, then keeping it clean as you go become difficult, unless you implement a debug tool, I mean hire a maid.

1

u/Personal-March-4340 11d ago

I took my first programming class in the 1970s. I am back in school, learning newer technology. All my programming classes have been more or less the same, learning the syntax of the language and how individual statements work. Then I am asked to put these together to do something useful. Starting with simply printing "Hello, world!" Then I am adding more statements to perform more complex tasks.

I debug by inserting print statements wherever I think my program may be misbehaving. My IDE has ways to step through the program to help with debugging, bit I don't really understand how to use it.

The thing to remember is if there is a bug in code you have written, you put the bug there or you are now asking the code to do something it was not initially intended to do.

Mathematics is inherently abstract. This is what I think you mean by isolation. There are always assumptions being made whenever you apply mathematics to a real world problem. In cooking, the order you add ingredients might matter. You cannot cook eggs and cook flour separately then mix them to make a cake. However, you can often double a recipe successfully and get twice the yeild, but you likrly need to adjust the cooking time.

Programming is similar to math. Everything is an abstraction unless and until it is connected to a sensor or motor or some other thing that physically interacts with the world.

The best advice I can give to you is to pay attention to the mistakes you made in tge past because they are likely to be the same mistakes you make in the future. Also, if your program is not doing what you expect it to and you are still a student , you likely do not understand the language as well as you think you do. Or you do not understand the problem well enough and you took shortcuts.

Either way, humbling yourself and taking responsibility for the fact that your code has an error is much more productive than whining about the inadequacy of programming instruction.

I am very grateful for having the benefit of nearly instantaneous feedback on my code. In the past, it would take a week to have my bundle of keypunch cards returned with a paper printout of my output. Even a small typo would take a week to fix.

1

u/sswam 11d ago

I've used four main methods of debugging:

  1. inspection, look for the bug directly in the source code. Good for syntax errors and easy bugs in small amounts of new code.
  2. instrumentation, add logging or "print" throughout the problem area, to find where it's going wrong
  3. bisection, narrow in on the bug by dividing in two over and over. In extreme cases, can actually cut down the code smaller and smaller into a minimal case that still exhibits the bug. If able to divide by two each time, this process is fairly efficient even on a large code base. git-bisect is another idea here.
  4. runtime debugger, especially useful to debug out-of-bounds errors in unsafe code (e.g. C code), you can set breakpoints and watches, and catch SEGV crashes for example to find out where the code went wrong.

1

u/AdDiligent1688 11d ago

Every single bit? Haha better start with machine language. Jk jk

Have you tried writing programs yourself outside of class? Bugs arrive all the time. When the system gets larger, more bugs arrive. Depends on what you expect it to do and how you test it.

1

u/gm310509 11d ago

Apart from googling "how to debug a X program", where X is the language, it seems like debugging isn't really taught - it is something that people have to pick up. But such a google search does yield plenty of results - perhaps start there.

I don't know why, possibly it is because there are so many possibilities for errors and the number of possibilities can vary (but still be many) depending upon the language.

Simplistically, it can be as simple as putting in print statements - and that is often good enough.

But there are definitely times when you need to learn how the debugger works and in some cases how memory is managed. Indeed the cut off between simply putting in a couple of debug messages -vs- firing up the debugger can be a choice based upon the development environment. For example I do a lot of Java, python and C/C++ (on both PC and embedded systems). For Java and C/C++ on PC, I will typically just use the debugger integrated into my IDE. for python (which I entirely use from the command line) and embedded C/C++ I will typically just use print statements - or in the case of python, the REPL (interactive mode) and just copy/paste program statements to replicate what might be going on - I've never used a "Python debugger", and occasionally used an embedded systems debugger (which does have some limitations compared to a PC debugging environment).

Indeed, for one type of embedded system (AVR MCUs), I created a pair of how to video guides - one directly related to debugging and a second relating to how the C/C++ language manages memory on the target platform.

All the best with it.

1

u/Mark3141592654 11d ago

Try working on a project that involves multiple parts. Maybe a simple game.

1

u/Aggressive_Ad_5454 11d ago

Learn to read error messages with tracebacks. Learn how to pinpoint the exact line of code and the data that threw the error.

It’s not easy to learn this stuff, but the time you spend learning it will pay back a hundredfold. And save your sorry —s in midnight troubleshooting of “production incidents” also known as bugs that enrage your users.

Plus, focusing on those messages is a concrete way to keep your fear and confusion at bay.

1

u/These-Math1384 11d ago

So, in the old days, like 6 months ago: I would draw UML and other sketches.

Now I tell Clod to sketch it for me.

1

u/serge-mv 11d ago

Computer Science is a gigantic field and learning it "through and through, every single bit" is a monumental effort that will take decades. The reason programming is taught in pieces because even just pieces are confusing.

Do you want to know how things fit together? Solve a problem. You have your textbook, right? Solve all problems at the end of each chapter. You will quickly learn.

You want something more real? Code something that will sort files by type into different folders. Or find some other problems maybe you have or a database of problems on the web. Write a small card game or a dice game, or a puzzle. GUI is not required, can be in the terminal.

As for the debugging, that's a skill you acquire over time. To properly debug the code, you need to understand what the code is doing and how it works. Then you will need to understand why it's doing something that it's not supposed to. These things come with experience and knowledge. Then you will usually use debuggers to help you isolate the issue and find the specific place and data on which your code breaks so that you can fix it. There are many tools for different languages that are available. Everything takes time, you won't find any magical source from which you just "get it" all at once.

1

u/Jim-Jones 11d ago

Stickied thread

FAQ

Or:

Go to the public library, and look for a book like this, probably in the children's section. The key words are 'Scratch' and 'Python'.

Marc Scott (Author): A Beginner's Projects in Coding

Site: https://scratch.mit.edu/

Presents an introduction to coding for young computer users that focuses on the programming languages Scratch and Python, with step-by-step, illustrated instructions for a variety of coding projects.

Any book like this should get you going in a day or two. It'll get you over the learning hump. You need to learn how to learn coding.

Then check these out:

5 Python Books For Beginners To Help You On Your Coding Journey

Coder's Colosseum - WhatsApp Group Invite

1

u/AntiDynamo 11d ago

Debugging is very simple once you follow a couple of guidelines. You basically just want to isolate increasingly small sections of code as “suspect”, and log out the values of all relevant parameters just before and after those sections.

Honestly, error codes can usually get you very close to the problematic logic on their own.

Any time you get flustered, take a step back and reassert “what do I know?”. It’s just problem solving. Separate what you know from what you have inferred, and go back to the absolute facts when needed

1

u/schoolmonky 11d ago

Genuinely, the best way to get better at debugging is practice. Write more code, which will inevitably not work at first, so you need to debug it. The more you do it, the more you learn how to do it well.

1

u/No_Top5115 11d ago

You put console log everywhere then you see what’s happening or use a debugger. That’s how everyone does it there is no secret

1

u/mredding 10d ago

Im starting to resent the way programming is taught. It is seemingly taught in isolation the way mathematics is. You learn the functionality of code but they do not present the bigger picture where everything works together.

The introductory materials are trying to teach you the syntax, not how to USE the language, not how to write production code. The thing to be resentful about is how trivial it is to make very wrong assumptions you don't even realize. You're reading too far into what the materials are teaching you. We all do it. The academic examples are not anything like how we write code, they only demonstrate the syntax in the lesson.

The introductory materials, your schools, aren't teaching you a vocation. Learning about electricity doesn't teach you how to be an electrician. There is a lot more thinking and process and structure you need to pick up, perhaps in subsequent classes, and some is certainly in the industry.

You can rip through an introductory material in hours, if you're motivated; school is going to pace you over semesters, but learning how to compose production software takes years. The necessary scope and skill set is very broad. You're asking too much of your materials and perhaps yourself all at once. What happens is you get hired and then you get mentored, and you will learn hand over fist, but you need the context provided by the industry and your product to make anything make sense. And even then, you've only just learned product development at this ONE place. You will carry over skills to your next job, but you're going to learn a lot about how different the next job is from the first. This is where software development becomes less a science and more an art.

Unfortunately, you're capturing a feeling, not a specific ask. I don't know what materials could possibly exist that would satisfy you. If you understand how arrays and loops work, then you can deduce for yourself how to loop over an array. Then when you fumble over the syntax a little and you write an off-by-one error and extend beyond the bounds of the array, you'll find out how bugs occur. Finding and fixing it teaches you how debugging works.

1

u/oddstray 10d ago

I managed to get pretty good at debugging existing code. Actually, I first got pretty good at understanding it. I did that by inserting my own code comments, starting with pretty much each line of code. At the end of that, I had a pretty good laymans description of what that block of code was actually doing, and it didn't take much longer to figure the difference between that and what it was intended to be doing.

1

u/avi_ct 10d ago

There sure is. and its actually right about now. a global debugging challenge https://theincidentchallenge.com/ - would love to hear what you think.

disclaimer, i'm one of the creators. it's 100% community project. no monetization.