Writing Good Comments
// Good (Comments)
Function Level Comments
These should be placed immediately before the function prototype they are describing in a header file.
Inline Implementation Comments
Sometimes it is necessary to explain a particularly complex statement or series of statements. In this case, you should use inline comments, placed either immediately before or trailing the line or lines in question. In general, prefer placing such comments before offending lines, unless the comment is quite short. These comments should start with a // followed by a space. If they are placed trailing a line, they should be separated from the end of the line by one space.
In the above example, there is a line of code that has been commented out. This is fine to do while testing, but any commented out lines of code should be removed before any merge into the master branch takes place, unless a compelling reason can be presented for them to remain.
Generally speaking, commenting each line is highly discouraged as it clutters code. Being clear and concise with comments is just as important as remembering to do comments. The general rule of thumb is that if the code itself is readable (unlike the example above), commenting why the code is written in such a matter is just as important as commenting what it does.
Notes to Other Developers (Or Yourself)
When writing code, it can sometimes be useful to leave notes to other developers or to yourself in the future. Examples of these include:
// TODO: something that should be done
// NOTE: a note about something in the code
Notes starting with these keywords (case sensitive) can be automatically highlighted by installable extensions on VSCode.
Teams Contributed to this Article:
BLRS (Purdue SIGBots)
Last updated