Changes

Jump to: navigation, search

Comments in JavaScript

1,541 bytes added, 20:56, 18 April 2007
no edit summary
There is an old saying amongst veteran programmers that goes something like "Don't comment bad code, re-write it!". Before exploring what these seasoned programmers are really saying, it is important to understand what comments are.
 
Comments in both programming and scripting languages provide a mechanism for the developer to write notes that are ignored by the compiler or interpreter. These notes are intended solely for either the developer or anyone else who may later need to modify the script. The main purpose of comments, therefore, is to allow the developer to make notes that will help both themselves and others who may read the script later to understand issues such as how the a particualr section of a script works, what a particular function does or what a variable is used to store. Commented a script is considered to be good practice. Rest assured that a section of script that seems obvious when you write it will often be confusing when you return to it months, or even years later to modify it. By including explanatory comments alongside the script this becomes less of a problem.
 
Now, back to that old saying - "Don't comment bad code, re-write it!". What this phrase suggest is that if code is well written in the first place you do not need comments to explain what it does. It also suggests that is you are having to write lost of comments explain what a section of your script does then you must have written is badly. Whilst one should always strive to write good code there is absolutely nothing wrong with including comments to explain what the code does. Even a well written script can be difficult to understand if it is solving a difficult problem so, ignore the old programmers adage and never hesitate comment your JavaScript scripts.

Navigation menu