-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation unclear on when to use require() vs revert() #6689
Comments
It's a draft PR @guylando that is mostly doing some document rearranging now. But is this any better in helping you understand things? |
@ChrisChinchilla Maybe I am missing something but I still don't see any explanation on when to use require and when to use revert. Randomly choosing which one to use feels bad for me as a developer. |
@guylando do you want us to mention something like
|
@chriseth if that is the case then it raises the question "But if they have no real difference and it is only a syntactic difference then why does revert(..) exist if it is just a syntactic sugar for require(1!=1, "..") ?" |
@guylando because having this freedom of choice makes some code better readable. Why do we have |
@chriseth that is legit. For someone who likes to write code according to the best practices, reading the current documentation made it seem as though some patterns of use or tips on decision making between the use of revert and require were not stated. I now see that they were not stated because they do not exist so that is legit, just wasn't clear beforehand. And when fallbacking to searching in google, finding this misleading answer: https://ethereum.stackexchange.com/questions/15166/difference-between-require-and-assert-and-the-difference-between-revert-and-thro/50957#50957 made the confusion even worse (I am pretty sure there ARE known patterns of use / best practices of when to use Currently seems the best practice is to use |
I can't really speak much for language design choices, as that's not really my area, and I would probably use the answer from @chriseth as more definitive (if not giving you the conclusive answer you are looking for) in preference of the SO thread. So I'll take a little of what he added and polish that PR some more. |
The documentation here: https://solidity.readthedocs.io/en/v0.5.8/control-structures.html#error-handling-assert-require-revert-and-exceptions
does not have a clear explanation of when to use require and when to use revert. It gives an example of performing the same thing once with require and another time with revert. But if they have no real difference and it is only a syntactic difference then why does
revert(..)
exist? Is it just syntactic sugar forrequire(1!=1, "..")
?Looked for it online and the only place where I saw information on this is here: https://ethereum.stackexchange.com/questions/15166/difference-between-require-and-assert-and-the-difference-between-revert-and-thro/50957#50957
where it is written that "revert is reserved for error-conditions that affect business-logic. For example someone sends a vote when the voting is already close". However I don't see a logical explanation of why such conditions shouldn't be checked and reverted using require statement?
Maybe the use of revert is to emit an event after the if and before the revert (which is not possible in require)? or will the emitted events get reverted and deleted as well?
Thanks
The text was updated successfully, but these errors were encountered: