Comments

Program comments are pieces of the program text that the interpreter ignores but that provides useful documentation to other programmers. Most programmers at least include their name and a brief summary of the program. This type of program is called a docstring. A docstring is a multi-line string in comment form. There is also end-of-line comments that can document a program. These comments begin with # and extend to the end of a line. They are often used to notate certain lines of code such as variables or expressions.

Input
Output

Good documentation is essential in a program. Ideally, program code is self-documenting, so a human reader can instantly understand it. The trick is to avoid documenting code that has an obvious meaning but also to aid the reader when the code alone might not provide enough information. With comprehension, it’s good to keep following in mind:

  • Begin a program with a statement of its purpose and other information that would help orient a programmer called on to modify the program at some future date.
  • Accompany a variable definition with a comment that explains the variable’s purpose
  • Precede major segments of code with brief comments that explain their purpose.
  • Include comments to explain the workings of complex or trick sections of code.

Leave a comment