thinkingincode.ninja [Dark]

Comments

Comments are not for the computer, but rather for the programmer, and anyone reading the code. A programmer can make notes and provide documentation using comments. Different languages have different ways of indicating comments, but there are two kinds of comments: single-line, and multi-line. Single-line comments extend from the comment marker through the end of the line. Multi-line comments extend from the opening comment marker through the closing comment marker. The most common multi-line comment marker is /* for the opening marker, and */ for the closing marker, while the two most common markers for single-line comments are // and #. Consider:

# This is a comment
int i = 0;

// This is also a comment
/*
// This will not be executed because it is
// commented out by the multi-line comment
i = 2;
Actually, because this is a multi-line
comment, you don't even need to use
the single-line comment markers
*/

string message = "Some text"; # This comment is at the end of a line of code

Proper use of comments is vital in just about every program, because they allow the programmer to make their intentions known to the reader. Code is written for some reason, and comments are how those reasons can be made known. Comments can also serve to inform the reader of certain pitfalls or other issues with the code; while a programmer should generally try to fix bugs that they know about, there are times where that just isn't possible at the point in time that the programmer is aware of the bug. Comments also can be used to record to-do items that the programmer doesn't want to forget.

Suggested next reading: Functions