I l@ve RuBoard |
![]() ![]() |
3.1 CommentsIdeally, a program serves two purposes: First, it presents the computer with a set of instructions, and second, it provides the programmer with a clear, easy-to-read description of what the program does. Example 2-1 contains a glaring error. It is an error that many programmers still make and one that causes more trouble than any other problem. The program contains no comments. A working but uncommented program is a time bomb waiting to explode. Sooner or later someone will have to modify or upgrade the program, and the lack of comments will make the job ten times more difficult. A well-commented, simple program is a work of art. Learning how to comment is as important as learning how to code properly. C++ has two flavors of comments. The first type starts with /* and ends with */. This type of comment can span multiple lines as shown: /* This is a single-line comment. */ /* * This is a multiline comment. */ The other form of comment begins with // and goes to the end of the line: // This is another form of comment. // The // must begin each line that is to be a comment. The advantage of the /* */ comment style is that you can easily span multiple lines, whereas with the // style you have to keep putting the // on each line. The disadvantage of /* */ is that forgetting a */ can really screw up your code. (Remember this because it's the answer to one of the questions later in the book.) Which flavor should you use? Whichever one makes your program as clear and as easy to read as possible. Mostly, it's a matter of taste. In this book we use the /* */ style comments for big, multiline comments, and the // style is reserved for comments that take up only a single line. Whatever comment style you decide to use, you must comment your programs. Example 3-1 shows how the "hello world" program looks after comments are added. Example 3-1. hello2/hello2.cpp/******************************************************** * hello -- program to print out "Hello World". * * Not an especially earth-shattering program. * * * * Author: Steve Oualline * * * * Purpose: Demonstration of a simple program * * * * Usage: * * Run the program and the message appears * ********************************************************/ #include <iostream> int main( ) { // Tell the world hello std::cout << "Hello World\n"; return (0); } In this program, the beginning comments are in a box of asterisks (*) called a comment box. This is done to emphasize the more important comments, much like bold characters are used for the headings in this book. Less important comments are not boxed. For example: // Tell the world hello std::cout << "Hello World\n"; To write a program, you must have a clear idea of what you are going to do. One of the best ways to organize your thoughts is to write them down in a language that is clear and easy to understand. Once the process has been clearly stated, it can be translated into a computer program. Understanding what you are doing is the most important part of programming. I once wrote two pages of comments describing a complex graphics algorithm. The comments were revised twice before I even started coding. The actual instructions took only half a page. Because I had organized my thoughts well (and was lucky), the program worked the first time. Your program should read like an essay. It should be as clear and easy to understand as possible. Good programming style comes from experience and practice. The style described in the following pages is the result of many years of programming experience. It can be used as a starting point for developing your own style. These are not rules, but only suggestions. The only rule is this: Make your program as clear, concise, and simple as possible. At the beginning of the program is a comment block that contains information about the program. Boxing the comments makes them stand out. The list that follows contains some of the sections that should be included at the beginning of your program. Not all programs will need all sections, so use only those that apply.
The format of your beginning comments will depend on what is needed for the environment in which you are programming. For example, if you are a student, the instructor may ask you to include in the program heading the assignment number, your name, student identification number, and other information. In industry, a project number or part number might be included. Comments should explain everything the programmer needs to know about the program, but no more. It is possible to overcomment a program. (This is rare, but it does happen.) When deciding on the format for your heading comments, make sure there is a reason for everything you include.
|
I l@ve RuBoard |
![]() ![]() |