In all of my days as a programmer a lot of people approached me with the question "Why isn't my code working?" and it's usually because of one of the following reasons:
A couple of days ago I was helping a good friend with a coding problem and he told me: "Don't solve it, tell me the strategy to solve it", this was the first time anyone told me that and it got me thinking... he was right, by solving every problem I'm not allowing the young programmers to learn and that gave me the idea to write this post.
I tried to think of the ways I deal with coding problems and I came up with several principles.
I often see people writing code with no coding standard (inconsistent indentation, spacing, etc...), and when I criticize them about it they say "I'm just testing something, it's not production code, bla bla bla...".
And when they ask me to help and I take a quick look at the code and tell them they're missing a semicolon or something like that they're amazed at how I do it.
Well... there's no magic to it, I'm just always writing my code as clean as possible (perfectly indented, consistent spacing between functions and parenthesis, etc... all according to a coding standard for the language I'm currently using).
Once you get used to always writing and looking at clean and consistent code, then whenever something is out of place, it just pops out.
(In a positive form: "Write code in small iterations of write-test-write-test...").
I'm constantly seeing people creating a new project and writing a lot of code without even compiling and running. The first time they try to actually run it they have no idea what's not working and have to go over a lot of code until they figure out what's wrong.
If you do have to figure out how to fix a large piece of code, the best way is to split the code to small chunks and check if they're working. Let's look at an example: People sometimes come to me with code like this (it happens a lot lately, I'm not sure why...):
$('div.class1 a[href^=https://]') .doSomething() .doSomethingElse()...
I often see young programmers try to solve bugs by treating the specific symptoms described in a bug report instead of trying to understand why that bug is happening and what is causing it.
Make sure you understand the domain of the problem and the technologies used in the feature, because if you don't, you'll just cause more harm than good.
I hope you find this useful, Until the next time, David.