Gas Optimization tricks with Assert and Require in Solidity

Gas Optimization tricks with Assert and Require in Solidity

By Dspyt | BlockChain Tech | 1 Mar 2023


Solidity is a popular programming language used to develop decentralized applications (DApps) and smart contracts on the Ethereum blockchain. It is important to ensure that conditions are checked before executing certain code, and this is done using two functions: assert and require.

Assert and require are both used to check the validity of conditions before executing code, however, they differ in how they are used for gas optimization. Assert is used for debugging purposes as it will always consume gas when called. On the other hand, require is designed for production use as it will not consume any gas if the condition is already true.

If the condition is not met in either case, the transaction will be reverted. However, it is important to note that a revert does not consume any gas, whereas a throw does. Therefore, it is important to be aware of the differences between assert, require, revert and throw when developing DApps on Ethereum.

In conclusion, assert and require are two important functions in Solidity which can help ensure that conditions are met before executing code. They both revert transactions if the condition is not met, however they differ in their use for gas optimization. It is important to understand these differences when developing DApps on Ethereum.

Articles Mentioned in the video:

Assert vs Require in Solidity – Learn the Difference [Best 2021 Guide] - https://codeforgeek.com/assert-vs-require-in-solidity/

Stackoverflow thread: Difference between require and assert and the difference between revert and throw - https://ethereum.stackexchange.com/questions/15166/difference-between-require-and-assert-and-the-difference-between-revert-and-thro

About Dspyt Channel:
We are Data Science with Python DAO
Homepage: https://dspyt.com
GitHub DAO Page: https://github.com/dspytdao

How do you rate this article?

6


Dspyt
Dspyt

Data Science with Python and blockchain DAO. We cover econometrics, python programming, blockchain technology and many more topics. https://dspyt.com


BlockChain Tech
BlockChain Tech

Interesting Blockchain technology and breakthrough functionality

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.