Back to Blog

Solidity modifiers: a cleaner alternative to if statements

September 29, 2025 3 min read
Software Development

Introduction

Solidity is a powerful language to program smart contracts. Because it is relatively new and directly involved in financial transactions, it is crucial to write clean and secure code. One common challenge is how to enforce conditions (like ownership checks) without repeating logic everywhere.

In solidity, we often need to enforce conditions. Writing those checks with if statements repeatedly can make code messy and error-prone.

Let’s check this code example

// SPDX-License-Identifier: MIT
pragma solidity >0.8.15 < 0.9.0;

contract ContractExample {
    address public owner;
    string public message;

    // assign address to the owner variable once contract is deployed
    constructor() {
        owner = msg.sender;
    }

    // here we check if the person who updates message var is the actual owner if not we revert the execution
    function updateMessage(string memory newMessage) public  {
        //We may refactor this piece of code to avoid repetition
        if (msg.sender != owner) {
            revert("You need to be the owner in order to update the message");
        }

        message = newMessage;

    }
}

As this code is a simple example of a state variable update, there is a subtle caveat that if we want to check for ownership on every function, we need to repeat this part of the code.

if (msg.sender != owner) {
  revert("You need to be the owner in order to update the message");
}

// code

There is another way to implement this code, for example, using the concept of modifiers.

A modifier is a construct in Solidity that changes how functions behave. Modifiers let you enforce conditions or add functionality before a function’s logic runs, without rewriting that logic inside every function keeping our code DRY (don’t repeat yourself).

// SPDX-License-Identifier: MIT
pragma solidity >0.8.15 < 0.9.0;

contract ContractModifiersExample {

    address public owner;
    string public message;

    modifier isOwner() {
        // if the person who calls the function is not the owner we revert the execution
        require(msg.sender == owner, "You need to be the owner in order to update the message");
        // if the person who calls the function is the owner we continue with the execution
        _;
    }

    constructor() {
        owner = msg.sender;
    }

    function updateMessage(string memory newMessage) public isOwner {
        message = newMessage;
    }
}

In Solidity, the require statement is used to check conditions before executing the rest of the function.

  • If the condition is true, the function continues.
  • If the condition is false, the execution reverts, any state changes are undone, and the remaining gas is refunded to the caller.

Some advantages of using modifiers

  • Reusability: write once, apply anywhere.
  • Readability: functions remain focused only on their purpose
  • Maintainability: update the check in one place instead of everywhere

Conclusion

Modifiers are a useful feature for reusing code and enhancing its readability. They perform certain checks before executing the function code, while modifiers don’t directly reduce gas cost, they help prevent mistakes that could lead to wasted gas or vulnerabilities. For that reason, modifiers are a best practice when writing solidity contracts.

Check out the full code on GitHub

Code repository

Thanks for reading, and see you in the next one!