Comments are used in most programming languages to describe a line of code or a code block. This helps developers to understand the code better, which is especially important if a team is working on the code. It makes the code also more maintainable because the code is easily understandable even after years. In Solidity there are three types of comments:
The main difference between the three types of comments is, that regular comments (single-line and multi-line comments) are ignored by the Solidity Parser. They are not included in the bytecode. NatSpec comments
on the other hand are compiled to machine-readable code to provide rich documentation.
To comment a single line in Solidity, use //
at the start of the line. The whole line will then be ignored by the Parser and can be used to comment on the code. Sometimes multiple single-line comments are used to comment on multiple lines however, it's best practice to use multi-line comments instead.
// this is a single-line comment in a solidity contract
A single-line comment is terminated by an end-of-line character like LF, VF, FF, CR, NEL, LS, or P.
To comment multiple lines in Solidity, begin the comment with /* and end the comment with */ to finish the comment block.
this is a
in a solidity contract
Solidity contracts can include another kind of comment, the so-called NatSpec comments. They are written using triple slashes (///) or double asterisk blocks (/** ... */) and are used above functions to document them. Read more about NatSpec comments.