Are there any tell tale signs that help you notice these types of bugs faster? I am learning Ruby on Rails but I assume there is some type of continuity with how this rears its head between languages.
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.
If you use git, that can definitely help you. The trick is in making small, targeted changes that do one logical 'thing', and then committing that. Don't be afraid to make tiny commits -- often I'll make a series of 1-liners if they aren't strictly related. Commits are very cheap in git, so don't worry about making "too many".
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.
To echo one of the comments on the linked post --- it is because of bugs like this that I am a firm advocate of languages with strong static checks.
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.
For me a big tell is if it should work and you have no clue why it doesn't or why it really behaves strangely/unexpected.
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.
The best way to prevent these kind of bugs is to change your programming style. Always inserting { } for if-statements, even for one liners, is a programming style, and helps eliminate this specific bug. You will pick up these things as you write programs, or by looking at other peoples code.
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.