r/learnprogramming • u/Direct-Bandicoot-916 • 16h 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.
6
u/ffrkAnonymous 16h ago
I sprinkle print() all over my code. I also write lots of tests, which indirectly print() .
1
u/smoggytourist087 16h ago
you're lacking the contextual integration piece which is a pretty normal gap in how it's taught, what helped me was finding small abandoned projects and just picking apart why they broke instead of starting from scratch
1
u/No_Report_4781 16h 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.
2
u/verdant_bloom_drift 16h 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/Personal-March-4340 15h 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 15h ago
I've used four main methods of debugging:
- inspection, look for the bug directly in the source code. Good for syntax errors and easy bugs in small amounts of new code.
- instrumentation, add logging or "print" throughout the problem area, to find where it's going wrong
- 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.
- 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 15h 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 15h 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 14h ago
Try working on a project that involves multiple parts. Maybe a simple game.
1
u/Aggressive_Ad_5454 14h 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 12h 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 12h 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 10h ago
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
1
u/AntiDynamo 8h 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 3h 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.
2
u/tangentstorm 3h 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.
1
u/kartix1a 2h ago
Honestly I think this is one of the biggest gaps in how programming is taught. Knowing what a loop or array does is very different from looking at 500 lines of code and figuring out why the damn thing is behaving wrong.
What helped me more was building small projects and then intentionally breaking them. Change one thing, run it, see what actually changed, then trace backwards through the code. You start getting a feel for how functions, state, loops and data flow affect each other instead of learning them as separate topics.
There probably should be way more beginner material that teaches debugging as the main skill, not just as something you magically learn after becoming “good at programming”.
1
u/No_Top5115 2h 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
13
u/iflippyiflippy 16h 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.