I have very high standards for all the code I write. I will never do anything like assuming that %client.player exists, as it can cause console spam, and I always try to be fairly efficient, especially in functions that are called often.
I usually end up rewriting a lot of what I code at some point, though not so much now I have a set style and standard I follow. But rewriting code means that you will know of any difficulties you could run into, and you know how to avoid them, and you know how to do it efficiently.
I find I'm very good at thinking of bugs that could occur before even writing any code, which is great because I can then plan systems around bugs which I know I would otherwise run into, which saves me having to make inefficient and dirty patches.
While testing code as you go is good to do, I often find that I cant practically test anything until a good amount of the system is coded. To help avoid bugs when doing this, read through everything at some point, like every single statement. Make sure its all good. I've written files 2000 lines long and had them working without any tests (well, a few typos here and there, but nothing major). Definitely helps being able to work out all the bugs you'll get.
Testing things with friends is a great way to both motivate you and test things, and they'll often give you great ideas or help you design something new.