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 Answer
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 SCommented Mar 14, 2016 at 17:00