Difference between requires and if..guard ? #21
-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract FundMe {
// function fund() public payable {
// require(msg.value > 1e18, "Didn't send enough"); // 1 eth = 10^18 wei
// }
function fund() public payable {
if(msg.value <= 1e18) {
return;
}
}
} What are the pros and cons of both the fund functions shown above? (The second one is inspired by traditional programming - it feels more natural to me) Any thoughts? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
"require" terminates further processing if the condition is not met, cleaner and stricter way to implement conditions that must be true and abort the transaction in case the condition isn't met. failure of the "if" condition does not terminate the processing unless explicitly mentioned in the if or the else block depending on the logic, and it allows further execution of the program. might be a good practice to implement validation checks using "require" and logical flow using "if" |
Beta Was this translation helpful? Give feedback.
-
Since solidity 0.8.* |
Beta Was this translation helpful? Give feedback.
"require" terminates further processing if the condition is not met, cleaner and stricter way to implement conditions that must be true and abort the transaction in case the condition isn't met.
failure of the "if" condition does not terminate the processing unless explicitly mentioned in the if or the else block depending on the logic, and it allows further execution of the program.
might be a good practice to implement validation checks using "require" and logical flow using "if"