What are Solidity modifiers?
Answer
Modifiers in Solidity are reusable code patterns that change the behavior of functions — typically for access control and validation. They use the _; placeholder to indicate where the function body executes. Example: modifier onlyOwner() { require(msg.sender == owner, "Not owner"); _; } modifier nonReentrant() { require(!locked, "Reentrant call"); locked = true; _; locked = false; } function withdraw() external onlyOwner nonReentrant { payable(msg.sender).transfer(address(this).balance); }. Common modifier patterns: (1) Access control — onlyOwner, onlyAdmin, onlyRole(bytes32 role); (2) State checks — whenNotPaused, onlyWhenActive; (3) Reentrancy guard — nonReentrant prevents reentrancy attacks; (4) Input validation — check parameters before function body. Modifiers are inlined at compile time. Multiple modifiers can be chained: function adminAction() external onlyOwner whenNotPaused { ... }. The OpenZeppelin Contracts library provides battle-tested modifiers via Ownable, AccessControl, Pausable, and ReentrancyGuard.
Previous
What is the difference between `memory` and `storage` in Solidity?
Next
What are Solidity events?