30

You know them, those errors that make NO sense. Where it seems like a gremlin just jumped deep inside your chips and messed up something. Do you take a walk, write stuff, call an uncle?

gnat
  • 21,442
  • 29
  • 112
  • 288
Caffeinated
  • 656
  • 2
  • 11
  • 33
  • 3
    Your description may be correct - Reboot and try again! – NoChance Sep 03 '11 at 03:32
  • 5
    post it on stackoverflow of course :) – William Sep 03 '11 at 08:44
  • Good-News Update: I(we) got it fixed! Thanks to you all! I was aided by the walk, then by simply examining closely the important files and googling. I will write down all your great advice for when I get another challenge! Thank You So Much – Caffeinated Sep 03 '11 at 09:33
  • That said, I do have one new discovery - it may help to jump in an IRC channel and ask stuff. – Caffeinated Sep 03 '11 at 15:43
  • 1
    Well if your uncle is Bil Gates or something... – BlackJack Sep 03 '11 at 20:08
  • 5
    Why did you decide on a threshold of 10 hours? That's way too long - if you don't have a good idea of what's causing unexpected behavior within an hour or two, you are in trouble. – Vector Sep 05 '11 at 20:37
  • @Mikey, yes. Well, I was thinking of non-contiguous programming time. i.e, with breaks & naps & chatting. But we can call it 2 hours really, even 30 minutes for advanced coders. – Caffeinated Sep 06 '11 at 17:06
  • 5
    "When the going gets tough, the tough go to sleep and let the subconscious work on it." -- anon – Michael Easter Sep 06 '11 at 23:52
  • @BlackJack, Yeah my uncle would launch into "when i was your age, back in the USSR...." – Caffeinated Sep 07 '11 at 03:13
  • 2
    1. Get someone to help. Two people is a must. 2. Narrow it down using excessive amounts of debug statements. There was a file where every single line was preceded by a debug macro to pinpoint the one that segfaulted. – SF. Sep 07 '11 at 09:27
  • I just usually get away from it for some time, and then have a fresh look at it again after that. Usually helps when you're in those situations :) – Michell Bak Sep 07 '11 at 11:22
  • @SF - "There was a file where every single line was preceded by a debug macro to pinpoint the one that segfaulted." I've used that technique many times - very effective. – Vector Sep 07 '11 at 16:16
  • 1) Rebuild 2) Look at an older commit to figure out what part of code has the problem 3) Make sure I understand what triggers the bug and doesnt and what the bug is actually doing 4) Isolate the code 5) Fix. Steps are always in order but not every step is must be done –  Aug 13 '13 at 10:28
  • "If you don't have a good idea of what's causing unexpected behavior within an hour or two, you are in trouble": I once had a bug in legacy code and it took me 8 days to find the problem (and 10 minutes to fix it). – Giorgio Dec 08 '14 at 10:34
  • @Giorgio:I think vector is assuming that you know how to create the problem in the first place. Being able to recreate the problem on demand is 95% of the solution (at least). I've seen many bugs over the years that have stumped people for days, weeks or a month because they couldn't figure out how to recreate the problem. – Dunk Dec 08 '14 at 20:01
  • @Dunk: In my particular example I could recreate the bug at will (open that particular data set, then do X, then do Y, then close the application --> crash) but I had to spend lots of hours in the debugger to find out the exact place in the code where the bug originated. It was pretty messy legacy code. – Giorgio Dec 08 '14 at 20:58

20 Answers20

79

Quit. No, not your job! Just get up and go home. You're done for the day or the weekend. 19 times out of 20 when you come back to the problem next, the solution will present itself within an hour.

