Skip to content
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

Closed
guylando opened this issue May 7, 2019 · 7 comments
Closed

Documentation unclear on when to use require() vs revert() #6689

guylando opened this issue May 7, 2019 · 7 comments
Assignees

Comments

@guylando
Copy link
Contributor

guylando commented May 7, 2019

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 for require(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

@ChrisChinchilla
Copy link
Contributor

It's a draft PR @guylando that is mostly doing some document rearranging now. But is this any better in helping you understand things?

@guylando
Copy link
Contributor Author

guylando commented May 27, 2019

@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.
The difference between require and assert or assert and revert are clear. The difference between revert and require is also clear. What is not clear is when to use if+revert and when to use require, because according to the documentation it is the same.

@chriseth
Copy link
Contributor

chriseth commented May 27, 2019

@guylando do you want us to mention something like

"if (!x) revert(msg);" and "require(x, msg);" are equivalent
(up to a potential side-effect of evaluating `msg`).
When to use which is fully up to the developer's taste.

@guylando
Copy link
Contributor Author

@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, "..") ?"

@chriseth
Copy link
Contributor

@guylando because having this freedom of choice makes some code better readable. Why do we have for if everything can be done with while?

@guylando
Copy link
Contributor Author

guylando commented May 27, 2019

@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 for and when to use while)

Currently seems the best practice is to use require for input validation but there does not seem to be a best practice of when to use a revert. Maybe a function which ALWAYS reverts to not write require(1!=1, "..")? that is the only thing that comes to mind.

@ChrisChinchilla
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants