What is a a Comment?
Comment is a term that has two meanings in computers: a comment on social media, and a comment in programming.
What is a Comment on Social Media?
A comment on social media is a kind of post that is always attached to another post, e.g. comments on Facebook, Instagram, TikTok, or Youtube. Comments are generally posted by a different user than the user who posted original post, the original poster (OP), so it's one user commenting on the post of another.
In some platforms, there are threaded comments, such that a comment can be a reply to another creating a thread of comments (or replies), e.g. Reddit.
There are also social media that allows posts to be replies, but the replies are not comments, in the sense that the replies are independent posts that will continue existing if the original post is deleted, e.g. Twitter and Tumblr.
Comments predate social media. Blog posts posted on blogs, and articles posted on news websites, already had a "comments section" with readers' comments before modern social media became popularized.
What is a Comment in Programming?
A comment in programming is a piece of arbitrary text written in source code that is ignored by the program that interprets the source code. Programmers write comments so they can leave notes for themselves about why a piece of source code was written, or elaborate what it's supposed to do. This is also called documentation.
// this is a comment.
// I can write anything I want here and it will be ignored.
// No errors.
var x = 10;
// the line above sets the variable X to the value 10.
Although comments are ignored by one interpreter, there are interpreters that are designed to target comments specifically, such that you can have one file that has code for two different programs: one everywhere EXCEPT the comments, and one that is only INSIDE the comments.
/**
* Multiplies two values.
* @param {number} foo - one factor.
* @param {number} bar - another factor.
* @returns {number} the product.
*/
function multiply(foo, bar) {
return foo * bar;
}
Above, everything between /*
and */
is a comment. The language above is Javascript, so a program that interprets Javascript source code will ignore the comment. Inside the comment, we have some text code like @param
. This would be interpreted by another program which specifically reads the comments. In this case, it would be JSDoc, that generates documentation from comments.
In order for comments to work, they must be delimited in a way the interpreter knows where the comment starts and ends.
In some cases we have single-line comments, where a line that starts with a character or character sequence, such as //
, #
, or ;
, is treated as a comment. Note that while #
marks the start of a comment in Python, it doesn't in Javascript or C, so different languages use different characters. The //
is the most common one because the C programming language uses it, and a lot of languages are inspired by C.
In some cases, there is a way to mark the start and the end of the comment. For example, with /*
and */
in C, or <!--
and -->
in HTML and XML.
It's worth noting that not all languages support both ways. CSS for example doesn't support single-line comments, while Zig doesn't support multi-line comments.
Leave a Reply