Solidity

Solidity Comments

Solidity Comments Main Tips

  • Comments in Solidity, like in most other programming languages, are used for providing hints throughout the code to make it easier to read.
  • In addition to the regular type of comment, Solidity also features the natspec comment.

Solidity Comments

Solidity features single line // and multi line /*….*/ comments.

Example

// This comment spans one line

/*
This
comment
spans
multiple
lines.
*/

 

Try on Remix Try live on Hosting

There is also a comment type, called a natspec comment, documentation for which is not yet written.

To start writing a single line natspec comment, write a triple slash ( /// ) or, for a multi line natspec comment – a double asterisk block ( /**…*/) directly above statements or function declarations. It is also viable to use Doxygen-style tags in these comments for documenting functions, annotating conditions for verifying formally and providing a confirmation text that is shown to users on attempts to invoke a function.

The example below will demonstrate how to document the contract’s title, the explanations for the two parameters for input and two values that are returned after executing the statements inside the function.

Example

pragma solidity ^0.4.0;

/** @title Calculate The Shapes. */
contract shapeCalc {
    /** @dev This function is made for calculating the surface and perimeter of a rectange.
      * @param w Rectangle width.
      * @param h Rectangle height.
      * @return s Rectangle surface.
      * @return p Rectangle perimeter.
      */
    function rectangle(uint h, uint w) returns (uint p, uint s) {
        p = 2 * (h + w);
        s = h * w;
    }
}

 

Try on Remix Try live on Hosting

Read previous post:
Solidity Version Pragma

Solidity Version Pragma Main Tips Pragmas in Solidity are used to specify certain conditions under which the source file can or...

Close