Why is it preferred to use require instead of if for input validation/checks of functions?


if is only half the equation:

if (msg.sender != owner) {
    // What goes here?

If you're going to put a revert() in there to abort the transaction, that's equivalent to:

require(msg.sender == owner);

The latter is a bit shorter, and, though this is a matter of opinion, I find it easier to read positive assertions ("sender must be owner" as opposed to "abort if sender is not owner").

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.