Assert vs Require in Solidity – Learn the Difference [Best 2021 Guide]

ASSERT VS REQUIRE In Solidity Featured Image

In this guide, I will explain the difference between assert vs require in Solidity.

Because Solidity, Blockchain, Ethereum, and everything else connected to these are relatively new to beginners, it might be difficult to comprehend what is going on and why.

When compared to other programming languages, the Solidity programming language is likewise very young.

There are several programming languages, some of which we as developers may have never heard of. This is due to the recurrent occurrence of large-scale discoveries, as well as the creation of new programming languages. Solidity is only one of many wonderful programming languages accessible.

Overview of the Solidity Programming Language

Solidity is an object-oriented programming language used to create smart contracts on the Ethereum network, also known as the Ethereum Virtual Machine (EVM). According to the official explanation, it is a curly brace language.

Since it is connected to those technologies, Solidity will be easier to understand if you are familiar with Python, C++, or JavaScript. This language, like most others, supports inheritance, libraries, tools, and complex user-defined types, making programming easier.

Solidity files are identified by the ‘.sol’ file suffix. It is a programming language that is statically typed. As of this writing, the most current stable version is Solidity v0.8.9. Click here to visit the official Solidity documentation.

Let us now move on to our guide to understanding Assert vs Require in Solidity.

Assert vs Require in Solidity

If you have used the assert() and the require() functions in Solidity, then some of you must have come across these really weird twin-like functions. Well, fret not, I am here to clear it all out for you guys today.

Well to start off, both the functions were introduced even before the major Byzantium Hardfork of Ethereum back in October 2017. Although many developers argue both these functions are the same or are used alternatively, that is not the fact. I mean that is exactly why they exist separately, right?

What was the Byzantium Hard Fork of Ethereum (2017)?

The Byzantium hard fork is a blockchain upgrade that was deployed in October 2017 at block 4,370,000. It was made up of nine Ethereum Improvement Protocols (EIPs) that were designed to improve Ethereum’s privacy, efficiency, scalability, and security.

Before the upgrade, both these functions behaved identically. This was the reason the assert vs require in Solidity topic sprouted.

However, they were compiled into different opcodes. The main difference the Byzantium upgrade gave birth to was that smart contracts that were deployed before the hard fork or upgrade, behaved slightly differently.

Let us dive deeper into our assert vs require in the Solidity guide to learn what that difference was!

Main Differences Between Assert vs Require In Solidity

Let us now take a deeper look at the ultimate comparison of assert vs require in Solidity.

1. Behavior of assert and require functions

The assert() and require() functions are a part of the error handling aspect in Solidity. Solidity makes use of state-reverting error handling exceptions. This means all changes made to the contract on that call or any sub-calls are undone if an error is thrown. It also flags an error.

They are quite similar as both check for conditions and if they are not met, would throw an error.

2. Gas Utility

The big difference between the two is that the assert() function when false, uses up all the remaining gas and reverts all the changes made.

Meanwhile, a require() function when false, also reverts back all the changes made to the contract but does refund all the remaining gas fees we offered to pay. This is the most common Solidity function used by developers for debugging and error handling.

3. Syntax

Before the two functions were introduced, smart contract developers wrote error handlers that looked something like this:

If (msg.sender != manager) { throw; }

Well, the throw keyword was then starting to deprecate and many switched to assert and require. The same code above can now be written like this.

With assert():

assert(msg.sender == manager);

With require():

require(msg.sender == manager);

When to Use require() and assert()?

At the end of the day, it all falls down to just one thing, when should both these functions be used? This is apparently the most crucial discussion.

Well, fortunately, the Solidity documentation states it very clearly.

  • The assert function should only be used to examine invariants and test for internal problems.
  • The require function should be used to check return values from calls to external contracts or to guarantee that valid conditions, such as inputs or contract state variables, are satisfied.

Analysis tools, when used correctly, can assess your contract and discover the conditions and function calls that will result in a failed assert. A properly running program should never reach a failing assert statement; if this occurs, there is a flaw in your contract that has to be addressed.

Read More: Generate a Random Number in Solidity

Conclusion

Solidity is an object-oriented programming language used to create smart contracts on the Ethereum network, also known as the Ethereum Virtual Machine (EVM).

If you must have used the assert() and the require() functions in Solidity, then some of you must have come across these really weird twin-like functions. Well, fret not, I am here to clear it all out for you guys today.

Read this detailed guide on assert vs require in Solidity to clarify for doubts.

Noteworthy References

https://ethereum.stackexchange.com/a/24185/80077

https://medium.com/blockchannel/the-use-of-revert-assert-and-require-in-solidity-and-the-new-revert-opcode-in-the-evm-1a3a7990e06e

https://www.investopedia.com/news/what-byzantium-hard-fork-ethereum/

https://docs.soliditylang.org/en/v0.4.24/control-structures.html