Comments are text fragments that are included in the source code of a program but do not execute or affect the functioning of the program itself.
Their main purpose is to provide explanations, clarifications, or annotations about the written code, facilitating understanding for both the original programmer and other developers who may read or modify the code in the future.
Comments are ignored by the compiler or interpreter, so they have no effect on the execution of the program.
Almost all programming languages incorporate the ability to add comments to the code. They are a tool that allows developers to communicate with other programmers and leave explanatory notes within the source code.
There are two main types of comments in programming: single-line comments and block comments.
Single-line comments
Single-line comments are those used to add brief notes or clarifications in a single line of code.
In most programming languages, special characters are used to indicate the beginning of a single-line comment.
It is very common to use a double slash //
in languages derived from C, such as C++, Java, or C#
// this is a single-line comment
int myVariable = 25; // it can also be placed at the end
But it is not the only case. For example, in Python, #
is used
# this is a single-line comment
myVariable = 25 # it can also be placed at the end
While SQL or VB use the separator '
' this is a single-line comment
Dim myVariable = 25 'it can also be placed at the end
Multi-line comments
Multi-line comments are used when it is necessary to add more extensive explanations or comments that span multiple lines of code.
In most programming languages, multi-line comments are enclosed between a start delimiter and an end delimiter.
In languages derived from C, /*
and */
are used
/*
This is a multi-line comment
It is used to provide
more extensive explanations
*/
int myVariable = 25;
But again, it is not the only possible option. Python, for example, uses '''
as both a start and end indicator for a block comment
'''
This is a multi-line comment
It is used to provide
more extensive explanations
'''
myVariable = 25
On the other hand, not all languages have multi-line comments. In this case, we simply comment out all the lines we want. For example, in VB
' This is a comment
' spanning multiple lines
' It is used to provide
' more extensive explanations
Dim myVariable = 25
Best practices Tips
While comments are a very useful tool for making code easier to understand, it is important not to overuse them.
Comments can become a problem if used excessively or incorrectly.
Good practices in commenting 👍
- Use comments to explain the why of the code, not the how. The code should be clear enough to explain how it works without needing additional comments.
- Use comments to explain parts of the code that are difficult to understand or that have significant complexity.
- Ensure that comments are accurate and up-to-date. If you change the code, make sure to update the comments as well.
Things to avoid 🙅
- Avoid commenting on code that is already obvious. Comments should provide additional information, not repeat what can already be seen in the code. That is, do not do this:
int myVariable = 25; // assign 25 to myVariable
- Do not use comments to disable code. If you have code that does not work correctly, it is better to remove it (do not leave commented-out code lying around)
- If you are going to comment out a piece of code temporarily because you are working on it (it happens to everyone). But do not leave it commented out forever, delete what you do not need when you finish.