Quick Guide To Solidity Comments

In this article, we will explore the importance of Solidity comments, the types of comments that can be used, and best practices for commenting your code effectively.

Comments in Solidity are an essential part of any codebase, including smart contracts.

They are used to document the code, explain how it works, and provide context for other developers who may be working on the same codebase.

Whether you are new to Solidity or an experienced developer, this article will provide valuable insights into the role of comments in writing secure and efficient smart contracts.



Solidity Comments

Solidity comments are similar to both C-style and C++-style comments and both are supported.

Solidity supports two types of comments:

Single-line Comments

A line that begins with // is treated by Solidity Compiler as a single line comment and is skipped during its execution.

Multi-line Comments

Multi-line comments start with a forward slash followed by an asterisk /* and end with an asterisk followed by a forward slash */.

The text between /* and */ is considered as a Multiple lines comment.

Here is an example of how comments can be used in solidity:

Example: 

pragma solidity ^0.8.0;contract SolidityTest{string mrx;constructor() public {mrx="Mr.Examples";}function getString() public view returns(string memory){ // The following lines in commented as we comment lines in C and C++ Programming/* * This is how we can use the multiline comments in Solidity * This is as same as how we use to comment multiple lines of code in C programming */ return mrx; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example Explanation

In above example we have defined a string variable mrx and a function getString to retrieve its value.

The line pragma solidity ^0.5.13; specifies the version of the Solidity compiler to use for this contract.

The ^ symbol means that the contract can be compiled with any version of Solidity from 0.5.13 up to, but not including, version 0.6.

The My_Contract contract has a constructor function that sets the initial value of mrx to “Mr.Examples” when the contract is deployed.

The constructor() function is a special function that is called only once when the contract is deployed to the blockchain.

The getString() function is a publicly accessible view function, which means it can be called from outside the contract and it does not modify the state of the contract. It returns the value of the mrx variable.

The comments in the code explain how to use single-line and multi-line comments in Solidity.

 

Example: 

pragma solidity ^0.8.0;contract SolidityTest { uint mrx; uint ample;// This line is used as comment hereconstructor() { mrx = 2; ample=4; }/* The Sum of Ample and Mrx will be: 2 + 4 = 6 */ function getValue() public view returns (uint sum) { return ample+mrx; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

 

Example Explanation

In this example we define a contract called SolidityTest and a function called getValue.

The HelloWorld contract has two integer variables called mrx and ample, which are initialized in the constructor.

This line is a comment that indicates the software license for the contract. In this case, it’s the GNU General Public License v3.0.

pragma solidity ^0.8.0;

This line specifies the version of the Solidity compiler that should be used to compile the contract. The ^ symbol means that the contract is compatible with any version of the Solidity compiler from 0.8.0 up to, but not including, 0.9.0.

contract SolidityTest{
// The code is written in this
}

This is the contract definition. It defines a new contract called SolidityTest.

uint mrx;
uint ample;

These two lines define two unsigned integer variables mrx and ample in the SolidityTest contract.

constructor() {
mrx = 2;
ample = 4;
}

This is the constructor function that initializes the values of mrx and ample to 2 and 4, respectively.

function getValue() public view returns (uint sum) {
return ample+mrx;
}

Above is a getValue function that calculates the sum of ample and mrx. It is marked as public, which means that it can be called by anyone, and view, which means that it does not modify the state of the contract. The function returns the sum of ample and mrx as an unsigned integer.

Solidity comment /* The Sum of Ample and Mrx will be: 2 + 4 = 6 */ is just a human-readable description of what the function does. It is not used by the compiler or the Ethereum Virtual Machine (EVM) in any way.

In conclusion, Solidity comments are an essential feature for developers to document and explain their code. They can help make the code more readable and understandable to other developers and future maintainers.

Solidity comments can be used to describe functions, variables, and contract features, as well as to provide information about the project’s overall architecture and design decisions.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *