views:

396

answers:

16

When I am stuck with a problem, I search Google for code snippets. I look at isolating the problem, so that I can better explain it to others in order to get answers.

What are the search techniques you use to find the solution to your problem?

I started asking questions in stackoverflow.com .

What other techniques or methods do you follow, to fix the problem more quickly?

+8  A: 

Well theres:

  • Google;
  • Google;
  • Google;
  • Stackoverflow;
  • Google;
  • Google;
  • Maybe a book if I have one.

Seriously, I started (hobby) programming in the 1980s and even into the mid 90s you had to know things and have a technical library. Then google came along and its easier to google something than it is to look up (bookmarked!) API documentation (google "java stringbuilder" will get me there faster than navigating will) let alone an actual book (electornic or paper).

Most problems you're trying to solve have been solved before. Many times.

The rest of debugging comes down to decomposition, possibly unit testing (which is related to decomposition) and verifying your assumptions.

by "decomposition", I mean that your solution is structured in such a way that small pieces can be individually tested and readily understood. If you have a 7000 line method you're (probably) doing something wrong.

Understanding what assumptions you've made is key too so you can verify them. For example, when I started with PHP I wrote a piece of code like this:

$fields = $_SESSION["fields"]; // $fields is an associative array
$fields["blah"] = "foo";

and I was scratching my head trying to figure out why it didn't work (the array wasn't being updated next time I queried $_SESSION). I came from a Java background where you might do this:

Map fields = (Map)httpSession.get("fields");
fields.put("blah", "foo");

and that would most definitely work. PHP however copies the array. A working solution is to use references:

$fields =& $_SESSION["fields"]; // $fields is an associative array
$fields["blah"] = "foo";

or simply:

$_SESSION["fields"]["blah"] = "foo";

The last thing I'll say about debugging and writing robust code in general is to understand the boundaries of your solution. By this I mean if you're implementing a linked list then the boundary conditions will revolve around when the list is empty.

cletus
If Google doesn't retrieve the right result, you're not asking the question correctly.
Christopher Mahan
Either that, or the question involves significant punctuation.
Steve Jessop
+2  A: 

My best friend for many years has been to jump on my bike and go home. Just getting away from the keyboard has solved many problems over the years for me.

krosenvold
Does that work even for problems you encounter at 9 in the morning?
Tim Stewart
Problems always come in the afternoon ;)
krosenvold
+18  A: 

Go and do something else. No, really. I've found that putting the problem away in the back of my mind helps. I can't count the number of times I thought of a great solution to something I've been working on when I was working on something else, or watching TV, or eating. It seems your brain is still working on the problem in the background.

If that fails to solve your problem, try talking to someone. You'd be surprised how often others can give solutions to your problem that are so simple you'd facepalm.

Walking away from the keyboard and going for a walk/run/coffee/talk with colleagues is often the best answer. I find exercise helps clear my brain!
Fortyrunner
Gotta agree with this one. I don't even know how many times the solution to something has come to me while I'm either in the shower or on the bus.
Mason Wheeler
+1  A: 

If the problem lasts to the end of the day, I try and consciously lock the problem away for solving before I go to sleep.

I realise this sounds a bit out there, but it has been very common in my experience that I'll wake up with at least an alternate approach to the problem, if not the full-on solution. The idea is not to stress about it - but actively decide to solve it over night. That way you can go to sleep without worry.

I think eating well, regular exercise and good sleep are huge contributors to the problem-solving process.

Galwegian
A: 

Don't forget Google Code Search

orip
A: 

Usually I'll try nut out the problem for a few hours or so, trying different things writing it on paper, making diagrams. If none of that works I'll usually work through the following options.

  1. Put a sticky note on my monitor and keep going with something else
  2. Glance at the note through out the next few hours to keep the problem in the back of my mind
  3. Google for similar problems and the methods used
  4. Consult a co-worker or a friend
  5. Ask on a forum such as stackoverflow
  6. Give up and design the problem away or design a way around the problem so it can be dealt with some other time and stick a TODO note at the site of said workaround
smarthall
A: 

It's often best to clear your head by doing something other than programming for a little while. See this answer for an example - I did it while struggling with a particularly thorny bug, and when I came back to the problem I solved it in about a minute.

MusiGenesis
+3  A: 

Explain the problem to a colleague, or write it down to describe it. That will makes you think a different way, from a different perspective. In order to be more accurate, and to describe the context of the problem, you'll step back, get a higher level view of the problem, you may find out think you overlooked something that is actually important.

Sometimes, you even find the explanation before ending your description.

philippe
A: 

Usually I try to solve it until I go to sleep.. Sometimes I write on paper what the code is doing and then I divide it in pieces; I try to know how the variables of the program change when it's running.

Francesco
A: 

Try solving a much smaller version of the problem first and see how you get on with that. Once you've done that the bigger problem won't look so scary.

AndyM
A: 

Go to the toilet. You move, so your brain gets oxygen. You relax, so you focus on other things.

Peeing for innovation! :)

furtelwart
A: 

Ask yourself: is solving this particular tricky problem really important to what you doing?

For the purposes of your application (or whatever the big picture is) is there a similar but easier problem that you could address to accomplish broadly the same thing.

AndyM
A: 

Normally, I would get pen and paper and try to work out the details of the problem there. If that doesn't help, Google. Failing that, I'd do something else for a while, or ask online. Worked for me so far.

Dan
A: 

The fact you are stuck might be a 'code-smell'. Suggesting that their is something wrong with the design or approach somewhere else. Try to put your finger on what's causing this and fix this instead.

When you come back to your problem it might no longer exist.

AndyM
A: 

One more time, browse thru what I think might be relevant, then take nap. There are two other answers which mention sleeping or napping, but this deserves more emphasis. It is now known that there's SERIOUS machinery in there which goes to work when you sleep. Google (( CBS SCIENCE SLEEP )) will get you to a great free video.

pngaz
A: 

If I can't figure out how to solve the real problem, I try to consider a simplified version of the problem. To take a simple example: I recently had the problem of finding a set of shipping routes to get an item from point A to point B, when there is not necessarily a direct route from A to B, but there might be an A to C and then C to B, or A to C, C to D, and then D to B. (I'm sure the airlines and railroads do this all the time.) That was fairly complex, so I tried looking first at the simple case: a direct A to B. That was easy. Then consider how I'd handle it with one stop along the way. Then consider two stops. At that point I was able to see the pattern to a solution.

Solutions to a simplified version of the problem may end up being a part of the bigger solution, with some additional complexity wrapped around them. But even if not, the exercise of solving the easier problem often gives you ideas on how to solve the real problem.

Jay