- Get Ready to Program
- What a Computer Program Does
- Common Programming Misconceptions
- Many Programs Already Exist
- Programmers Are in Demand
- The Real Value of Programs
- Users Generally Don't Own Programs
- Giving Computers Programs
- Your First Program
- Clarifying Comments
- Entering Your Own Program
- Summary
- Q&A
- Workshop
Clarifying Comments
The program presented in Figure 1.2 not only has program statements, but also some lines that do not do anything when the program is run by the interpreter, but have great value to anyone reading your code. These lines start with either a slash and asterisk (/*) or two consecutive slashes (//) and are known as comments.
You may be asking if there’s any difference between the slash and asterisk comment and the double-slash comments. Taking the second type first, the double-slash comment is known as a single-line comment. JavaScript will ignore all text to the right of the two slashes. As soon as you hit return, JavaScript will start paying attention to your code again. So if you want to keep commenting, you will need to start the next line with a fresh pair of slashes.
// The next five lines ask web visitors for their favorite // color and Steinbeck novel
In the previous example, if you left off the two slashes, JavaScript would assume you were either typing color or calling a function named color and you might not get the results you were expecting.
When JavaScript encounters a comment that begins with the slash and asterisk, it will treat everything that follows as a comment until it sees a closing asterisk and slash (*/). This can be on the same line:
/* Next, the code calculates the area of the circle. */
Or it can go on for several lines:
/* This section is used to enter a contact The user can enter a name, physical address, up to 5 email addresses, home, work, and cell phone numbers, and any additional comments to help remember how they know the person. */
All languages support comments, which are also called remarks, although some languages use an apostrophe instead of the slashes. Although you’ll find many comments in programs, and you should put many comments in the programs you write, comments are not for the interpreter or for the computer. Comments are for people. In JavaScript, for example, the interpreter ignores all text to the right of the slashes or slash/asterisk. The computer knows that comments are for you and not for it.
Reasons for Comments
Comments help produce more understandable program listings. The programming industry considers the inclusion of comments to be one of the most important good programming habits you can develop. Here are common reasons programmers put comments in their programs:
- Comments can identify programmers—If you develop programs with others or write programs for a company, you should put your name and contact information in comments at the top of your programs. Later, if someone has a question about the program or wants you to change the program, they’ll know that you wrote the program and they can locate you.
- Before a section of tricky code, you can use comments to include your explanation of the code’s purpose. Later, if you or someone else needs to change the program, the change will be easier; reading the comments is much simpler than trying to figure out the goals of the tricky code from uncommented code.
Placement of Comments
When you use the double slashes (//), a comment can appear on the same line as code. The following code shows comments to the right of lines of other program instructions. Notice that double slashes precede the text. These comments explain what each line of code does.
if hours > 12 // Test the hour for AM/PM indicator hours = hours - 12 // Convert 24-hour time to 12-hour amOrPm$ = " PM" // Set indicator that will print
Sure, as a programmer, you will often understand code without the need to put a remark to the right of each line. But at the time you write a program, the program is clearest to you. When you later go back to edit a program, what may have been clear before is no longer clear because time has passed. A few comments will go a long way toward reminding you of your original intent.
However, do not overdo comments. Some comments are redundant, as the following shows:
document.write("Martha"); // Prints the word Martha
Such a comment is not helpful and serves only to cloud an already-understandable instruction. Don’t overuse comments, but use them generously when needed to clarify your code.