Dave Nay
  • 3,809
  • 2
  • 18
  • 25
  • Hmm, I see. However, I'm right here looking at a problem I was looking at yesterday! But yes, I know - walk a way a bit. :) – Caffeinated Sep 03 '11 at 02:08
  • 17
    You could also try rubber ducking it. http://en.wikipedia.org/wiki/Rubber_duck_debugging – Dave Nay Sep 03 '11 at 02:11
  • 2
    19 out of 20, yes. My worst one never did get solved, only worked around. No test environment ever showed it, only the full production environment in operation--we couldn't even reproduce it after hours. – Loren Pechtel Sep 03 '11 at 02:14
  • 3
    Getting away from something thats irritating you can be really hard - but I've found over the years that its always the best thing to do. The subconscious mind can work on the problem while you eat, sleep, good off, watch TV... and next day (or the day after) things go better. One word of warning though: gather information before you walk away... Walking away is not the same as ignoring it and pretending its not there. You still need hard work! – quickly_now Sep 03 '11 at 03:10
  • 1
    I do not know about an hour. I generally solve most of these types of problems in the shower when I get up in the morning. The second most frequent will be when i am nearly asleep at night and have finally allowed myself to stop thinking about it. – SoylentGray Sep 06 '11 at 18:25
  • 3
    There was a fascinating NOVA Science NOW hosted by Neil deGrasse Tyson which talked about the science of sleep. In it was discussed the phenomenon of banging your head on a problem for hours, going to sleep, and waking up and solving it right away. When we sleep, our brain turns the events of our day over and over and over, analyzing it from many different angles. What it leaves behind are new neural pathways that can actually help us see the problem in a whole new way subconsciously, and then actually solve the problem. Pretty awesome. – Byrne Reese Sep 07 '11 at 00:05
  • @Byrne: http://www.pbs.org/wgbh/nova/body/sleep.html – Dave Nay Sep 07 '11 at 00:57
  • I can usually solve one programming problem while I sleep or at least come up with a new angle to try. Hard to solve bugs are often caused by something very simple or an unusual combination of conditions. Think about the bug without the code to try to come up with possible causes. – Michael Shopsin Sep 07 '11 at 17:02
44

Before ten hours go by, I would get some help.

  1. Describe the problem to someone else, anyone else, even your rubber duck.
  2. Ask someone else to take a look at the code, or step through it with them.
  3. Isolate it. Delete a bunch of stuff, then bring it back bit by bit until the problem reappears.
  4. Get some sleep!
kevin cline
  • 33,608
  • 3
  • 71
  • 142
  • 12
    +1 for deleting everything until the problem disappears. – Jonah Sep 03 '11 at 03:05
  • 4
    You should do one of those things before 1 hour goes by. The more you stare the less likely you are to achieve your epiphany. I normally solve a problem just by talking it through with someone. – Ben Sep 03 '11 at 21:43
  • Spot on. Often I figure out the problem, (or get close to it) by first describing the problem. Frequently this occurs while writing a description of the problem for a StackOverflow question. Which also requires a reduction (isolation) and then failing that, a wait period where you step away from the problem and let the SO answers come rolling in. – sholsinger Sep 07 '11 at 14:31
17

One word, timebox, set a limited amount of time to work on something, and if it isn't solved, move on to something else and come back to it the next day with a fresh perspective.

That and another set of eyes, is always worth more than any time you can waste staring at something.

I would never spend more than 45 mins to an hour trying to solve something in one sitting, it violates the law of diminishing returns.

9

For those really horrible problems my strategy usually goes as follows.

  • Experiment and google. Keep trying to solve the problem. Most of the time this solves the problem in an hour or less.

  • So that hasn't worked. Take a break. Have a coffee, talk about something unrelated to a colleague. Push the problem out of your mind. When you look at the problem 5 or 10 minutes later you are looking at it from a slightly different perspective. Most of the time this works.

  • In this case it hasn't. So spend another 10 - 30 minutes looking at it. Then call in a colleague. But before you do, make some notes; you want to demonstrate the problem, reproduce it, then list of the things you have tried, and most importantly prove that you have tried them. So do a dry run first. Set some book marks in the code, close any superfluous open documents etc. This way you may either solve the problem yourself, or when you do demonstrate the problem you won't be wasting their time.

  • Ask your colleague to make you prove all your assumptions. is that setter actually being invoke? Is that method really returning what you claim it is? You think that object isn't null - show them it isn't null.

  • Most of the time, either demonstrating the problem will make you realise that you haven't tried all the possibilities or your colleague will see your mistake.

  • If that doesn't work its time to get serious. Document exactly what you are trying to do, what you have tried, and why it didn't work. Email this to all your colleagues. Post it on SO. At this point the document should be a perfect SO question.

  • While you wait for responses, google google google. Try every permutation of the question you have. Open up a bunch of tabs. You probably aren't going to get an answer by this point, but you're looking for ideas, possibilities, different ways of approaching the problem.

  • Do something else, if you've spent 5 hours on a problem its time to leave it for another day. Maybe you will get a useful response. Maybe when you attack the problem the next day it will be obvious.

  • If none of that works, its time to look for a different solution. Maybe you can use a different method, a different technology. Maybe you should consider abandoning the feature for now. Are you billing the client by the hour? Are you working for a company on an internal app? You need to escalate this to the owner and tell them "look, I've spent x hours on this and made no progress, is the cost benefit worth it?". You don't want to go to your boss and tell them you spent 16 hours on a problem only for them to turn around and say, it nots that important, skip it for this release. you need to find that out earlier.

  • And if that doesn't work? Well your only options are to keep hammering away at the problem or seek out industry expertise. Ask technology experts on twitter. Email your technology provider.

