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.
Modifiers and the Checks-Effects-Interactions Pattern is a free Blockchain Smart Contracts with Solidity lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Blockchain Smart Contracts with Solidity learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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");
_;
}Applying a Modifier
Attach a modifier to a function declaration. The check runs automatically on every call, keeping the function body focused on logic.
function withdraw() public onlyOwner {
payable(owner).transfer(address(this).balance);
}Modifiers with Parameters
Modifiers can take arguments, making them flexible for validating dynamic conditions such as minimum values.
modifier costs(uint price) {
require(msg.value >= price, "Not enough");
_;
}Stacking Multiple Modifiers
You can apply several modifiers to one function; they run left to right. Keep them small and single-purpose so combinations stay predictable.
function buy() public payable onlyWhenOpen costs(1 ether) {
// ...
}The Reentrancy Danger
When a contract calls out to an external address, that address can call back into your contract before the first call finishes. This reentrancy can drain funds if state is updated too late.
Checks-Effects-Interactions
The checks-effects-interactions pattern orders your function in three phases: validate inputs (checks), update state (effects), then call external contracts (interactions). External calls come last.
A Vulnerable Withdraw
This sends ETH before zeroing the balance, leaving a reentrancy window.
// VULNERABLE
function withdraw() public {
uint amt = balances[msg.sender];
payable(msg.sender).call{value: amt}("");
balances[msg.sender] = 0; // too late!
}The Safe Version
Update state before the external call so a reentrant call sees a zero balance.
// SAFE
function withdraw() public {
uint amt = balances[msg.sender];
balances[msg.sender] = 0; // effects first
payable(msg.sender).call{value: amt}(""); // interaction last
}Reentrancy Guards
For extra safety, add a reentrancy guard modifier that sets a lock flag during execution, rejecting nested calls. Libraries like OpenZeppelin provide nonReentrant.
Combining the Patterns
Use modifiers for the checks phase (access and validation), structure the body as effects-then-interactions, and add a guard on functions that move value. Together they form a strong defensive baseline.
Quick Check
Check your knowledge of safe contract patterns.
Recap
You learned defensive Solidity patterns:
- Modifiers centralize preconditions like access control
- They can take parameters and be stacked
- Checks-effects-interactions orders functions to prevent reentrancy
- Add a reentrancy guard on value-moving functions
These patterns are foundational to writing secure contracts.
Frequently asked questions
Is the “Modifiers and the Checks-Effects-Interactions Pattern” lesson free?
Yes — the full text of “Modifiers and the Checks-Effects-Interactions Pattern” is free to read here on the web, and the Blockchain Smart Contracts with Solidity course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Blockchain Smart Contracts with Solidity course, upgrade to CoddyKit PRO.
What will I learn in “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. You practise Blockchain Smart Contracts with Solidity with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Blockchain Smart Contracts with Solidity?
No prior experience is required. Blockchain Smart Contracts with Solidity on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Modifiers and the Checks-Effects-Interactions Pattern” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Blockchain Smart Contracts with Solidity lesson?
Yes. Every Blockchain Smart Contracts with Solidity lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Inheritance and Interfaces
- Libraries and Abstract Contracts
- Error Handling with Revert/Require
- Modifiers and the Checks-Effects-Interactions Pattern