Solidity Smart Contract Visibility Specifiers

ยท

3 min read

Solidity Smart Contract Visibility Specifiers

What is Visibility

In the context of programming, visibility refers to the accessibility of a variable, function, or another programming element from different parts of a program. Choosing the appropriate level of visibility for a programming element is important for several reasons. For one, it can help to prevent accidental or unauthorized access to sensitive data. It can also help to improve the organization and structure of a program by limiting the scope of certain variables or functions. In this article, we would be looking at visibility as it relates to solidity.

Types of Solidity Visibility

In solidity, we have four different types of visibility, which specify the accessibility of an element they include

  • External

  • Public

  • Internal

  • Private

Public Functions and Variables:

Public functions and variables can be accessed from within the contract and from outside the contract. This means that anyone can call these functions and variables. Public functions are commonly used for getter and setter methods.

Here's an example of a public function and variable in Solidity:

contract PizzaOrder {
    uint public orderNumber;

    function setOrderNumber(uint _number) public {
        orderNumber = _number;
    }
}

Private Functions and Variables:

Private functions and variables can only be accessed from within the contract. This means that external parties cannot call these functions and variables. Private functions are typically used for helper functions that should not be exposed to the outside world.

Here's an example of a private function and variable in Solidity:

contract PizzaOrder {
    uint private orderNumber;

    function _setOrderNumber(uint _number) private {
        orderNumber = _number;
    }
}

Internal Functions and Variables:

Internal functions and variables can only be accessed from within the contract and any contracts that inherit from it. This means that external entities cannot call these functions and variables. Internal functions are typically used for helper functions that need to be accessed by contracts that inherit from the current contract.

Here's an example of an internal function and variable in Solidity:

contract PizzaOrder {
    uint internal orderNumber;

    function _setOrderNumber(uint _number) internal {
        orderNumber = _number;
    }
}

contract PizzaDelivery is PizzaOrder {
    function setOrderNumber(uint _number) public {
        _setOrderNumber(_number);
    }
}

External Functions

External functions can only be called from outside the contract. This means that only external parties can call these functions. External functions are commonly used for functions that interact with other contracts or send ether.

Here's an example of an external function in Solidity:

contract PizzaOrder {
    function deliver() external {
        // Deliver the pizza
    }
}

Conclusion: Visibility modifiers play an essential role in Solidity smart contracts by regulating access to functions and variables. By using the right visibility keyword, developers can control how external parties interact with their contracts, improving security and efficiency. Whether you're a beginner or an experienced developer, understanding the different visibility modifiers available in Solidity is crucial for building robust and effective smart contracts.

website: https://kyilax-portfolio.vercel.app

Linkedin: https://www.linkedin.com/in/daniel-osariemen-095772210/

Twitter: https://twitter.com/thexovc

ย