guides:programstyle:comments
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
guides:programstyle:comments [2020/08/18 11:12] – [Comments Before Functions] wikiadmin | guides:programstyle:comments [2024/07/25 15:01] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 80: | Line 80: | ||
==== Comment Placement in Code ==== | ==== Comment Placement in Code ==== | ||
- | Single line comments should be placed before sections of code which need explanation. | + | Single line comments should be placed before sections of code which need explanation. |
+ | Comments should begin in line with the code which is being documented: | ||
+ | |||
+ | An acceptable example: | ||
+ | <code c++> | ||
+ | // Read the input from the user and ignore any extraneous output | ||
+ | cin >> word; | ||
+ | cin.ignore(1000,' | ||
+ | </ | ||
+ | |||
+ | An unacceptable example: | ||
+ | <code c++> | ||
+ | // Print a line of the story filling in the blank | ||
+ | cout << line1Part1 << nounResponse << " " << line1Part2 << endl; | ||
+ | </ | ||
+ | |||
+ | Comments should generally not be placed on the same line as code. This guideline may be violated when declaring variables. | ||
+ | |||
+ | The following example is not acceptable. | ||
+ | <code c++> | ||
+ | cout << endl; // done with output, print an endl | ||
+ | </ | ||
+ | |||
+ | Use comments to supplement the understanding of major sections of code. Only rarely should comments be used on every line of code. The following is not an acceptable use of documentation. | ||
+ | <code c++> | ||
+ | // Get the noun from the user | ||
+ | noun = GetWord(" | ||
+ | // Get the adjective from the user | ||
+ | adjective = GetWord(" | ||
+ | // Get the action verb from the user | ||
+ | actionVerb = GetWord(" | ||
+ | </ | ||
+ | |||
+ | The code above is mostly self documenting due to identifier choice, so a single comment would be more appropriate | ||
+ | <code c++> | ||
+ | // Get the words from the user | ||
+ | noun = GetWord(" | ||
+ | adjective = GetWord(" | ||
+ | actionVerb = GetWord(" | ||
+ | </ | ||
+ | |||
+ | |||
+ | Finally, most comments in code should employ a c++ style line comment. | ||
+ | |||
+ | ==== Comment Contents ==== | ||
+ | |||
+ | Comments should describe the code, however comments should not repeat the code. | ||
+ | |||
+ | An acceptable example: | ||
+ | <code c++> | ||
+ | // use the Pythagorean theorem (a^2+b^2 = c^2) to find the length of the third size. | ||
+ | hypot = sqrt(sideA * sideA + sideB * sideB) | ||
+ | </ | ||
+ | |||
+ | An unacceptable example: | ||
+ | <code c++> | ||
+ | // c = sqrt(a*a + b*b) | ||
+ | hypot = sqrt(sideA * sideA + sideB * sideB); | ||
+ | </ | ||
+ | In the above example, the comment provides NO additional information. | ||
+ | |||
+ | |||
+ | In general, comments should support a knowledgeable programmer. |
guides/programstyle/comments.1597749158.txt.gz · Last modified: 2024/07/25 15:01 (external edit)