Solidity theorytheory 0/50 · 0%
Structure · medium

27. Modifiers in depth

How `_;` splices code, and when to prefer a function.

A modifier's body runs around the function: code before `_;` runs first, code after runs on the way out. Multiple modifiers nest left to right.

modifier nonReentrant() {
    require(!locked, "reentrant");
    locked = true;
    _;
    locked = false;
}

Modifiers duplicate their bytecode at every use site, so a long modifier can bloat your contract; call an internal function from a short modifier instead.

Check your understanding

  1. 1. What does `_;` represent?

  2. 2. How do stacked modifiers run?

  3. 3. Why keep modifiers short?