Writing a bug-free script or code at the first trial is a fantasy for software developers. We can't entirely escape bugs in our codes but we can adhere to some programming principles to minimize the number of bugs we have to deal with.
There some programming mistakes we can always avoid for a smoother experience when writing codes.
1. Writing codes that are repetitive. - A standard principle in programming is that you keep your codes DRY (don't repeat yourself). If your copying and pasting a particular line(s) of code it is clear you are not using the DRY principle.
2. Using poor variable names. - Your variable name should be descriptive enough to convey the value stored. Avoid using reserved keywords as variable names.
3. Using magic numbers or strings. - Values (numbers or strings) used in your code should be store as properly named variables. It reduces confusing when reviewing your code.
4. Writing long lines of code without commenting. - Use comments to what the next few lines of code do. This would be much appreciated when you revisit your work in the feature.
5. Overloading a function. - As much as possible a function should be doing only one specific thing. For each specific task endeavour to write a new function.
6. Writing unreadable or complex codes. - Try writing codes that are human-readable for your sake and others who you might be working with.
7. Misusing global variables. - Many bugs you would encounter with your codes would come from using unnecessary global variables throughout your codes. Learn to use local variables and closure.