Difference Between require, revert, and assert in Solidity!

Difference Between require, revert, and assert in Solidity!

ยท

2 min read

Introduction

Hey folks, in this article we're gonna explore the differences between require, revert, and assert functions in Solidity! If you aren't aware about Solidity, then you can get a brief idea about it by clicking here.

  • What is require function?

  1. The require function is used to verify some conditions before any statement is executed. If the given condition tends to be false, then it would give an error and the execution would stop.

  2. In short, require statements declare the prerequisites for running the function, which has to be satisfied to work.

  3. During the event, the execution is terminated due to a false condition, the unused gas is returned to the caller and the state is reversed to the original state.

For Example-

require(num == 23, "The number entered is incorrect!");

Advantages

  1. Imply conditions before execution.

  2. Authenticate user responses.

  • What is revert function?

  1. The revert function does not evaluate any condition and does not depend on any state or statement.

  2. It is used for handling more complex operations.

  3. When a revert statement is called the unused gas is returned and the state reverts to its original state.

For Example-

revert("There's an error!");

Advantages

  1. Unused gas is returned.

  2. Capable of handling complicated operations.

  • What is assert function?

  1. The assert function is used to check those statements which can never be false.

  2. It plays a vital role in preventing impossible scenarios. The output is declared in boolean form.

  3. In contrast to require and revert functions, assert doesn't return any unused gas instead it consumes the gas to reverse the program to its original state.

For Example-

assert(value > 345);

Advantages

  1. Prevents impossible scenarios.

  2. Used for cases where statements can never be false.

I hope that you must have found this article quite helpful. If yes, then do give a read to some of my other articles!

Who knows you might become a great programmer ๐Ÿค”!

ย