Pretty much all examples of contract functions I see use the require(...)
pattern. If the initial condition is not as expected, an error is thrown and the transaction is reverted.
I don't like this pattern because the consumer will not know why they got an error, especially if the function logic is complex. Is there any downside to simply using if...else + events instead?
Instead of:
function AddUser(address _user) {
require(!userExists(_user));
... add the user ...
}
I would do:
function AddUser(address _user) returns (bool) {
if (userExists(_user)) {
UserExistsEvent(_user);
return false;
} else {
... add the user ...
return true;
}
}
This way the caller will know exactly what happened in the callee function (by return value and/or event) instead of getting some generic error.
Am I missing something? Why are most functions I see follow the require/throw pattern instead? Is it cheaper?