5

Is there any reason for why Solidity has modifiers instead of just having a normal function which is called in the first line of a function body? The docs motivate it with readability but I do not see how that justifies adding a new keyword for a function and the weird underscore syntax.

1
  • Improving readability by eliminating duplication, that's it.
    – ferit
    Commented May 20, 2018 at 20:19

1 Answer 1

7

You are not required to use modifiers.

The underscore syntax looks weird at first, but not that hard to work with once understood (think about it as including the function body there). They can be used as cleanup methods too, where you put the underscore in the front. (Note: a return in the body will cause the cleanup not to be executed.)

The nice thing about modifiers is that you can have multiple of them in one line and easily understand the intent & signature of the method in one line:

function test(uint x) owneronly valueonly cleanup returns (uint y) {

vs.

3 methods being called in the function body.

1
  • After I started using them I started asking myself "why don't other languages have them"... Say what you want about multidimensional arrays in solidity, but this feature I like...
    – Paul S
    Commented Mar 14, 2016 at 17:00

Your Answer

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

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