Language: EN

programacion-comentarios

What are comments in code

Comments are pieces of text included in a program’s source code, but are not executed or affect the program’s operation.

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 program’s execution.

Almost all languages incorporate the possibility of adding comments to the code. They are a tool that allows developers to communicate with other programmers and leave explanatory notes within the source code.

Comments provide additional information about a program’s operation and help better understand its logic and structure.

There are two main types of comments in programming: single-line comments and block comments.

Single-line comments

Single-line comments are 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

Multiline 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 C-derived languages, /* and */ are used

/*
This is a comment
of multiple lines
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 the start or end indicator of a block comment

'''
This is a comment
of multiple lines
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
' of multiple lines
' It is used to provide
' more extensive explanations
Dim myVariable = 25

Best practices for using comments

While comments are a very useful tool to make code easier to understand, it is important not to abuse them.

Comments can become a problem if they are used excessively or incorrectly.

  • Use comments to explain the why of the code, not the how. The code should be clear enough to explain how it works, without the need for additional comments.
  • Use comments to explain parts of the code that are difficult to understand or have significant complexity.
  • Make sure that comments are accurate and up to date. If you change the code, make sure to also update the comments.
  • Avoid commenting out obvious code. Comments should provide additional information, not repeat what can already be seen in the code. In other words, do not do this:
int myVariable = 25; // assign 25 to myVariable
  • Do not use comments to deactivate code. If you have code that is not working correctly, it is better to remove it or temporarily comment it out to fix it in the future.