Walk Away From Your Computer. Seriously.
kcurtin.squarespace.com
kcurtin.squarespace.com
The other aspect to this is reliance on Google. This is part stood out to me...
I start to panic a little bit...I turn to google hoping
for a quick fix and copy/paste one of my error messages
into the search box.
Unless Ruby/Rails error messages these days are completely anemic (I haven't worked with Rails in a while), then the line number and error message should provide enough clues to debug a misspelling without having to go to Google.Trust your analytic abilities -- it will keep you from that "panic" state.
Instead of seeing an error and panicking, be like Stanley Moss in Wag the Dog -- "This is nothing" :) Eventually you'll get to a point like Paul Graham where debugging relaxes you:
I like debugging: it's the one time that hacking is as
straightforward as people think it is. You have a totally
constrained problem, and all you have to do is solve it.
Your program is supposed to do x. Instead it does y. Where
does it go wrong? You know you're going to win in the end.
It's as relaxing as painting a wall.
Source: http://www.paulgraham.com/hp.htmlNow, if I get frustrated, I'm much more likely to walk away and do other things and come back with a fresh mind later. It has almost always been helpful.
Walking away is critical to any creative process.
One comment that I saw on the original thread was talking to people. I wanted to highlight this because it has helped me more times than anything else. Even explaining step by step what my program should do to my girlfriend, who has very little computer expertise in general and usually gives me a blank stare, can help a lot.
I remember finding an image on reddit a while back of a programmer laboring over his code for hours only to find a greater than which was supposed to be a less than sign, my thought was 'a mistake we will all learn again and again'.
1. As one of the article commenters pointed out: "git diff". Seriously.
2. One of the best things I ever did was to buy a weight bench/barbell. When I do my twice-an-hour "walk away for five minutes" routine, I go and do 20 bicep curls or bench presses. I solve problems faster and also improve my health/strength. Win/win!
Seriously. Debugger. For tests, it's as simple as `gem install rdebug` and put `debugger` in the failing test(s). When it stops on that line, type `eval instance_variables`, and viola - your array contains "@microposts" and not "@micropost". Running it against a full Rails application, especially with, say, Passenger, is a bit trickier, but it's still worth the initial effort.
The OP just learned a valuable lesson in what computers actually do. The single char oops is usually programmers' first insight into what it really means when we say "computers do exactly what they are told nothing more or less". This is one of those things we all have had frustrate us to no end. Similar mistakes abound, and are one of the those things that cause a lot of people to give up on the programming thing all together. Here's a real kicker about it: they don't stop with experience, we just get better at noticing them faster.
For example about 2 years ago -- 5 years into a programming career and 12 years into programming in general, I had this code give me trouble:
// C Code:
if (test_fails)
do_crashy_thing();
So I decided to throw a quick printf in there (the situation was such that learning the debugging tools available to me would take a couple weeks, so printf was my quick solution here). I should note that I hade been working heavily in python for the preceding 3 months. My code looked like this now: // C Code:
if (test_fails)
log("info about test");
do_crashy_thing();
Which caused my code to crash every time instead of sometimes. C coders will know that multi-statement conditionals need to be wrapped in { } but single statement ones don't. Further python if statements are indent scoped. The recent python work sort of made me blind to the problem in the code. After 3 days of WTFing, talking to the rubber duck, and so on, I called over a colleague and he pointed out the mistake in something like 40 seconds. A younger me would have been embarrased, but this just happens in programming.My point is: these things happen, and I'm glad to see a blog post about this, without the demoralization that normally comes with it. I think it should be more openly discussed in tutorials and other newb oriented posts.
A few tips for anyone who is experiencing this sort of thing and not sure what to do:
Get a rubber duck (or whatever) and start employing the rubber-duck debugging technique: http://en.wikipedia.org/wiki/Rubber_duck_debugging it really helps!
Be willing to include colleagues early and often. Experienced programmers understand and won't think less of you. Doubly so if you are new. (The will tease you a bit about it, but in that shared experienced bonding way, not in the "mock the outsider" way).
Realize that as you are chasing the bug by getting down and dirty with other bits of the program, you aren't wasting time, you are most likely fixing other bugs that would have otherwise just manifested later anyway.
I will definitely experiment with the tips posted above - I am learning Github which could be useful for this I think, but do you recommend any simple tools for showing someone who is remote your code? The issue with copy/pasting is figuring out which code is relevant to show someone.
Thanks for your insightful comment.
Then when you run into a problem, you can easily use "git bisect" to figure out which commit caused the problem, and if you've kept to small commits, you should hopefully be able to figure out the offending line(s) of code pretty quickly.
But beyond this, I think the early sign is your gut says, "it should be so simple but I can't figure out what's wrong". Listen to your instinct -- it's probably right! Don't spend another 2 hours poking at it. That's a good time to either step away for a bit or ask someone else to look over your shoulder for a bit while you try to explain to them what's going on.
For showing code to others in general i really like pastebins. http://paste.pocoo.org/ supports "multiple files" which means you can upload multiple distributed snippets at once. http://jsfiddle.net/ is really great for javascript/html and helps others to change/modify the stuff.
I often try to simplify the example (if the persons i am showing it to are not working in the same team as i am) to make it clear what the problem is and make the problem more accessible. This simplification often helps me to understand and solve the problem.
A few claims: * no matter how good a programmer you are, you will make stupid mistakes (such as typos, as in the case of this article) every now and then. * it is statistically inevitable that you will make many such errors in a large code base. * bugs are easier and cheaper to fix the earlier you catch them; the best time to catch a bug is before the code is paged out of a programmer's brain. * it doesn't take too many experiences of discovering a typo bug in your "production" code base before you realize there must be a better way.
My personal approach is the following: * restrict the use of untyped scripting languages to pieces of code that can fit in one screen or so; that is, just small enough that you can keep all of the moving pieces in your head at once. * as much as possible, use languages and tools that provide strong static checks for larger projects. This includes language type systems (e.g., Scala, OCaml, Haskell, etc.) and tools that help you check your code (Prefix/Prefast, Coverity, Findbugs, etc.) * testing is good, but static checks are far better.
Unfortunately these opinions don't match common practice in the web programming world.
Mind you, much of the C code I've co-maintained for the past while is compiler source, and compiler developers may be more attuned to seeing structured text as a parse tree.
Good for you!
But fortunately you put the braces in anyway, because the person who touches the code after you might be more prone to the mistake.
I almost never misspell things. I might therefore be tempted to believe that spell checkers are a pointless and annoying waste of time. But not everyone has the same brain as I, and experience has taught me that there are lots of people who can't spell well, many of whom are actually really good writers. I'd assume that the optional-braces problem is a cliche for a similar reason.
Another one is if(0 == some_thing). It seems clever and I try to use it, but if you write if(some_thing = 0), the compiler just warns you. So there's no real point in doing this.
The problem with compiler warnings is, unless you have a warning-free base build, they're difficult to see. And in turn, a warning-free build more or less depends on the very first developers to have turned the warning flags on while they were checking in stuff like mad trying to get someting to ship.
So, yeah, I could claim that I don't too many such silly mistakes (and that might even be true), but I can both understand and approve of such coding standards.
if (not_true) return;
Anything longer goes on the next line, within brances.To my mind that's worse because you lose meaning when doing the auto formatting. Better to just give a warning instead, this also goes for while and for statements.
In the early 1990's half of the country went without phone service for I think it was several hours due to a rare case statement that ended up running, he forgot a break, code that wasn't supposed to, ran. Crash.
Sometimes you can, for whatever reason, get so close to something - a project, a situation, a problem, a case, etc. - that you get tunnel vision and fail to appreciate the bigger picture. That's just a product of focus, determination, and ambition, I think.
Once you step out of that tunnel and focus on something else, you can release the frustration you had before and return to the problem with a clear mind and even a totally fresh outlook.
The problem doesn't matter as much as your own wellbeing, so there's no point in stressing yourself out over one when there are million other things you could be doing. Such a laid back approach may allow you to be more productive, hence in that case it would be intuitive.
When I face the same problem and when my head is full of all the noisy - "why is this thing not working, I think I did everything right" s
- I stroll around, walk a mile or two - have some coffee there at some store - look at the people there and think - 'what is running on their mind? Not a buggy code,I guess' - and then come back, stare at the screen and voila! there it is! An insignificant pesky ';' was missing!
So, yes, walk away from your computer. What you and your computer have is a relationship and you need to spend some time apart to realize what you're missing in there.And to get it to work. ;)
When I'm doing something difficult, nothing is more important than giving my subconscious some time to work out a problem.
http://www.amazon.com/Your-Brain-Work-Strategies-Distraction...
(PS: dyed in the wool Python guy here)
Hence why you often hear great stories that start off "So I was in the shower yesterday..." and rarely hear great stories that start off "So I stayed at my desk for an additional two hours..."
There's also something about the "self control as a finite resource" that comes in to play here.
I must admit this is very controversial among startups I've worked in, as the policy is usually along the lines of "no one goes home until this works".
Good thing I have my own company now, and bugs are only solved by programmers that are "fresh" :)
Of course, I ignore him 80% of the time because I can't stand to step away from a problem before it's solved, but he's always right.
Edit: I think you meant to reply to the guy talking about braces, not the parent article talking about typos.
http://news.ycombinator.com/item?id=3036988
Is this some sort of spam account trying to get karma?
But I know that if I did, I'd just feel compelled to blog about it.
FROM DOWNTOWN!