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.

  • Improving readability by eliminating duplication, that's it. – ferit May 20 '18 at 20:19
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.

  • 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 Mar 14 '16 at 17:00

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.