7

Explain the problem to someone else.

By explaining the problem to someone else, you have to clarify it: this often lets you see the solution.

(One of the UK professional computer magazines once proposed selling life size cardboard cut-outs of a senior programmer specifically for this purpose.)

I find sleeping on a problem (sometimes for a couple of days) can also help.

MZB
  • 535
  • 4
  • 8
5

I have a three step plan:

  1. Get a coffee or other tasty beverage.
  2. Work on something else for the rest of the day.
  3. "Phone a friend" and doodle on the whiteboard.

Each stage is an escalation if the previous step failed. There's almost always something else productive I can work on at stage 2.

Flexo
  • 712
  • 2
  • 7
  • 17
  • Nice advice! So "Phone a friend" is quoted because it should be limited to 60 seconds, like on Millionaire, yeah? I like the whiteboard idea too. – Caffeinated Sep 06 '11 at 17:09
  • 1
    I find the whiteboard really helps think it through methodically. Quotes were because often the friend is in the same office so actually calling would be odd. But it sort of felt like a lifeline from the tv show. – Flexo Sep 06 '11 at 19:38
4

Sleep over it

Otherwise, call someone nearby and ask him to take a quick look over the code.

Often errors which would take you a long time to find (since its your code) are found very easily by others

Akash
  • 674
  • 1
  • 5
  • 12
3

You could see if getting up, pacing around, and thinking about the problem helps you find a solution. Whether or not you're actually standing or pacing, try getting away from the computer while you're thinking.

compman
  • 1,387
  • 13
  • 21
3

I generally do one of the three:

  1. Take a walk/bike ride...some that gets you away from the computer.
  2. Play with my dog or cat
  3. If you have a hobby, work on that for a while.

Any of the three do a good job of distracting oneself from the situation at hand. I find the distractions let my subconscious brain chew on something for a while. After an hour or so of this, bam, there's the solution :-).

toddward
  • 31
  • 1
3

Build a test Harness to target that exact Defect and Isolate it

Just keep eliminating good code.. while replicating the defect. Until you target the exact piece of code casing the error. Then trace the code.

Recommended reading : The Pragmatic Programmer Specifically Chapter 10 : Tracer bullets

Morons
  • 14,674
  • 4
  • 37
  • 73
  • all this is good and well but it takes for granted that the bug has been and can be reproduced. What if the 19 hours spent thus far were to just that... attempting to find a means to reproduce the problem in a deterministic and systematic manner... what then ? To me THAT is the essence of the question here ! – Newtopian Sep 07 '11 at 04:58
  • The Pragmatic Programmer is excellent – Caffeinated Sep 07 '11 at 19:02
2

Edit:

The short answer:

Q: How do you tackle really bizarre errors that keep you puzzled for more than 10 hours?

A: Make sure they never happen: understand your design, know your code, learn how to use your debugger.


Explanation:

"Where it seems like a gremlin just jumped deep inside your chips and messed up something"

This should never happen. If it's your code, you should have a very good idea of what is causing the error before you attempt to fix it.

Futhermore, when you write your code you should already know where and why it's likely to fail.

