If so, why was the name require
chosen instead of just assert
like most modern programming languages?
Where does the name require
originate from?
Solidity contains both.
require(false)
is handled as a user-provided exception (like throw
).
assert(false)
is handled as a run time exception (like out of gas, among others).
Internally, Solidity performs a revert operation (instruction
0xfd
) when a user-provided exception is thrown or the condition of a require call is not met. In contrast, it performs an invalid operation (instruction0xfe
) if a runtime exception is encountered or the condition of an assert call is not met. In both cases, this causes the EVM to revert all changes made to the state. The reason for this is that there is no safe way to continue execution, because an expected effect did not occur. Because we want to retain the atomicity of transactions, the safest thing to do is to revert all changes and make the whole transaction (or at least call) without effect.
(http://solidity.readthedocs.io/en/develop/control-structures.html#exceptions)
The difference between revert and invalid is described in this answer.
INVALID will throw the transaction, its state changes and cause the consumption of all gas sent with it.
As of Solidity 0.4.10 there is now a revert() call which enacts the REVERT opcode introduced in EIP140. This opcode, halts the contract, throws state changes but refunds unused gas.
It's basically a short form. The idea likely originates from "requiring things to be there", which checks for a boolean condition, but also can check things the
assert
way.
I'd never recommend to use require, hence it was caught working unpredictable. I used it to ensure two addresses are equal, and require failed even they were, whilst if ... throw worked fine for the same. I killed a day to find it. Thanks to unit tests on my contracts that revealed the problem before deployment.
require