Modifiers and the Checks-Effects-Interactions Pattern
Use function modifiers to enforce preconditions cleanly and apply the checks-effects-interactions pattern to write safer contracts.
What Are Modifiers?
A modifier is reusable code that runs before (or around) a function body. It is ideal for enforcing preconditions like access control without repeating the same checks everywhere.
Declaring a Modifier
The special _; placeholder marks where the function body executes. Code before it runs first; code after runs when the function returns.
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}All lessons in this course
- Inheritance and Interfaces
- Libraries and Abstract Contracts
- Error Handling with Revert/Require
- Modifiers and the Checks-Effects-Interactions Pattern