2.4 Comments & NatSpec
Solidity
supports single line comments and multiline comments as shown here
Comments are recommended to be used as inline documentation of what the contracts are supposed to do, what the functions, variables, expressions, various control and data flow expected to do as per the specification and what is really implemented. They can also be used to specify certain assumptions that the developer is making in the implementation and they can also represent some of the invariants that need to be maintained.
Comments become a critical part of documentation that is included or encapsulated within the code itself, affect the readability of the code to a great extent and maintainability. In fact, comments become critical when we start talking about evaluating the security of smart contracts: comments give a lot of vital clues as to what the developer intended to implement or just information related to the various syntax or the semantics itself.
Solidity
also supports a special type of comment called NatSpec which stands for Ethereum Natural Language Specification Format. These are specialized comments that are specific to Solidity
and Ethereum
. They are written as follows
and are located directly above the function declaration or statements that are relevant to the NatSpec. These NatSpec comments come in many different types: there are many different tags such as
@title
: describes the contract or the interface.@author
: specifies the developer (i.e. who is authoring the contract).@notice
: explains to an end user what the contract or function does.@dev
: directed towards the developer for any extra implementation related details.
There are also specific tags related to function parameters (@param
), the return variable (@return
) and so on... These NatSpec comments are meant to automatically generate JSON
documentation for both developers as well as users and provide a lot of valuable information that the developer intended for all these various aspects of parameters, returns, contracts and so on... They also form an important piece of the toolset that helps evaluate smart contract security.
Last updated