Here's my advice, been programming for 15 years.
Write comments, one per block of code that does a step, then fill in code. You will then have well commented code, and forced yourself to think through the solution before you begin coding. This saves tons of time by avoiding thought errors before you code.
When hunting a bug, don't just look at what's not working. Instead look at what was most recently changed, even if it seems it couldn't possibly be related. The times I didn't do it this way have cost me many days hunting down a really tricky bug. Sometimes it really is unrelated to recent changes, but not often.
If you are stuck, take a break and do something mindless, like get some water, go to bathroom etc. your subconscious keeps working without the interference of your conscious mind.
Preplan your work a few days ahead if possible. You can avoid many roadblocks by thinking through things ahead of time.
Persistence pays off. I've worked through many "seemingly impossible" tasks, only to find the solution after failing a few times first.
Visualize what the users interaction will be before coding. I like to draw it on paper and pretend to use it. Putting yourself in your users shoes allows you to see what might be difficult to understand. I rarely keep my first design, but since it's just a drawing I'm not invested in it. If you lay it out in software, it's much more tempting to keep a poor design.
Ask a colleague if you are stuck. Often, articulating the problem out loud is sufficient to solve it!