Having said that - asking a peer, posting on SO, retracing and rolling back your steps and taking a break - all the suggestions mentioned above will help.

The other thing, is you must know your tools - your debugging toolkit. Logging messages at suspect points in your code, examining your call stack carefully, using conditional breakpoints and watches, etc etc. Debugging skills are not extras - they are part of programming.

Vector
  • 3,180
  • 3
  • 22
  • 25
  • Being able to retrace one's steps is vital. Thank you! – Caffeinated Sep 06 '11 at 17:21
  • >Being able to retrace one's steps is vital. SourceControl software is the key to being able to rollback/retrace. Be ANAL about it and if you can, set your settings to force you to leave comments upon checkin. – Vector Sep 06 '11 at 18:55
  • 3
    Unfortunately, no matter how well you know you code, sometimes the problem is an interaction with someone else's (closed source) code. – Nate C-K Sep 06 '11 at 19:32
  • 2
    +1 @Nate CK- very true. The worst types of bugs happen when you get some sort of gibberish back from a web service you were relying on. I had an Saas vendor a while back who subtly changed some functionality without warning in their web service. I had to explain to the developer how to fix his own bug over the phone as he described to me what his code looked like. – Morgan Herlocker Sep 06 '11 at 20:10
  • @Nate C-K : "sometimes the problem is an interaction with someone else's (closed source) code." IMO you should know YOUR CODE well enough to determine that quite quickly. – Vector Sep 07 '11 at 03:07
  • Agree with Mikey. There should not be "bizzare" errors to start with. –  Sep 07 '11 at 13:49
  • 1
    To determine what? That there's a problem in 3rd party code? That part is relatively easy. The hard part is figuring out what conditions trigger it, and how to work around it, when you don't have the source, the vendor is not responsive, and perhaps it doesn't happen on your test system. If you think knowing your code is going to solve all that for you, I suggest maybe you've never had to deal with it. – Nate C-K Sep 07 '11 at 15:29
  • @Nate C-K - but that doesn't sound like the problem the OP was having. In your scenario, you have already determined the source of the error and know what and where you need to work-around. BUT - "and perhaps it doesn't happen on your test system" - if so, maybe your test system isn't what it should be. BTW, I don't claim any of this is easy - but drawing a complete blank after a couple of hours of debugging and work isn't a good sign. Recently I came across a problem that surfaces in a 3rd party component - it took me 15 minutes to determine that - but it may take a few days to resolve it. – Vector Sep 07 '11 at 16:14
2

All of these suggestions are great. However, I use a technique quite often that I didn't see mentioned. Make lists to organize your thoughts about the problem. If I have a particularly sticky problem I usually write out multiple lists such as: Facts, Assumptions, Questions, Symptoms, etc. I find that oftentimes in the process of organizing things in this way I discover assumptions I didn't realize I had (that often turn out to be wrong), questions I didn't realize need to be asked, other permutations I can check, etc.

RationalGeek
  • 10,077
  • 7
  • 38
  • 56
1

I had a similar problem, an apparent memory corruption in Objective-C, which I struggled with for many hours. But then me and my colleagues just took a walk for lunch, and I explained the problem (and one particular bit having to do with deserialization of an object in its init method), and basically explained the whole problem to myself.

