What is the benefits from writing repetitive validation as functions instead of modifiers? The documentation clearly recommendations to use modifiers yet 'best practice' open source parties such as open zeppelin use functions some times to validate.

I found below in the general 'crowdsale.sol' contract by openZeppelin

 function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
    require(_beneficiary != address(0));
    require(_weiAmount != 0);

The content of the function, why isn't it just moved to a modifier? is it cheaper to implement through function? what is the theory behind this decision.

3 Answers 3


An important aspect not mentioned so far is that modifiers clog up the stack, whereas functions do not. If not used properly, modifiers can be one of the causes of the following error:

Stack too deep, try removing local variables.

This has previously been discussed in these threads:

Therefore, this could be one of the reasons OpenZeppelin prefers input validation via functions.

  • > Therefore, this could be one of the reasons OpenZeppelin prefers input validation via functions. Can you give an example? Dec 12, 2021 at 10:21
  • @MiratCanBayrak see OP's question. Dec 12, 2021 at 20:57

This may not be a definitive answer (there may be other reasons), but Vyper disallows the use of modifiers because:

Modifiers - eg. in Solidity you can do function foo() mod1 { ... }, where mod1 can be defined elsewhere in the code to include a check that is done before execution, a check that is done after execution, some state changes, or possibly other things. Vyper does not have this, because it makes it too easy to write misleading code. mod1 just looks too innocuous for something that could add arbitrary pre-conditions, post-conditions or state changes. Also, it encourages people to write code where the execution jumps around the file, harming auditability. The usual use case for a modifier is something that performs a single check before execution of a program; our recommendation is to simply inline these checks as asserts.

Additionally, there may be instances where you only really need to validate using requires sometimes, as opposed to upon the execution of every single function.

  • I'm not sure this is relevant for the solidity language, however is the viper language going to be production ready soon? what is the state of it
    – NowsyMe
    Feb 27, 2018 at 17:28

This is purely syntax sugar - whether you use modifiers or check conditions in functions doesn't affect the amount of gas used or any other user experience. Modifiers are used for encapsulation of some commonly used logic - like if you need to check for the same conditions in several functions, you can either add those conditions checks to each of those functions, or you can create one Modifier and apply it to all of them. This way, if your conditions change, you only have to refactor them in one place. Again, Modifiers are purely for better readability, no difference otherwise.

  • It is more modifier vs single function with checks which is then called in other functions.
    – NowsyMe
    Feb 27, 2018 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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