- 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
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.
// The next five lines ask web visitors for their favorite // color and Steinbeck novel
/* 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. */
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.