In my opinion so far, make a code that is easy to understand, it will save yourself and others when the code gets bigger. Make enough informative comments to make sure you remember what you did so you (and others) know what it does. Bugs will be there, but nothing better than codes that everyone can understand to solve those bugs quickly.
Secondly, always think ahead of what you write. If you put ++power for a character, think like, when should it be called? how does it added? does the owner of that variable already removed? is it called during attack animation callback? is there a network request failure that should cancel out ++power? etc. This usually comes with experience because you got to make something first to understand what you need and what's going on with your program.
Then to make things easier, always test your code. Program yourself some unit testing (and any other kind of testing methods out there) to convince yourself with the code. In easy term, it's basically like a checklist before going on a trip. You'll face trouble on your trip if you're missing something no matter how small it is as long as it matters right? So you got to always do a proper checking on all items you have before going on a trip. ?