0

I recently read a smart contract from Openzepplin with the following code in it


    uint256 private certainAmount;
    

    function getCertainAmount() public returns (uint256){
        return certainAmount;
    }

    
    function usesCertainAmount() public {
        uint256 calculation = getCertainAmount() + 200;
        /***/
    }

as you can see they are using the getter of the private function even inside the same contract, and I can't seem to understand why?

2

The main reason is that if those are functions, you can override them in inheriting contracts more easily. If they were just variables in a function, you would need to rewrite the whole function - now you just need to rewrite the variable part.

This is a quite common pattern with OpenZeppelin: they make their contracts "pluggable", so you can easily inject your own functionality in places. They even have sometimes empty function implementations just because of this.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.