Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I'm a beginner programmer, self-teaching and I have a question. I have some code and I don't know why my code isn't doing what I want it to do. My question is; what do you do when you just don't know why/how your code is not working and you've spent ages at it?

Start over, or keep going, or what? I'm talking about best practice here, best practice for inexperienced programmers.

share|improve this question

closed as too broad by Frank, Telastyn, MichaelT, GlenH7, AProgrammer Jul 3 '13 at 16:03

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

Go to bed and tackle your problem in the morning. – Gilbert Le Blanc Jul 3 '13 at 13:14
Debug your code. If you do not know how, learn how to debug first. – Kolyunya Jul 3 '13 at 13:42
@GilbertLeBlanc 's suggestion is actually extremely valuable. During sleep, the brain works actively at solving problems you encountered during the day. It happened to me countless times: I would stare for hours at a problem or piece of code without being able to figure it out. The next morning, when I go back to it, the answer just pops up in a matter of minutes... – Radu Murzea Jul 3 '13 at 14:51
First: is your algorithm solid? Have you written it out, sketched a picture? Have you got any proof that it should eventually give the desired result? Second: check your code against the algorithm. Third: break your code into pieces, and test each of the pieces in a test harness on a sample dataset independently of others. Fourth: read the language standard. You may be surprised. – Deer Hunter Jul 3 '13 at 16:00
Ask your Teddy bear for help! - Serious - grab a Teddy bear, Grandfather, mum or other suitably inanimate object, and walk it through you code explaining what, and why it does what each line does..... – mattnz Jul 4 '13 at 7:41

5 Answers 5

I sometimes find it useful to reduce the source code to a minimum (comment out stuff) that works / does what I expect it to. Then I keep adding from the commented out functionalities until I manage to pinpoint the location of the problem.

Basically, approach it like you would any other problem: try to work with a more relaxed / more simple version of the problem, and build on it until you find the error.

EDIT: And of course, as mentioned in one of the other answers -- debug! I had assumed you had hit the wall in your post during debugging.

share|improve this answer
Also asking on Stack Overflow with the real problem (after doing what Cristina mentioned) could help. – Uwe Plonus Jul 3 '13 at 11:23
@UwePlonus believe me Stack Overflow was my first port-of-call. – Jason Jul 3 '13 at 11:28
Could you please make the debug bold? – Uooo Jul 3 '13 at 11:51
@w4rumy There :-) – Cristina Jul 3 '13 at 11:58

I think that you just mature to the next step on programming (welcome!), you should not see yourself as a beginner. You're facing logical problems in your code that needs some deeper analysis.

Now you need to get familiar with debugging. Depending which language you use and which IDE you'll see how the debugging works. I suggest you use debugging from the IDE itself, as you may be familiar with (if not then I recommend switching from it and use a IDE that has it integrated), and from there start adding breaks in parts of the code, and start and stop the debug checking the values on the variables.

Good luck!

share|improve this answer
Yeah so far the debug button was just when I wanted to run something to double check it worked; now comes the actual debugging. Also, I'm happy to be here in the next stage of programming, I like what you guys have done with the place. – Jason Jul 3 '13 at 11:33

Although debugging is a valuable skill that you should hone, eventually, as your skill set and development maturity grows, you will rely more on unit/integration tests than manual debugging. A failed unit/integration test will often tell you enough to ascertain the issue without tedious breakpointing and stepping (warning: sometimes you will still have to debug though).

I suggest that you jump straight into unit/integration testing - there is no better time to start than now as you are still learning. The refactoring that may be necessary to modularize your code to make it testable will be well worth it in the long run. The testing effort will force you to find ways to incorporate useful principles (if object-oriented: SOLID) which will greatly increase your effectiveness as a developer.

When you find yourself in a situation of not knowing what is going wrong, you know that your test plan and probably your design need work. In cases like this, debugging can be reactive (though useful depending on the situation). Be proactive and build out tests.

share|improve this answer
While unit testing is a good idea in general and the points suggested here will help with detection of errors in many cases they are start. If test is failing you still need to find out how to fix it. – Maciej Piechotka Jul 3 '13 at 14:39
@MaciejPiechotka Yes definitely, you still do need to know how to fix it. It has been my experience that a proper test plan lessens the need for traditional debugging dramatically. Usually a failed test will immediately tell the developer what is wrong. – smp7d Jul 3 '13 at 16:39

As mentioned in the comments, taking a break from programming can be extremely beneficial as well. Whenever I get to a point where I don't know what to do or just don't feel like focusing anymore, I get up and walk around the block my office is located at. And if I'm facing an especially tough problem, I usually move on to a different task and come back to it the next day, after giving my brain a chance to work on it subconsciously.

share|improve this answer

I'm also a junior programmer and a self-learner. I would recommend "The Pragmatic Programmer" by A. Hunt & D. Thomas. I've heard the name couple of years ago but I haven't realize how much practical help is in there -for both beginners and self-learners- until I bought a copy for myself and start to read and practice it chapter by chapter.

There is a whole part related to debugging and how to strategically face, find and solve errors and problems. There are also a lot of realistic motivations in this book.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.