(techy details: basically, I initialized and returned an object onto something else than self, so there were two allocs, but just one object returned. Memory shifted and went crazy, crashes, and the debugger didn't really know what to do with it either).

cthulhu
  • 911
  • 5
  • 8
  • 1
    "I initialized and returned an object onto something else than self" - bugs like that are TOUGH! You can look it over 100 times and not catch it. But couldn't you see two allocations by tracing through in the debugger? – Vector Sep 06 '11 at 02:31
1

enter image description here

Take a bath.

Any Rodney McKay fans?

Seriously, though, if there's one commonality among all these answers, it's to take a break and do something else.

I like to think of it as relegating the problem to your subconscious. Even if we are otherwise unaware, our minds (seem to) continue to work on the problem, even when we're doing something else, such as taking a bath.

Paperjam
  • 208
  • 2
  • 6
1

Step through it step by step, down into assembly. Who calls what, break-point on memory access. That usually catches the bug real fast.

If not, take a walk.

Coder
  • 6,958
  • 5
  • 37
  • 49
1

A combination of all of these :

  • Get away from it for a while so it can sit on your back burner. Sleep, rest, eat, take a walk, whatever.

  • Examine the problem more, what else does it do wrong, what other symptoms can you find?

  • Research the problem, see what you can find. Remember to try different keywords

  • Try something different. A work around. A different debugging technique. A validator. A different computer.

  • Talk to someone. Even if they are not able to help, or not even a programmer, sometimes talking will trigger the idea lightbulb

  • Restart! If appropriate, try restarting your computer, the server, etc. If nothing else, you can use the time to think.

  • Ask StackOverflow! We are here to help

rlb.usa
  • 961
  • 9
  • 18
1

I really didn't like the most up-voted answer, because even though that sometime works, some times you just need to figure it out that same day, so what I'd recommend, in this order, is:

  1. Confirm that it's not only happening to you. This can save you a lot of time. Maybe you uninstalled a required component, or made a change in your environment, and an exception is being swallowed somewhere in your code. If it's happening only to you, I would use an environment comparison tool. I recently read about a software called Envy, which allows you to do just that, although it's not freeware, it costs 10 USD.

  2. Happening to everybody? Fine, now do a View History on the code and verify for recent changes that might have caused the error, either directly or indirectly.

  3. There aren't any recent changes? If it's a very specific error (an exception), 'stackoverflow it'. Now that doesn't sound better than 'google it' but I feel good to say I first search stackoverflow for programming research than google. If it's a really known issue, it's very likely that you'll find a solution here. If not, then post a question on the related stackexchange site. You might get a very quick answer, or even if you don't, your question will be out there while you do more research. That's a benefit.

  4. If you didn't find an answer online or it isn't a general error, then walk through the code step by step, checking if the obtained results of each step make sense for the result you expect. Go start to finish on each method, and bottom to top on a tiered solution. (i.e. if you're troubleshooting performance, start with the code that retrieves the records. doesn't make sense to start in the UI if you can determine quickly if the first step is the problem).

  5. If after going through the code a couple of times you still haven't found what's wrong, call-in someone to talk about it. As someone already mentioned, talking about it out loud can light the bulb. Plus pair-programming is really useful.

  6. At this point, if it's feasible, walk away either for some time or for the day. I read a very true tweet yesterday that said "I went to bed thinking 'how the fck' and woke up thinking 'but of course'". So true.

  7. If you still don't have an answer, I'd dare say you could try refactoring into smaller tasks/methods/functions. Henry Ford said something like 'There isn't a task so complex that cannot be accomplished by breaking it into smaller tasks'. At this point, if the solution is too complex and you haven't figured out either by yourself or with the help of someone else, refactor the code into smaller tasks. Even if you don't end up commiting it, it can help you find the reason.

  8. Add instrumentation to your code.

  9. Tweet about it??

silverCORE
  • 173
  • 1
  • 6
1

You need to step back. My motto is 'if the problem is too hard then you are solving the wrong problem'. What are your assumptions? don't trust anything.

The corollary to that is 'the weirder the problem, the weirder the solution'. The computer's strength is its logic so you can't win on logic. You have a brain and have to out-think it.

In modern times there are so many other things interacting on a system - firewalls, AV, Antispyware, automatic updates happening every night - you have to deal with moving targets.

jqa
  • 1,410
  • 10
  • 13
-1

Google it. Stackoverflow it. Post it on forums. Basically if you can't solve it alone, get people to help.

Pacerier
  • 4,973
  • 7
  • 39
  • 58
-1
  1. Write down the problem.
  2. Think hard.
  3. Implement the solution.
Ingo
  • 3,903
  • 18
  • 23
  • Concise, very good! – Caffeinated Sep 07 '11 at 18:44
  • 1
    Actually, no. Thinking too hard along the same tracks is just about the worst possible thing you can do. *'Challenge, enumerate, revisit and test each of your assumptions, in a systematic way'* is the solution here; people are discussing different tactics of achieving that. – smci Jan 21 '12 at 22:43