Panic! How to get help with programming

So, you started programming, it is going great, but you hit a stumbling block. The code does not work and you just cannot figure out why! What to do? Smash the laptop against the wall? Drink more coffee? Consider your life choices? Cry?

The answer is, of course, none of the above. Help is out there, but the first thing to do is, DON’T PANIC!

Photo Source: Flickr Creative Commons by Jim Linwood

This is normal, it happens all the time to even experienced programmers and a big part of dealing with it, is really to not panic. As you become more experienced you will of course get stuck less, you will be better at figuring out the problems in a more structured way, but it will still happen occasionally. When you are just a beginner you will just have to accept that it is part of the learning process. But I promised some advice, so here goes:

Are you solving the problem you think you are?

This is always the first thing to consider. Did you understand the problem correctly? Do you understand what is really supposed to happen? It has happened to me that my code did the right thing, but I did not completely understand it, so the output took me by surprise. It is not a common experience, but it can happen. More likely is it that your code is wrong.

This means that it never, or not always, produces the right output or behavior that you want. The first thing to do is to sum up in your head what it should be doing. Then read the code again, slowly this time, and try to figure out if it is actually what you want. If you are pretty sure that it is, then maybe it is time to try the debugger. But before we pull out the big guns, I will introduce you to shutgun debugging.

We could also call it poor man’s debugging, but whatever the name, the idea is simple: print out the program state (or at least the relevant parts) at key points during execution.

For example:

var i : Integer
For (i<=1;i<11;i<=i+2)
i<=f(i)
End For
print("I is: " + i)

Let’s say we were expecting:

I is: 10

But we get:

I is: 9

It is late, we have had too much of that good, black caffeine drink, and we just cannot figure out by reading the code, what is wrong. It seems that f is doing something, but is it?

We may then try shotgun debugging:

var i : Integer
For (i<=1;i<11;i<=i+2)
print("Before calling f, i is: " + i)
f(i)
End For

We get:

Before calling f, i is: 1
Before calling f, i is: 3
Before calling f, i is: 5
Before calling f, i is: 7
Before calling f, i is: 9

Aha! The problem is not f it seems, but rather that i is incremented in steps of 2, instead of 1. The problem is this line:

For (i<=1;i<11;i<=i+2)

Where i<=i+2 should be replaced with i<=i+1 .

Heavy artillery — the debugger

The debugger is the programmer’s best friend, rather as diamonds are a girl’s best friend. It helps us when times are tough and … okay, this analogy is a bit stretched it seems. But the point is, diamonds are cool and so is a debugger.

The debugger is extremely useful in helping us figure out what is wrong. Debuggers come with a ton of features, but the one I will focus on here is stepping. The debugger allows you to run the program one line at a time. It is like running your program by hand, except you get a machine to do the hard work. Just press a key and the program runs the line you are on, and it will show you what the value is of all the variables.

In this example we are debugging a short program which does essentially the same as above, it is a for loop which is supposed to step 10 times and print the value of i at the end, calling some function f with i as an argument in each iteration. We have a breakpoint set at line 14, which means that the debugger will stop there. These are usually simply made by clicking to the right of the line number.

The IntelliJ debugger stopped at line 14 (Java)

In this case we see the debugger stopped at line 14 in the top right window and in the lower part we can see that at this time the value of i is 0.

We can then step by pressing F8. If step for one iteration we can see below that i=2. This already tells us what is wrong, i must have been incremented by 2, instead of by 1.

The debugger after one iteration.

This is of course a simple example, but the more complicated your code, the more useful the debugger becomes.

When debugging is not enough

When the traditional debugger is not enough either, you may want to try first the approach known as rubber duck debugging.

An enormous rubber duck. Credit: Eva Rinaldi under Creative Commons

Rubber duck debugging is essentially telling a rubber duck what you think your program is doing, what it should be doing and what you think is wrong. Surprisingly often this will make you realize the answer. If you have a friend, he or she may stand in for the rubber duck. If rubber duck debugging is too humiliating, or not successful, it is time to try … drumroll … the internet!

Online solutions

When you are completely stuck or just lazy, it is time to try the internet. The most obvious resource is Stackoverflow. Stackoverflow is a website where people can ask questions and other people answer them — pretty simple. The two advantages Stackoverflow have, are a big community, and a voting system. The big community means that there is a realistic chance that somebody will answer your question, and the voting system means that it is relatively quick to see if other people think an answer is good.

For many, many problems the question has already been asked, so I have only very rarely had to ask myself. In the cases when I did, my problems were extremely specialized, so I did not get many replies. In this case, if you find the solution, it is nice to answer your own question for other people to read later.

The main problem with Stackoverflow in my opinion is separating the good answers from the bad, and this is especially true with the simple questions where many people will have an opinion. For those I think you have to look at votes and maybe you can find some people, like Jon Skeet, who you can trust to give good and frequent answers.

In any case you should never be too trustful of answers to problems that you find online. In many cases the people writing them never tried to run their own code, they may not have understood the problem and you should never assume that they are that much smarter than you, after all they were also on that website in the first place. So always try to understand the code that you find online, try to avoid pasting it directly, instead reading it and writing your own version of the solution that you found. That way you can be more confident that the solution is correct and that you actually learned something from it.

Good luck!

I hope these little pieces of advice may be useful in your quest to become a programmer. If all of these fail, there is one more thing to try: ask a friend. Programmers are mostly friendly people, and many will not be able to resist helping someone seeking the noble knowledge of software, and you may find yourself trying to stop the person after ten minutes of explanation — this is where you can offer a beer and gently steer the conversation to something more interesting (perhaps your next programming assignment).

--

--