Smart-contract development

function modifier

A modifier is a reusable wrapper you tag onto functions to run shared checks before (or after) the function body. The textbook example is onlyOwner: write the 'are you the owner?' check once as a modifier, then attach it to every privileged function instead of copy-pasting the same require into each one.

The mechanism hinges on a single placeholder, _;, inside the modifier body. Everything written before _ runs as a precondition (typically require checks); the underscore marks exactly where the wrapped function's own body is spliced in; anything after _ runs as a postcondition. The compiler effectively inlines the modifier's code into each function it decorates, so a heavy modifier increases the deployed bytecode size once per use site. When several modifiers are stacked on one function they nest left to right, each wrapping around the next at its underscore.

Modifiers are ideal for cross-cutting concerns: access control, reentrancy guards (lock before _, unlock after), and pause switches. Two pitfalls matter. First, a modifier whose _; is unreachable (for instance behind a return) silently prevents the function body from ever running — a dangerous, easy-to-miss bug. Second, because the body is inlined, very large modifier logic bloats every decorated function; for heavy checks it is often cleaner to call an internal helper function from inside a thin modifier.

modifier onlyOwner() {
  require(msg.sender == owner, "not owner");
  _;   // the wrapped function body runs here
}
function setFee(uint256 f) external onlyOwner { fee = f; }

Write the guard once, reuse it everywhere.

A modifier that can reach its end without hitting _; will skip the function body entirely while still returning 'successfully'. Always make sure the underscore is reachable.