Apps/Gaming

How to Manage Visibility of Variables and Functions in Solidity

Visibility modifiers determine the level of access to the variables and functions in your applications and codebase. These access modifiers are necessary to protect which smart contracts can see the data and behavior of a given contract.

Solidity has three levels of visibility for state variables and four levels for functions. In this Solidity programming tutorial, developers will learn how to use these modifiers.

We covered the concept of inheritance in solidity in a previous article. We recommend you give that a read as well.

State Variable Visibility in Solidity

To define the visibility of a state variable in Solidity, simply precede the variable name with the modifier, as shown in the sample code below:

// Unlike in Java, // the data type comes before the visibility modifier in Solidity uint public num;

There are three visibility modifiers that developers can use: public, internal, or private.

    • Public means that the variable can be accessed by the contract and by other smart contracts. When accessing the variable internally, simply use the variable name (eg var). When accessing it externally, use the notation this.var. It is worth noting that a getter function is always created for a public variable. Therefore, this function is called whenever a variable is accessed externally. The getter function created has the same name as the public variable and it takes in no arguments. However, no setter function is created, implying that the external function can not modify the variable.
    • Internal means that the variable can only be used with the contract it is defined in and its subclasses.
    • Private means that the variable can only be accessed within the contract it is defined. Trying to access it outside this contract gives a compilation error.

Function Visibility in Solidity

There are two types of function calls in Solidity: internal and external. External function calls make an actual EVM call, while internal function calls do not make an EVM call. The level of visibility of internal calls to subclasses can be restricted, thereby making the number of visibility modifiers four.

The access modifiers in Solidity are: external, public, internal and private:

      • An external function can be accessed from other contracts using transactions. This type of function can be accessed using the this.functionName() notation and not just functionName().
      • A public function can be either called internally or via an EVM message call.
      • An internal function can be accessed in the contract in which it is defined and in its subclasses.
      • A private function can only be called from the contract in which it is defined.

The Solidity code example below re-enforces all of the concepts that we have learned in the above sections:

// SPDX license identifier: GPL-3.0 pragma solidity >=0.5.0 <0.9.0; contract A{ unit private a; function f1(unit x, unit z) internal returns (unit){ return x + z; } function f2(unit y) private returns(uint b){ return 6*y ; } } contract B { function f3() public{ A crtA = new A(); unit var = crtA.f2(89); // compilation error } } contract C is A { function f4() public { A contA = new A(); uint var2 = f1(74, 51); } }

Thoughts on Variable and Function Visibility in Solidity

Visibility is necessary to limit the level of access to the variables and functions in your Solidity applications. You should always use the principle of least privilege when determining which level of visibility to use. That is, assign only as much visibility as is necessary and no more.

Lastly, it is important for Solidity developers to know that private or internal visibility simply restricts the access other contracts have to your contract’s data. The data is, however, still publicly viewable on the blockchain.

Reading: Best Online Python Courses

Related posts

Axosoft Scrum Project Management Tool Review

TechLifely

Agile versus Scrum: How Do They Compare?

TechLifely

JavaScript output

TechLifely

Leave a Comment