Press to flip
The 'what' of the code should be commented when the code is difficult to understand or non-obvious.
Press to flip
A comment is a text ignored by the compiler and is added to explain the code and why a particular approach is taken.
Press to flip
Comments are added to explain what the code is doing, the reason why a particular approach is chosen, and to make notes for developers reading the code in the future.
Press to flip
There are two types of comments in C++: single line comments and multi-line comments.
Press to flip
Single line comments are added using two backslashes (//).
Press to flip
In Windows, use Ctrl + backslash; in Mac, use Command + backslash.
Press to flip
Single line comments can be used to comment out a single line of code for testing new versions of that line of code.
Press to flip
Multi-line comments begin with /* and end with */.
Press to flip
In Windows, use Ctrl + Shift + backslash; in Mac, use Command + Shift + backslash.
Press to flip
Unnecessary comments should be avoided, especially for obvious code, because they do not add significant value and may clutter the code.
Press to flip
It is considered a best practice to explain the 'why' of a code instead of the 'what', especially if the code is obvious.
Press to flip
The 'what' of the code should be commented when the code is difficult to understand or non-obvious.
Press to flip
A comment is a text ignored by the compiler and is added to explain the code and why a particular approach is taken.