Good Programming Practices in JavaScript
- Format scripts neatly. Keep them readable. Use consistent spacing and give variables meaningful names.
- Use lots of JavaScript comments. However, not to the point of making the script unreadable. Both proper formatting and correct usage of comments will help you to remember what you wrote at a later date and more importantly, assist those who come after you, since others do not know what you wrote and why you wrote it.
- End all JavaScript statements with semicolons even where not required. Also put all block start { and end } markers on separate lines and put them into the same column.
- Declare all variables with the var keyword. This will avoid problems with variable scope and thus any confusion.
- Always use the theory of reduction. Divide complexity into smaller tasks as a way of solving larger problems. Divide complex scripts into meaningful functions where each function completes a specific task.
- When writing large scripts write that large script in distinct and separate phases. Test each phase and ensure each functions as required before proceeding to the next phase.