0

If I'm wrapping functions, like below, does it make a difference whether I check the input argument in the private function or the external one? Will it save me gas cost to do it in the external one because it will revert the function the earliest?

contract Example {


    function wrapperForFunction(uint256 exampleArg) external {
        //require(...) check best here ?
        _function(exampleArg);
    }
    
    function _function(uint256 exampleArg) private {
        //require(...) check best here ?
    }
}
0

If there is additional code in wrapperForFunction before _function is called, it can save some gas when you call require sooner. If it's just a wrapper for _function and you don't do any additional stuff, the difference in gas is neglectible.

Using the following contract, the total transaction cost is 21422 gas according to Remix.

contract Example {
  function wrapperForFunction() public {
    _function();
  }
  
  function _function() private {
    require(false);
  }
}

When moving the require to the wrapperForFunction function, the transaction cost is 21407, a difference of 15 gas.

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.