Solidity Tutorial: all about Modifiers

Jean Cvllr
Coinmonks

--

Modifiers are analog to the decorator pattern (source: https://refactoring.guru/design-patterns/decorator)

In Solidity, Modifiers express what actions are occurring in a declarative and readable manner. They are similar to the decorator pattern used in Object Oriented Programming.

What is exactly a Modifier in Solidity ?

The Solidity documentation define a modifier as follow:

A function modifier is a compile-time source code roll-up.

It can be used to amend the semantics of functions in a declarative way.

From this definition, we can understand that a modifier aims to change the behaviour of the function to which it is attached.

For instance, automatically checking a condition prior to executing the function (this is mainly what they are used for).

Modifiers are useful because they reduce code redundancy. You can re-use the same modifier in multiple functions if you are checking for the same condition over your smart contract.

When to use a modifier in Solidity?

The main use case of modifiers is for automatically checking a condition, prior to executing a function. If the function does not meet the modifier requirement, an exception is thrown…

--

--